2017年12月20日 星期三

Python的Unicode HOWTO

https://docs.python.org/3/howto/unicode.html
最早的ASCII碼(Code),只有0~127(7bits),無法包含更多特殊字。
1980年代的個人電腦幾乎都是8bit,所以可以處理ASCII且還多出128~255可以使用,但不同機器有不同的Code。
Unicode一開始用16bits來取代原來的8bits,有2^16 (65536)個值可使用,目標是可包含所有的語言,但其實還是不夠。後來擴展到 0 through 1,114,111 ( 0x10FFFF in base 16).
一個字元(Character),Unicode定義了一個字元的Code point,就是這個字元的值。表示法如U+12CA就表示值是0x12ca的字元。
Encoding(編碼)是指從字串轉為位元組序列的規則,UTF-8就是一種最普遍的編碼方式,UTF (Unicode Transformation Format),8是指8-bits的數字被使用來編碼。規則是這樣:
  1. If the code point is < 128, it’s represented by the corresponding byte value.
  2. If the code point is >= 128, it’s turned into a sequence of two, three, or four bytes, where each byte of the sequence is between 128 and 255.
Python的預設編碼方式是UTF-8,可用特殊的註解改變(第一或第二行)
# -*- coding:  -*-
Python支援以Unicode為名的變數
若想保持Python的原始碼是ASCII-only,可用Escape字符 /u或/U或/N字元名來編寫
>>> "\N{GREEK CAPITAL LETTER DELTA}"  # Using the character name
'\u0394'
>>> "\u0394"                          # Using a 16-bit hex value
'\u0394'
>>> "\U00000394"                      # Using a 32-bit hex value
'\u0394'
另外,可用bytes的method "decode()"來輸出Unicode,另外帶encoding參數和error處理方式(請看最上方連結)
Python 3.2有100種不同的encoding。
單個字元的轉換,可用chr(int)輸出Unicode,用ord(str)輸出值(code point)
字串與位元組串的操作是bytes.decode() 和 str.encode()

The most important tip is:

Software should only work with Unicode strings internally, decoding the input data as soon as possible and encoding the output only at the end.

2017年11月28日 星期二

Bash if

if [[ -n "${VAR}" ]]; then
  echo variable VAR is set and value is ${VAR}
fi

if [[ -z "${VAR}" ]]; then
  echo variable VAR is unset or empty
fi

                        +-------+-------+-----------+
                VAR is: | unset | empty | non-empty |
+-----------------------+-------+-------+-----------+
| [ -z "${VAR}" ]       | true  | true  | false     |
| [ -z "${VAR+set}" ]   | true  | false | false     |
| [ -z "${VAR-unset}" ] | false | true  | false     |
| [ -n "${VAR}" ]       | false | false | true      |
| [ -n "${VAR+set}" ]   | false | true  | true      |
| [ -n "${VAR-unset}" ] | true  | false | true      |
+-----------------------+-------+-------+-----------+

if [[ -e file.txt ]]; then
  echo file.txt is exist
fi

if [[ ! -e file.txt ]]; then
  echo file.txt is NOT exist
fi

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

2017年11月20日 星期一

docker jekyll on windows 10

docker run -p 80:4000 -v c:\tmp:/srv/jekyll jekyll/jekyll jekyll s -s ./my-site --watch --force-polling

Browse http://127.0.0.1:4000

on Windows 7  with Docker toolbox:
replace "c:\tmp" to "/c/tmp"

docker run -p 4000:4000 -v /c/Users/Jack_Lin/Documents/abc:/srv/jekyll jekyll/jekyll jekyll s --watch --force-polling

Browse http://192.168.99.100:4000

If your want to customize minima theme. check
https://github.com/jekyll/minima

Setup proxy on Docker Toolbox on Windows7

https://github.com/docker/toolbox/issues/102

Hi!,
Just tested the following steps on Win7 with Docker-toolbox and it works ! Wow!
Split from windows command directory to linux with the following command:
$ docker-machine ssh default
Now the command line start with "docker@default:~$"
Write sudo ..... as the following line
docker@default:~$ sudo vi /var/lib/boot2docker/profile
Write "i" to pass in insert mode and return button.
Add the two http export settings with username and password :
export "HTTP_PROXY=http://username:password@proxy.something.it:port"
export "HTTPS_PROXY=http://username:password@proxy.something.it:port"
Digit esc ": wq!" to save the new line and verify if everithing is ok with the cat command
docker@default:~$ cat /var/lib/boot2docker/profile
Restart the daemon
docker@default:~$ sudo /etc/init.d/docker restart
Now i have restarted the Docker toolbox and the "Docker pull image_name" work fine!
Thanks a lot to everybody!

2017年8月18日 星期五

Setup Centos syslog server and Ubuntu syslog client

On CentOS syslog server: (172.16.0.1)
edit /etc/rsyslogd.conf
uncomment
$ModLoad imtcp
$InputTCPServerRun 514

add a line after "*.emerg"
*.emerg                /var/log/emerg.conf

Add firewall rule of TCP port 514
# firewall-cmd --permanent --zone=public --add-port=514/tcp
# firewall-cmd --reload

On Ubuntu syslog client
edit /etc/rsyslog.d/50-default.conf
add a line after "*.emerg"
*.emerg           @@172.16.0.1

Try on client:
# logger -p user.emerg "emerg from XXXX"

Check the syslog server is recieved.
# cat /var/log/emerg.log

vga=0x319

# vim /etc/defualt/grub
add GRUB_CMDLINE_LINUX="nomodeset vga=0x319"

# grub-mkconfig -o /boot/grub/grub.cfg