Python作為一種腳本語言,作為Perl的對手,在操作文件系統上面很有一套, 由於語言的推廣,在web方面也出現了很多Python的框架,最有名的莫過於Django了,其實不太熟悉Django,但是近些年來Python在web方面沒有太多的進展,反而back end javascript,例如nodejs的崛起,帶動了javascript作為一個后端語言風潮,和以前的PHP,以及基於Java的J2ee, SpringMVC(曾經占有40%的web框架)競爭,個人覺得Python並不占優勢。
Python需要找到自己的發展方向,無疑Shellscript作為linux娘胎里帶來的語言,其蹩腳性顯而易見,而Perl is ugly, everyone knows。所以python可以在Command line這條路上多走一些。
Prerequisite, I'm newbie in Python, so I just to share my learning python feelings. So Let's start.
Python and Pip
我是用maven用習慣了,所以沒有build工具,以及作為3rd party包查找工具,我肯定要找一個,python肯定要提供一個,我第一步找到的是Pip,easy_install好像也可以但是重復了,我就熟悉了pip就夠用了。pip的安裝比較簡單,執行python的一個遠程get.pip.py的腳本就可以(這個方式顯得有些不專業不得不說,而且沒有和linux apt-get, Mac brew等等結合,顯得有些low).而且,pip在開發中也不是必須的,這點讓人有些沮喪,說明在開發大型項目的時候,python的標准化,還沒有十分完備(個人意見)。


Run pip list will get you have already install python packages. sys.path you'll get the directory which python 3rd party packages location.
說起來,npm和bower這種安裝工具提供本地安裝,但是pip沒有,是讓人沮喪的,npm安裝默認是本地,如果全局要加-g。
npm install -g bower
Python command line framework
說到今天的主題了,一直以來,我需要一個交互式的命令行程序,動態的申請虛擬機,刪除虛擬機,安裝一些軟件等等,我希望都通過命令行來實現,而不是通過制作一個web端(以前,我一直是怎么做的)。所以我找到了python,找到了一些不錯的,激動人心的python框架。
http://docs.python-guide.org/en/latest/scenarios/cli/
這篇文章中提到了幾個。
Clint
沒有好的文檔,確實,這點比較致命,而且沒法知道框架的全貌,不過幾個功能還是不錯的。https://github.com/kennethreitz/clint Github地址,因為是開發中,所以不完善是必須的,可以理解了。
- 可以打印color,有前置的提示符
from clint.textui import puts, indent, colored
puts(colored.red('this is a text'))
with indent(4,">>> "):
puts(colored.yellow("hello?"))
puts(colored.green('this is the end'))

- 處理輸入參數
from clint import arguments
args = arguments.Args()
print args.get(0)
Run python test.py 123 will print 123.
- 處理輸入流
from clint import piped_in
if __name__ == '__main__':
in_data = piped_in()
print in_data
Run python test.py < 1.txt will print 1.txt content.
- 進度條打印
from time import sleep
from random import random
from clint.textui import progress
for i in progress.bar(range(100)):
sleep(random()*0.2)

- 提示框
from clint.textui import prompt,puts,colored,validators
name = prompt.query("What's your name?")
puts(name)
language = prompt.query("Your favorite tool (optional)?", validators=[])
puts(language)
Click
http://click.pocoo.org/3/ 更成熟的工具,研究一些高級功能。
格式化打印?
循環的詢問?
……
