* [[https://www.learnpython.org/|learnpython.org]] * [[https://www.learnpython.org/en/Modules_and_Packages|Modules and Packages]] * module 就是一個 python 檔案,package 是一個目錄包含檔案 ''__init__.py'',目錄下可以有零到多個 module。 * [[https://www.learnpython.org/en/Sets|Sets]] * set 是 list 的特化,不包含重複的元素。 a = set([1, 2, 3, 1]) # 1 不重複 * [[https://openhome.cc/Gossip/Python/|語言技術:Python Gossip]] * [[https://openhome.cc/Gossip/Python/ModuleABC.html|簡介模組]] #!/usr/bin/env python # -*- coding: utf-8 -*- def doSome(text): return text + '...processed...' def main(): fixture = 'orz' print(doSome(fixture)) if __name__ == '__main__': main() * [[https://openhome.cc/Gossip/Python/NumericType.html|數值型態]] # 將 16 進制的 a 轉成 2 進制格式,去掉開頭的 "0b",並將剩下的字符串反向表示。 >>> bin(int("a", 16))[2:][::-1] '0101' * [[https://openhome.cc/Gossip/Python/LambdaExpression.html|lambda 運算式]] max = lambda m, n: m if m > n else n max(1, 2) * [[https://openhome.cc/Gossip/Python/Class.html|定義類別]] class Account: def __init__(self, number, name, balance = 0): self.number = number self.name = name self.balance = balance * [[https://docs.python.org/2/tutorial/index.html|The Python Tutorial]] * [[https://docs.python.org/2/tutorial/datastructures.html#using-lists-as-stacks|5.1.1. Using Lists as Stacks]] * [[https://docs.python.org/2/library/index.html|The Python Standard Library]] * [[https://docs.python.org/2/library/string.html|7.1. string — Common string operations]] # 可以用此來組合命令行參數。 '{2}, {1}, {0}'.format('a', 'b', 'c') * [[https://docs.python.org/2/library/re.html|7.2. re — Regular expression operations]] * [[https://docs.python.org/2/howto/regex.html#regex-howto|Regular Expression HOWTO]] * [[http://www.thegeekstuff.com/2014/07/advanced-python-regex/|3 Advanced Python RegEx Examples (Multi-line, Substitution, Greedy/Non-Greedy Matching in Python)]] import re p = re.compile(r'Hello (Kitty)') # group(0) 代表匹配到的全部字串。 print p.match('Hello Kitty').group(0) # group(1) 代表第一個括號內匹配到的字串。 print p.match('Hello Kitty').group(1) * [[https://docs.python.org/2/library/argparse.html|15.4. argparse — Parser for command-line options, arguments and sub-commands]] * [[https://docs.python.org/2/howto/argparse.html|Argparse Tutorial]] $ cat samply.py import argparse parser = argparse.ArgumentParser() parser.add_argument("target", help="the target architecture") parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() if (args.verbose): print "verbose: " + args.target else: print args.target $ python sample.py x86 * [[https://docs.python.org/2/library/subprocess.html#module-subprocess|17.1. subprocess — Subprocess management]] * [[https://blog.aweimeow.tw/2016/09/09/python-subprocess-%E5%90%84%E5%87%BD%E5%BC%8F%E7%9A%84%E4%BD%BF%E7%94%A8%E6%99%82%E6%A9%9F/|PYTHON SUBPROCESS 各函式的使用時機]] * [[https://imsardine.wordpress.com/tech/shell-scripting-in-python/|Shell Scripting in Python]] #!/usr/bin/env python import subprocess # 命令執行完之後才會返回。返回 return code。 retcode = subprocess.call('ls -l'.split()) # retcode 非 0 時,丟出異常。只需要返回值的時候適用。 try: retcode = subprocess.check_call('exit 1', shell=True) except subprocess.CalledProcessError as e: print e # retcode 非 0 時,丟出異常。返回 stdout。命令行有 pipe,改以直接調用 shell 執行。 try: stdout = subprocess.check_output('ls -l | wc -l; exit 0', shell=True) except subprocess.CalledProcessError as e: print e # 不用 shell=True,改以 Popen 達成 pipe 效果。前述函式最終皆是調用 Popen。以 ls 的輸出當作 wc 的輸入。 ls = subprocess.Popen('ls -l'.split(), stdout=subprocess.PIPE) wc = subprocess.Popen('wc -l'.split(), stdin=ls.stdout, stdout=subprocess.PIPE) # communicate 傳回 stdout 和 stderr。這裡只取 stdout。 print(wc.communicate()[0]) wc = subprocess.Popen('wc -l'.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE) print(wc.communicate(input='\n\n\n')[0]) * [[https://stackoverflow.com/questions/13332268/python-subprocess-command-with-pipe|Python subprocess command with pipe]] ====== 外部連結 ====== * [[https://www.python.org/|Python]] * [[http://www.greenteapress.com/thinkpython/thinkpython.pdf|Think Python]]