2009年8月11日 星期二

正規表示式1 regular expressions


出處 http://osteele.com/tools/rework/

For more information about how to use regular expressions, including examples, additional documentation, and additional tools, see:
Regular-Expressions.info (online)
RegExLib (online)
Jeffrey Freidl's Mastering Regular Expressions (Amazon)

Quick Reference:
. any character except newline. If DOTALL, matches newline.
^ the start of the string. In multiline, matches start of each line.
$ the end of the string or just before the last newline. In multiline, matches before each newline.
\d,\w,\s digit, word, or whitespace, respectively
\D,\W,\S anything except digit, word, or whitespace
\. a period (and so on for \*, \(, etc.)
[ab] characters a or b
[a-c] a through c
[^ab] any character except a or b
expr* zero or more repetitions of expr
expr+ one or more repetitions of expr
expr? zero or one repetition of expr
*?,+?,?? ...same as above, but as little text as possible
expr{m} m copies of expr
expr{m,n} between m and n copies of the preceding expression
expr{m,n}? ...but as few as possible
a|b either a or b
(expr) same as expr, but captures the match for use in \1, etc.
(?:expr) doesn't capture the match
(?=expr) followed by expr
(?!expr) not followed by expr

Rsync 參數說明


使用方式:
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC [DEST]
rsync [OPTION]... SRC [SRC]... DEST
rsync [OPTION]... [USER@]HOST::SRC [DEST]
rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

參數說明: (網路上的中文說明好像都這份~~所以也不知道真正的出處在那。)

-h , --help 顯示rsync求助資訊.
--version 顯示rsync版本.
-v , --verbose 複雜的輸出訊息.
-q , --quiet 安靜模式,幾乎沒有訊息產生.常用在以cron執行rsync.
-I, --ignore-times 通常rsync為了加快速度會忽略同樣檔案大小且同樣存取時間點的檔案.可以透過此參數關閉此快速檢查.
--size-only rsync只檢查檔案大小是否改變,不管時間存取點是否改變.通常用在mirror,且對方時間不太正確時.
-c, --checksum 在傳送之前透過128bit的md4檢查碼來檢查所有要傳送的檔案.(會拖慢速度.)
-a, --archive archive mode 權限保存模式,相當於 -rlptgoD 參數.
很快速的保存幾乎所有的權限設定,除了硬式連結(透過-H設定).
-r, --recursive 複製所有下層的資料(遞迴)
-R, --relative 使用相對路徑.
如:
rsync foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo.c檔案
rsync -R foo/bar/foo.c remote:/tmp/ 在遠端產生/tmp/foo/bar/foo.c 檔案
-R, --no-relative 不使用相對路徑.
-b, --backup 目的地端先前已經存在的檔案在傳輸或刪除前會被備份.
--backup-dir=DIR 設定備份的資料夾.
--suffix=SUFFIX 指定備份的檔案名稱字尾形式(預設為~).
-K, --keep-dirlinks 接收方將連結到資料夾的檔案視為資料夾處理
-l, --links 複製所有的連結
-H, --hard-links 保留硬式連結
-p, --perms 保留檔案權限
-o, --owner 保留檔案擁有者(root only)
-g, --group 保留檔案群組
-D, --devices 保留device資訊(root only)
-t, --times 保留時間點
-n, --dry-run 不實際執行傳送,只顯示將會有的傳輸動作
-S, --sparse 嘗試去處理稀疏的檔案,讓這些檔案在目的端佔去較少的磁碟空間.
-W, --whole-file 複製所有的檔案,不額外作檢查.
--no-whole-file 關閉 --whole-file 參數
-x, --one-file-system 不要跨越檔案系統分界(只在一個檔案系統處理)
-B, --block-size=SIZE 強制透過rsync程式去比對修復block-sizeforce
-e --rsh=COMMAND 定義所使用的remote shell
--rsync-path=PATH 定義rsync在遠端機器存放資料的路徑
--existing 只比對更新目的端已經存在的檔案
--ignore-existing 忽略目的端已經存在的檔案(也就是不更新)
--delete 刪除傳送端已經不存在,而目的端存在的檔案
--delete-excluded 除了把傳送端已經不存在,而目的端存在的檔案刪除之外,也刪除 --exclude 參數所包含的檔案.
--delete-after rsync預設會在檔案傳送前進行相關刪除動作確保接收端有足夠的檔案空間,但可以透過 --delete-after 讓刪除動作在檔案傳送後再行刪除.
--ignore-errors 忽略任何錯誤既使是I/O error 也進行 --delete 刪除動作.
--max-delete=NUM 定義rsync不要刪除超過 NUM 個檔案.
--partial rsync若遇到傳輸過程中斷時,會把那些已經傳輸的檔案刪除.在某種狀況下保留那些部分傳送的檔案是令人高興的.你可以透過 --partial 參數達到這個目的.
--partial-dir=DIR 在 --partial 參數啟動時,你還可以定義rsync把那些部分傳送的檔案寫入定義的資料夾,而非直接寫入目的端.需要注意的是,此資料夾不應該被其他使用者可以寫入.(如:/tmp)
--force 當目的端資料夾被傳送端非資料夾名稱覆蓋時,強制rsync刪除資料夾,即使該資料夾不是空的.
--numeric-ids 不將傳送端檔案的uid及gid值,與目的端的使用者/群組進行配對.若傳送端並沒有uid及gid的對應名稱(如:原帳號群組被刪除的遺留檔案),或目的端沒有相對應的帳號/群組,保留數字型態的uid/gid
--timeout=TIMEOUT 設定 I/O 逾時的時間(秒). 超過這個秒數而沒有資料傳送,rsync將會結束.預設為0,也就是沒有定義逾時時間.
-T, --temp-dir=DIR 定義rsync在接收端產生暫時性的複製檔案時使用資料夾暫存.預設是直接在接收端資料夾直接產生暫存檔案.
--compare-dest=DIR 定義rsync在目的端建立資料夾來比對傳送過來的檔案.
--link-dest=DIR 與 --compare-dest 相同,但同時會針對無法改變的檔案建立硬式連結.
-z, --compress 壓縮模式,當資料在傳送到目的端進行檔案壓縮.
-P -P參數和 --partial --progress 相同.只是為了把參數簡單化.
-C, --cvs-exclude 排除那些通常不希望傳送的檔案.定義的方式與CVS傳送相同:
RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS .make .state .nse_depinfo *~ #* .#* ,* _$* *$ *.old *.bak *.BAK *.orig *.rej .del-* *.a *.olb *.o *.obj *.so *.exe *.Z *.elc *.ln core .svn/ 符合以上條件的都會被忽略而不傳送.
--exclude=PATTER 符合PATTERN(規則表示式)樣式的檔案不進行傳送
--exclude-from=FILE 和--exclude參數相同,不過是把不進行傳送的檔案事先寫入某一檔案.執行時,透過此參數讓rsync讀取.(; #開頭的行列或空白行會被rsync忽略)
--include=PATTERN 定義rsync不要排除符合pattern樣式的檔案.
--include-from=FILE 和--include參數相同,只不過把要包含的檔案寫到某一檔案.
--files-from=FILE 把要傳送的檔案名稱都精確的寫入某一檔案,讓rsync讀取.
如: rsync -a --files-from=/tmp/foo /usr remote:/backup
-0 --from0 定義檔案所要讀取的檔案是null字元結尾.
--version 顯示版本訊息.
--daemon 定義rsync以daemon型態執行.
--no-detach 當以daemon型態執行時,不要進行分裂且變成背景程序.
--address=ADDRESS 定義所要連結(bind)的ip位址或是host名稱(daemon限定)
--config=FILE 定義所要讀取的設定檔rsyncd.conf位置(daemon限定) 預設值為 /usr/local/etc/rsyncd.conf
--port=PORT 定義rsyncd(daemon)要執行的port(預設為tcp 873)
--blocking-io 使用blocking I/O連結遠端的shell,如rsh , remsh
--no-blocking-io 使用non-blocking連結遠端的shell,如ssh (預設值)
--stats 顯示檔案傳送時的資訊狀態
--progress 顯示傳送的進度.(給檔案傳送時,怕無聊的人用的..)
--log-format=FORMAT 定義log的格式(在rsyncd.conf設定)
--password-file=FILE 從檔案讀取與遠端rsync伺服器連結的密碼
--bwlimit=KBPS 定義傳輸頻寬的大小(KBytes/秒)
--write-batch=FILE 把紀錄資料寫入一個檔案(給其他相同環境且相同需求的機器使用)
--read-batch=FILE 透過讀取紀錄檔案來進行傳輸.(檔案由 --write-batch 參數產生)
--checksum-seed=NUM 定義檔案 checksum-seed 的大小(byte)
-4 --ipv4 使用IPv4協定
-6 --ipv6 使用IPv6協定

2009年8月4日 星期二

Python 套件管理

http://sourceforge.net/projects/pythonpkgmgr/

Python Package Manager is a cross platform tool for Python to assist with the downloading and installation of python packages. Coded in Python, and using wxWidgets, this program is a GUI that drives easy_install and/or pip.




psyco python加速模組


#Download From:
#python 2.6 win32
# http://www.voidspace.org.uk/python/modules.shtml#psyco
#
#python 2.5 win32
# http://sourceforge.net/projects/psyco/files/
#
# psyco is a python accelerator which speeds up pysync by 33%

#說明網頁
#http://psyco.sourceforge.net/psycoguide/node8.html

#For larger applications, try:
import psyco
psyco.profile()

#------
try:
import psyco
psyco.profile()
except:
pass

#other import
from xxxx import *
import xxx,xxxx

#------
if __name__ == '__main__':
# Import Psyco if available
try:
import psyco
psyco.full()
except ImportError:
pass
# ...your code here...

#------
psyco.full()#對所有函數用psyco進行編譯
psyco.bind(myfunction1)#對選擇的函數用psyco進行編譯

g = psyco.proxy(f) #對函數 f用psyco進行編譯
g(args) #Psyco-accelerated call #編譯後 g函數速度會有提升
f(args) #regular slow call #f函數保持原來的調用速度

psyco.log # 用來紀錄log #Enable logging to a file named xxx.log-psyco by default, where xxx is the name of the

psyco.profile() # 可用來替代 psyco.full()

#------
轉貼:: http://www.cnpython.org/docs/300/p_240.html

使用時,在需要做效率優化的源文件前面加入以下兩句就一切OK了
import psyco
psyco.full()

psyco.profile()可以對大程序進行適當分析,以確定哪些函數最值得編譯。
psyco.log()函數用來記錄profile()得到的信息,下次就可以運行就能更快一點。
psyco.bind(myfunc)指定對函數myfunc進行編譯,可以做到比full()更精細的控制。
psyco.proxy(f)創建一個新的函數,它的代碼是由f編譯得到二進制碼。

import math, timeit, psyco

def TestA():
res, loopcnt = 0.0, 100
for i in range(loopcnt):
for j in range(loopcnt):
for k in range(loopcnt):
res = res + math.sin(i+j+k) # loopcnt^3 times

if __name__=='__main__':
TestB = psyco.proxy(TestA)
ta = timeit.Timer("TestA()", "from __main__ import TestA")
tb = timeit.Timer("TestB()", "from __main__ import TestB")
print("TestA(): %.2fs" % (ta.timeit(10)))
print("TestB(): %.2fs" % (tb.timeit(10)))

運行結果如下:
$ python test.py
TestA(): 15.84s
TestB(): 1.82s

很明顯,使用Psyco處理過的函數執行速度快了大約7~8倍,比Psyco作者宣稱的平均加速4倍的倍率更大些。
Psyco的工作原理類似JIT,只不過是on-the-fly的,對用戶而言基本透明。Psyco的主要缺點是耗費內存資源較多,在我看來不是什麼大問題,也許做網絡服務和企業應用的人才需要考慮這些吧。

tw.archive.ubuntu.com apt-get掛站處理方法

## source.list 將tw.archive.ubuntu.com取代為mirror.nttu.edu.tw

/etc/apt# vim sources.list
:1,$s/tw.archive.ubuntu.com/mirror.nttu.edu.tw/g


VIM字串搜尋與取代
s(substitute)指令可搜尋某行列範圍。
g(global)指令則可搜尋整個編輯緩衝區的資料。
s指令以第一個滿足該條件的字串為其取代的對象,若該行有數個滿足該條
件的字串,也僅能取代第一個,若想取代所有的字串則需加上g參數。
:1,$s/old/new/g 將檔案中所有的『old』改成『new』。
:10,20s/^/ / 將第10行至第20行資料的最前面插入5個空白。
:%s/old/new/g 將編輯緩衝區中所有的『old』改成『new』。