2013年5月15日 星期三
2013年5月14日 星期二
Python: _() 的用途
_在Django中是為了i18n(internationalization)的目的,為了將字串轉譯成其他語言。
http://stackoverflow.com/questions/3967231/whats-the-meaning-of-in-python
Python - “_” Argument to functions
In the interactive interpreter _ is used to reference the last returned value
Eg
>>> 2 + 4
6
>>> _ + 4
10
So you can use it as an argument in a function as well
>>> 2 + 4
6
>>> for i in range(_): print(i)
0
1
2
3
4
5
http://stackoverflow.com/questions/10232404/python-argument-to-functions
2013年5月10日 星期五
Python: getopt範例
#!/usr/bin/env python
#
# 1. scp the compiled rpm tarball to the hosts we want to deploy
# 2. then extract it
# 3. install it
import sys
from getopt import getopt
from getopt import GetoptError
from fabric.api import env, run
def usage():
print "Usage: %s -h <deploy-host> (-f <file> | -e <cmd>)" % sys.argv[0]
print " %s -H <deploy-host-file> (-f <file> | -e <cmd>)" % sys.argv[0]
print "OPTIONS:"
print " -h, --host: host list"
print " -H, --hostfile: host list file"
print " -f, --file: deployfile"
print " -e, --exec: command execution"
print " --help, print information"
def main():
try:
"""
h: H: f: e: 加上冒號(:)代表是option後面要加上參數的意思
若只有選項而沒有在後面接上冒號,例如: "v" ,則是代表該選項後面不需要接上參數
"""
opts, args = getopt(sys.argv[1:], "h:H:f:e:", ["help","host=","hostfile=","file=","exec="])
for opt,arg in opts:
if opt in ("-h","--host"):
env.hosts = []
print a
elif opt in ("-H","--hostfile"):
env.hosts = []
print arg
elif opt in ("-f","--file"):
pass
elif opt in ("-e","--exec"):
pass
else:
Usage()
sys.exit(1)
except GetoptError:
print "Error: GetoptError!"
usage()
sys.exit(1)
if "__main__" == __name__:
main()
2013年5月6日 星期一
2013年5月5日 星期日
Python的名稱解析機制: LEGB原則
@ 變數的範圍(以及能在何處中使用)是由該變數在程式中何處指定來決定的,
而變數的搜索範圍也稱為"詞彙範圍(Lexical Scoping)"。
@ 然而在函式內任何型態的指定敘述(assignment)都會將變數名稱歸類成"區域變數"。
ex: =敘述、import、def、引數傳遞 等。
@ Python對於其所引用之變數名稱最多只搜尋四個範圍:
L: Local, 區域範圍
E: Enclosing, 任何其所在的def和lambda區域(由內到外)
G: Global,全域範圍
B: Built, 內建範圍
2013年5月4日 星期六
bash: 算術運算
bash中,算術運算有四種方式:
1. expr
2. $(())
3. $[]
4. let
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/techdoc/shell/c860.html
1. expr
2. $(())
3. $[]
4. let
http://www.csie.nctu.edu.tw/~tsaiwn/course/introcs/history/linux/linux.tnc.edu.tw/techdoc/shell/c860.html
python : for comprehension(for的包含式)
For comprehension(for的包含式)
使用()與for comprehension(包含式)事實上就是產生一個generator(產生器)。
python: tuple使用範例
tuple是"不可變動(immutable)"的物件,具有索引的特性,支援切片運算。
但tuple所謂的"不可變動(immutable)",是指不得更改在tuple中每個索引所參考的物件,而不是指不可以更變所參考物件的值。
Tuple的內容不可變動,指的是不能變動每個索引所參考的物件,而不是指物件本身不能變動(這裡指可以更變所參考物件的值)。例如:
上例中,打算指定Tuple索引0為新的串列物件是不可行的,但t[0][0]指的是改變索引0處的串列索引0的元素,同樣的t[0][1]指的是改變索引0處的串列索引1的元素,也就是相當於:
>>> t = (1,2,3,4,5)
>>> t[0]
1
>>> t[1]
2
>>> t[2]
3
>>> t[3]
4
>>> t[4]
5
>>> t[-1]
5
>>> t[-2]
4
>>> t[-3]
3
>>> t[-4]
2
>>> t[-5]
1
>>> (1,2,3,4)[0]
1
>>> (1,2,3,4)[1]
2
所以說像以下這種程式碼,當recursive=0時,就會變成 'scp -t %s' ; 若recursive=1時,則為'scp -r -t %s'
scp_command = ('scp -t %s', 'scp -r -t %s')[recursive]
但tuple所謂的"不可變動(immutable)",是指不得更改在tuple中每個索引所參考的物件,而不是指不可以更變所參考物件的值。
Tuple的內容不可變動,指的是不能變動每個索引所參考的物件,而不是指物件本身不能變動(這裡指可以更變所參考物件的值)。例如:
| >>> t = ([1, 2], [3, 4]) >>> t([1, 2], [3, 4]) >>> t[0] = [10, 20] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> t[0][0] = 10 >>> t[0][1] = 20 >>> t([10, 20], [3, 4]) >>> |
上例中,打算指定Tuple索引0為新的串列物件是不可行的,但t[0][0]指的是改變索引0處的串列索引0的元素,同樣的t[0][1]指的是改變索引0處的串列索引1的元素,也就是相當於:
| >>> t = ([1, 2], [3, 4]) >>> l = t[0] >>> l[0] = 10 >>> l[1] = 20 >>> t([10, 20], [3, 4]) >>> |
>>> t = (1,2,3,4,5)
>>> t[0]
1
>>> t[1]
2
>>> t[2]
3
>>> t[3]
4
>>> t[4]
5
>>> t[-1]
5
>>> t[-2]
4
>>> t[-3]
3
>>> t[-4]
2
>>> t[-5]
1
>>> (1,2,3,4)[0]
1
>>> (1,2,3,4)[1]
2
所以說像以下這種程式碼,當recursive=0時,就會變成 'scp -t %s' ; 若recursive=1時,則為'scp -r -t %s'
scp_command = ('scp -t %s', 'scp -r -t %s')[recursive]
2013年5月2日 星期四
2013年5月1日 星期三
常見script語言是否得加上"分號(semicolon)"來當作"語句(statement)"結束之總整理
script與萬惡的"分號(semicolon)"的淵源
在用putty寫scripts很多時候都會被萬惡的分號(;)給搞混,會心想說我現在到底在寫哪一種語言?!根據每種scripts的語言特性,整理一下到底有哪些scripts是必須得加上"分號(;)"的。
bash script
不用分號,在bash中指令的執行不須加上分號(;),分號只是用來代表指令的依序執行而已。python
不用分號,在python的世界當中是不推薦使用分號當statement的結束
php
必須得加上分號當作statement的結束。
perl
必須得加上分號當作statement的結束。
ruby
不須加上分號,因為Ruby 則是根據 shell 的傳統,例如 sh 及 csh。同一行上的多項敘述必須以分號來分隔,但並不需要在該行結尾處使用,換行字元 (linefeed) 在此的作用與分號相同。如果該行以反斜線 (\) 結尾,則可忽略之後的換行字元,如此能讓單一邏輯程式敘述行 (logical line) 橫跨數行。JavaScript
可加可不加,習慣上,JavaScript 程式敘述結尾會加上分號(;),表示一個完整敘述的結束。其實分號並不是必要的,只要每一句程式敘述皆不同行,是可以省略分號的,不過為了程式的完整性,以及日後維護程式方便,建議您最好還是養成每一行結尾都加上分號的習慣。2013年4月29日 星期一
SQLAlchemy
ORM與傳統SQL語句間的差異
ORM的出現,相信你早已由網路資料、書籍中得知許多,但心中或有疑問,為何要捨SQL指令不用,而去學習一種新的語法,新的資料存取架構?本文嘗試由反向角度出發,探討著ORM為何會出現,SQL指令又有何不妥之處,讓ORM將其隱藏起來。在你看過的資料中,多半會列出一張表,告訴你ORM有何好處,我們就從這張表開始吧。
表1 ORM的優點
1、以物件表現列、以屬性表現欄,以純OOP的概念來操控資料庫。
|
2、隱藏SQL指令,以此達到跨資料庫時,不需修改應用程式的目的。
|
3、以單一語法,操控所有資料庫,減輕學習者的負擔。
|
4、自動產生SQL指令,避免因程式設計師能力不同,寫出效率不彰的應用程式。
|
5、自動產生SQL指令,避免程式設計師對安全性的認知程度不同,寫出不安全的應用程式。
|
SQLAlchemy的特色: ORM(Object Relational Mapper) vs Core
SQLAlchemy considers the database to be a relational algebra engine, not just a collection of tables.
- Rows can be selected from not only tables but also joins and other select statements; any of these units can be composed into a larger structure.
- SQLAlchemy's expression language builds on this concept from its core.
ORM(Object Relational Mapper, 物件關係映射器)
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tablesSQLAlchemy features, http://www.sqlalchemy.org/features.html
http://www.keakon.net/2012/12/03/SQLAlchemy%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C
http://www.dotblogs.com.tw/code6421/archive/2009/02/10/7100.aspx
2013年4月24日 星期三
bash script利用exec作I/O重導入當stdin來讀檔
exec < filename # 利用exec來做I/O重導入以當成stdin
while read line
do
echo $line # 一行一行的印出
done
http://blog.longwin.com.tw/2012/06/shell-read-file-cat-line-2012/
訂閱:
文章 (Atom)