2020年12月9日 星期三

Python 上面常使用的資料格式:Json, XML 和 CSV

Python handle json

1. With http request :

resp = requests.post(....)

json_object = resp.json()

2. With string:

json_data = '{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}'

json_object = (json.loads(json_data))

3. With Json file :

with open('file.json', 'r') as f:

    json_object = json.load(f)

4. Check key exist :

if 'som_key' in json_object :

5. iterate :

for onekey on json_object:

or

for (index,key) in enumerate(json_object):

Python handle XML :
1. With string :

import xml.etree.ElementTree as ET
xml_str = '<a> <b attr="c"></b></a>'
root = ET.fromstring(r.content)

2. With http request :

resp = requests.get(...)
xml_str = resp.content
# Fall back to 1.

3. With XML file

tree = ET.parse('xmlfile.xml')
root = tree.getRoot()


2020年10月21日 星期三

調整 input type=file 樣式的方法

 HTML 裡面的 inline 和 inline-block

基本上,inline 就是讓元件在前一個元件的同一行,然後加了 block,代表可以指定寬度

所以 

inline : 同一行,動態寬度

block : 會換行,指定寬度

inline-block : 同一行,指定寬度


Label 是一個附屬元件,可以指定給其他元件,比如說 input。當對 Label 觸發事件,像是滑鼠點擊事件,則會傳給它所附屬的元件


如何調整 input type= file 之中,顯示的字和按鈕的樣式:

方法一:使用 hidden input + button 和 label 元件,然後套用 ref

方法二:用客製元件,畫好按鈕和顯示樣置,然後上面蓋一個透明的 input_file_object 

<style>

div.fileinputs {

position: relative;

}


div.fakefile {

position: absolute;

top: 0px;

left: 0px;

z-index: 1;

}


input.file {

position: relative;

text-align: right;

-moz-opacity:0 ;

filter:alpha(opacity: 0);

opacity: 0;

z-index: 2;

}

</style>

<div class="fileinputs">

<input type="file" class="file" />

<div class="fakefile">

<input />

<img src="search.gif" />

</div>

</div>

方法三:使用-webkit-file-upload-button 和 file-selector-button 等等 attribute 改來按鈕樣式

input[type=file]::-webkit-file-upload-button {
  border: 2px solid #6c5ce7;
  padding: .2em .4em;
  border-radius: .2em;
  background-color: #a29bfe;
  transition: 1s;
}

input[type=file]::file-selector-button {
  border: 2px solid #6c5ce7;
  padding: .2em .4em;
  border-radius: .2em;
  background-color: #a29bfe;
  transition: 1s;
}

input[type=file]::-webkit-file-upload-button:hover {
  background-color: #81ecec;
  border: 2px solid #00cec9;
}
 
input[type=file]::file-selector-button:hover {
  background-color: #81ecec;
  border: 2px solid #00cec9;
}

方法四:使用 label for 和 hidden input

.custom-file-input::-webkit-file-upload-button {
  visibility: hidden;
}
.custom-file-input::before {
  content: 'Select some files';
  display: inline-block;
  background: -webkit-linear-gradient(top, #f9f9f9, #e3e3e3);
  border: 1px solid #999;
  border-radius: 3px;
  padding: 5px 8px;
  outline: none;
  white-space: nowrap;
  -webkit-user-select: none;
  cursor: pointer;
  text-shadow: 1px 1px #fff;
  font-weight: 700;
  font-size: 10pt;
}
.custom-file-input:hover::before {
  border-color: black;
}
.custom-file-input:active::before {
  background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}
<label class="custom-file-input" for="Upload" >
</label>

<input id="Upload" type="file" multiple="multiple" name="_photos" accept="image/*" style="visibility: hidden">



2020年10月12日 星期一

程式設計的一些小技巧

 

1. Check key in a dictionary

PHP : "key" in obj

Python : if "key1" in d:

Java : hashMap.containsKey("bb")

2. Split string

PHP : list($encrypted_data, $iv, $ivh) = explode('::', base64_decode($garble), 2);

Python : metas = resp.content.split('/')

Java : String[] tokens = str.split(":");

3. Check variable is null/undefined

JS : if( var === undefined )


2020年8月28日 星期五

安裝新設定的 ubuntu

安裝新設定的 ubuntu
apt :
openssh-server
openssh-client
vim
cscope
ctags
chromium-browser
pinta
git


Download :
intellij idea
Android Studio
VSCode

Build Tools
autoconf
automake
libtool
pkg-config



2020年8月22日 星期六

Android 處理手機轉動

 1. 固定 layout 方向

<activity android:name=".mode.camera.WebRtcMonitorClientActivity"
android:screenOrientation="portrait" />

2. 自行處理轉動,讓系統不會呼叫到 "onStop ... "

<activity android:name=".mode.camera.WebRtcMonitorClientActivity"
android:configChanges="orientation|screenSize|keyboardHidden" />