Use command line tool in Python   陳信屹 , Hsin-Yi Chen (hychen), <ossug.hychen@gmail.com>   2011/03/26, OSDC.tw 2011, 中央研究院人...
開始前就先進廣告 ! ■   Python Conference Taiwan 2011! ■   歡迎投稿 , Call for paper! ■   今年 5/28 ( 六 ) ■   http://wiki.python.org.tw/P...
Who am I?  ■   ID: hychen ( 阿宅都靠 ID 認親的 )  ■   AboutMe: http://about.me/hychen  ■   HackingThursday Meeting 成員      http:/...
再來一個廣告 !  ■   Hacking Thursday 是由幾位居住於台北地區的自由軟體 /      開放原碼開發者所發起的聚會。  ■   八卦閒聊 / 技術交流 / 點子發想 / 找人一起做專案  ■   每週四晚上於特定咖啡店 ,...
http://timc.idv.tw/wordcloud/zh/#feed:http://hychen.wuweig.org/?feed=atom
The evolution of a Folk Programmer, Me  ■   ~2006 Linux Server / Dorm Network Management  ■   2007   play 國軍 online  ■   2...
The evolution of a Folk Programmer, Me  ■   ~2006 Linux Server / Dorm Network Management                           Play ma...
系      統      整      合The evolution of a Folk Programmer, Me  ■   ~2006 Linux Server / Dorm Network Management            ...
Agenda  ■   Execute a command and get result  ■   Command Args and Options ↔ Python function args /      kwargs  ■   Makin...
Execute a command and get result       四十歲之後,草木竹石皆可為劍。                      ~ 笑傲江湖 , 金庸
os.system  ■   Executing the command (a string) in a subshell.  ■   Can use shell variable, ex $HOME  ■   Can only get ret...
commands.getoutput  ■   Execute shell commands via os.popen() and      return status, output.  ■   Can use shell variable,...
Moresubprocess.*                                     security  ■   To spawn processes, connect to their input/output/error...
subprocess – Replacing os.system  ■   subprocess.call  ■   The arguments are list  ■   Can only get return code      ■   S...
If you want run subprocess.call with string arg...  ■   Shlex – a lexical analyzer class for simple shell-like      syntax...
subprocess.Popen  ■   spawn processes, connect to their input/output/error pipes  ■   more control of process  ■   Popen.c...
subprocess - Replacing shell pipe line  ■   subprocess.Popen : control process  ■   subprocess.PIPE  ■
Command Args and Options ↔ Python function args / kwargs●      #/usr/bin/env python      from GitPython Import this_idea  ...
Python – function args  ■   Function is a object too, every thing is object in Python  ■   Function arguments is a tuple (...
Python – function kwargs  ■   Function keyarguments is a dict (key-value pair list)                                       ...
Introduce UCLTIP     Use Command Line Tool In Python
Transform CLI Tool arguments  ■   The command string combine the option string if the value      is True(boolean)  ■   Exa...
UCLTIP - SingleCmd  ■   For command without sub command  ■   Create a callable instance by implemented __call__  ■   Raise...
Transform CLI Tool Boolean option style  ■   The command string combine the option string if the value      is True(boolea...
Transform CLI Tool Key-Value option style  ■   The command string will execute combines If the option      value is string...
Transform CLI Tool Key-Value option style II  ■   The command string will execute combines If the option      value is str...
command with sub command  ■   Without prefix    ■   With prefix      ■   git                ■   Zenity      ■   pbuilder  ...
UCLTIP - CmdDispatcher  ■   For CLI tool has sub command  ■   Command name → instance  ■   Subcommand name → method name  ...
Use UCLTIP to create a CLI tool Python Binding   「這世界滿是假象,    我行的也是邪道。 」                   ~ 道士下山 , 徐皓峰
Python Binding with Native Code(Cython or Ctype)
Python Binding (UCLTIP))
UCLTIP helper      ■   ucltip.use_helper      ■   Shourcut          ■   _c ↔ ucltip.SingeCmd          ■   _d ↔ ucltip.CmdD...
Conclusion of UCLTIP  ■   Try to find a convenient way to use CLI tool in Python  ■   Can use CLI tool in Python by OO way...
UCLTIP Installation  ■   user@host# add-apt-repository ppa:ossug-hychen/python-      ucltip  ■   user@host# apt-get instal...
Reference 1  ■   UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/  ■   UCLTIP Source Code: http://github.com/hychen/uc...
Reference 2  ■   VSGUI – Implemeted Zenity binding by UCLTIP 0.1 (may not      work now) https://github.com/hychen/vsgui
本簡報授權聲明 ■   此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款     ■   陳信屹 , Hsin-Yi Chen (hychen)     ■   Email: ossug.h...
Upcoming SlideShare
Loading in...5
×

How to use command line tool in Python @OSDC.tw 2011

2,602

Published on

introduce UCLTIP in OSDC.tw 2011

UCLTIP is a library makes you use command line tool in Python more easier. The original idea and most of basic codes are from GitPython project http://pypi.python.org/pypi/GitPython/

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,602
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
51
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

How to use command line tool in Python @OSDC.tw 2011

  1. 1. Use command line tool in Python 陳信屹 , Hsin-Yi Chen (hychen), <ossug.hychen@gmail.com> 2011/03/26, OSDC.tw 2011, 中央研究院人文科學大樓 本簡報採用 創用 CC 「姓名標示 - 非商業性」 3.0 台灣條款 本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。
  2. 2. 開始前就先進廣告 ! ■ Python Conference Taiwan 2011! ■ 歡迎投稿 , Call for paper! ■ 今年 5/28 ( 六 ) ■ http://wiki.python.org.tw/PycTW2011
  3. 3. Who am I? ■ ID: hychen ( 阿宅都靠 ID 認親的 ) ■ AboutMe: http://about.me/hychen ■ HackingThursday Meeting 成員 http://hackingthursday.wikidot.com/
  4. 4. 再來一個廣告 ! ■ Hacking Thursday 是由幾位居住於台北地區的自由軟體 / 開放原碼開發者所發起的聚會。 ■ 八卦閒聊 / 技術交流 / 點子發想 / 找人一起做專案 ■ 每週四晚上於特定咖啡店 , 通常十幾人 ■ http://www.hackingthursday.org/H4 Meeting – hacking, health, happy, heyheyhey
  5. 5. http://timc.idv.tw/wordcloud/zh/#feed:http://hychen.wuweig.org/?feed=atom
  6. 6. The evolution of a Folk Programmer, Me ■ ~2006 Linux Server / Dorm Network Management ■ 2007 play 國軍 online ■ 2008 Web 2.0 Development ■ 2009 Embedded Linux Wireless Driver Integration ■ 2010~ Ubuntu Customization/Integration
  7. 7. The evolution of a Folk Programmer, Me ■ ~2006 Linux Server / Dorm Network Management Play many linux distro / Shell Script, Perl, php ■ 2007 play 國軍 online ■ 2008 Web 2.0 Development PHP / Javascript / Python / ■ 2009 Embedded Linux Wireless Driver Integration C / Python / Shell Script ■ 2010~ Ubuntu Customization/Integration Python / Shell Script
  8. 8. 系 統 整 合The evolution of a Folk Programmer, Me ■ ~2006 Linux Server / Dorm Network Management Play many linux distro / Shell Script, Perl, php ■ 2007 play 國軍 online ■ 2008 Web 2.0 Development PHP / Javascript / Python / ■ 2009 Embedded Linux Wireless Driver Integration C / Python / Shell Script ■ 2010~ Ubuntu Customization/Integration Python / Shell Script
  9. 9. Agenda ■ Execute a command and get result ■ Command Args and Options ↔ Python function args / kwargs ■ Making command line tool Python binding faster - UCLTIP
  10. 10. Execute a command and get result 四十歲之後,草木竹石皆可為劍。 ~ 笑傲江湖 , 金庸
  11. 11. os.system ■ Executing the command (a string) in a subshell. ■ Can use shell variable, ex $HOME ■ Can only get return code ■ Success → return 0 ■ Failed → return any integer number != 0
  12. 12. commands.getoutput ■ Execute shell commands via os.popen() and return status, output. ■ Can use shell variable, ex $HOME ■
  13. 13. Moresubprocess.* security ■ To spawn processes, connect to their input/output/error pipes, and obtain their return codes. ■ Never call /bin/sh implicitly by default ■ Can not use shell variabl, ex $HOME ■ Replacement of ■ os.system, os.spawn*, os.popen*, popen2*, commands.*
  14. 14. subprocess – Replacing os.system ■ subprocess.call ■ The arguments are list ■ Can only get return code ■ Success → return 0 ■ Failed → return integer number != 0 ■ subprocess.check_call : raise CalledProcessError if Failed
  15. 15. If you want run subprocess.call with string arg... ■ Shlex – a lexical analyzer class for simple shell-like syntaxes. ■ awk “{print 1}” → [awk, {print 1}] ■
  16. 16. subprocess.Popen ■ spawn processes, connect to their input/output/error pipes ■ more control of process ■ Popen.communicate – get result ■ Popen.wait – wait command executed complete ■ Popen.sdtin ← None or subprocess.PIPE or File ■ Popen.stdout ← None or subprocess.PIPE or File
  17. 17. subprocess - Replacing shell pipe line ■ subprocess.Popen : control process ■ subprocess.PIPE ■
  18. 18. Command Args and Options ↔ Python function args / kwargs● #/usr/bin/env python from GitPython Import this_idea # ls --quoting-style=c ls(quoting-style=c)
  19. 19. Python – function args ■ Function is a object too, every thing is object in Python ■ Function arguments is a tuple (a immutable list) ■ decouple the arguments tuple ■ func(args[0], args[1], args[2]...)↔func(*args)
  20. 20. Python – function kwargs ■ Function keyarguments is a dict (key-value pair list) like hash in Perl ■ decouple the keyarguments ■ func(key1=value1, key2=value2,....) ↔ func(**kwargs) ■ func(args[0], key1=value1,...) ↔ func(*args, **kwargs) ■ non-keyword arg must before keyword arg
  21. 21. Introduce UCLTIP Use Command Line Tool In Python
  22. 22. Transform CLI Tool arguments ■ The command string combine the option string if the value is True(boolean) ■ Example ■ user@host: expr 1 + 3 → expr(1, +, 3)
  23. 23. UCLTIP - SingleCmd ■ For command without sub command ■ Create a callable instance by implemented __call__ ■ Raises ComandNotFound if command not exists ■ Return ■ Success → return result string ■ Failed → raises CommandExecutedFalur
  24. 24. Transform CLI Tool Boolean option style ■ The command string combine the option string if the value is True(boolean) ■ Example ■ -d → func(d=True) ■ --dry-run → func(dry_run=True)
  25. 25. Transform CLI Tool Key-Value option style ■ The command string will execute combines If the option value is string or number ■ Example: ■ -t maverick → func(t=maverick) ■ --text hello → func(text=hello)
  26. 26. Transform CLI Tool Key-Value option style II ■ The command string will execute combines If the option value is string or number ■ Example: ■ -t= maverick → func(t=maverick) ■ --text= hello → func(text=hello)
  27. 27. command with sub command ■ Without prefix ■ With prefix ■ git ■ Zenity ■ pbuilder ■ ... ■ apt-get ■ ... apt-get install vim zenity --info=text
  28. 28. UCLTIP - CmdDispatcher ■ For CLI tool has sub command ■ Command name → instance ■ Subcommand name → method name ■ args, options → method args, kwargs ■ Example: ■ apt-get install vim git -t maverick ■ apt-get.install(vim, git, t=maverick)
  29. 29. Use UCLTIP to create a CLI tool Python Binding 「這世界滿是假象, 我行的也是邪道。 」 ~ 道士下山 , 徐皓峰
  30. 30. Python Binding with Native Code(Cython or Ctype)
  31. 31. Python Binding (UCLTIP))
  32. 32. UCLTIP helper ■ ucltip.use_helper ■ Shourcut ■ _c ↔ ucltip.SingeCmd ■ _d ↔ ucltip.CmdDispatcher開發版本才有 , 在 cquery branch ■ cQuery – execute commands like jQuery (Is it useful????) ■ _q(echo "a b c d e").c(awk "{print $3}").read()開發版本才有 , 在 master branch ■ register_singlecmds – create multiple SingleCmd one time
  33. 33. Conclusion of UCLTIP ■ Try to find a convenient way to use CLI tool in Python ■ Can use CLI tool in Python by OO way ■ Can create a work CLI tool Python binding soon ■ Performance may be not good ■ Need to modify codes if CLI tool input/output is changed
  34. 34. UCLTIP Installation ■ user@host# add-apt-repository ppa:ossug-hychen/python- ucltip ■ user@host# apt-get install python-ucltip ■ 要嘗鮮請用 github 上的版本 github.com/hychen/ucltip
  35. 35. Reference 1 ■ UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/ ■ UCLTIP Source Code: http://github.com/hychen/ucltip ■ UCLTIP 0.1 Intro: http://hychen.wuweig.org/?p=748 ■ Video of Create CLI Python binding faster by UCLTIP - http://tinyurl.com/4u4owp5 ■ GitPython: http://gitorious.org/projects/git-python/
  36. 36. Reference 2 ■ VSGUI – Implemeted Zenity binding by UCLTIP 0.1 (may not work now) https://github.com/hychen/vsgui
  37. 37. 本簡報授權聲明 ■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款 ■ 陳信屹 , Hsin-Yi Chen (hychen) ■ Email: ossug.hychen@gmail.com ■ Http://about.me/hychen
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×