I'm a embedded software engineer! I'm now learning English. and i like photography. I also like Programming with Python, and learning the Turbo Gears! for Web Development!

Wednesday, May 02, 2007

The Turbo Gears!

The Turbo Gears!
---- Riverleaf bible
1 The TurboGears!
TurboGears (超速齒輪)是個 python 風格(pythonic) 的網頁應用程式設計開發集成框架. 透過巧妙集成了許多 python 語言下的網頁程式相關開發工具, 提供了網頁程式開發者強大的支援.TurboGears 利用 CherryPy(網頁控制框架), Kid(樣板系統), Mochikit(Javascript 函式庫), SQLObject(資料庫物件模型)這幾個套件為主體, 朝著簡單易用又靈活的方向, 做到快速網頁應用程式開發的功能. 組成 TurboGears 的齒輪們都是開發多年的成熟套件, 在主要功能的穩定性上可以信任, 而且 TurboGears 還可搭配其他的 ORM 系統如SQLAlchemy, 其他的模板系統如 cheetah, Paste, Stan等, 延伸性極高.使用 TurboGears 可以順暢地利用 python 語言, 以簡單, 直覺, 易讀的方式來控制網站程式的運作(Business logic), 產生表單物件, 檢驗表單內容, 並使用視覺化的模板系統來編輯動態網站程式的外觀, 或運用AJAX來製作新一代的網頁應用程式.在多語言支援上, TurboGears 提供網頁介面的 il8n 翻譯協助工具, 讓網站多語言支援變得不再困難; 在牽涉到資料庫的進階設計中, TurboGears 提供 DataController 來達成自動生成對應資料庫欄位的網頁編輯介面 CRUD/scanfold 功能. 提供網頁介面的"資料表設計師(Database Designer)"工具, 可以視覺化地設計資料組織方式; 提供網頁介面的資料表管理工具"(CatWalk)", 管理後端的資料模型。

2 Python
2.1 The Zen of Python
2.1.1 英文原文版
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
2.1.2 python 禪學 (INET6整理版)
任何安裝好的 python 版本中都包含著 python 禪學(哲學),在任何 python 互動介面(如 IDLE)中輸入
>>> import this
則會出現如下的 python 禪學(哲學)
Python 禪學 (pythoon Zen)美觀的比醜陋的好 (Beautiful is better than ugly)直率的比含糊的好 (Explicit is better than implicit)簡單的比繁雜的好 (Simple is better than complex)繁雜的比晦澀的好 (Complex is better than complicated)平直的比巢雜的好 (Flat is better than nested)疏落有致的比櫛次鱗比的好 (Sparse is better than dense)可讀性應當被重視 (Readability counts)就算是特例, 也不能壞了規矩 (Special cases aren't special enough to break the rules)然而能解決問題仍然比維持單純重要 (Although practicality beats purity)錯誤不應該被默默地放過 (Errors should never pass silently)除非你是刻意為之 (Unless explicitly silenced)面對模稜兩可的情況, 要抗拒猜測的誘惑 (In the face of ambiguity, refuse the temptation to guess)應該會有一個--而且最好只有一個--適當的方式來實現(There should be one-- and preferably only one --obvious way to do it)雖然不是這個語言創造者的妳一開始可能看不出來(Although that way may not be obvious at first unless you're Dutch)現在動手比總是不動手的好 (Now is better than never)然而不動手可能還比沒有準備就動手的好(Although never is often better than *right* now)如果實作方式難以解釋, 那大概是個壞主意 (If the implementation is hard to explain, it's a bad idea)如果實作方式易於解釋, 那可能是個好主意 (If the implementation is easy to explain, it may be a good idea)名稱空間是一個很棒的主意 -- 我們就盡量用吧! (Namespaces are one honking great idea -- let's do more of those!)--Tim Peters (gasolin 翻譯)
2007/03/01 更新:參考 http://wiki.python.org.tw/The_Zen_Of_Python 其他人的理解修改
2.1.3 Python 之道 (POT 眾譯版)
Tim Peters 所提出的 PEP 20: The Zen of Python 是撰寫 Python 程式的精神指標。別懷疑,也別覺得太玄,每一句都是金玉良言。
這裡列出 The Zen of Python 的原文版以及本站 POT 眾人合力的翻譯版。如果想知道這中文翻譯版的實作過程,或是想逐條檢討翻譯,請至 The Zen Of Python CHT History 頁面。

美麗優於醜陋,明講好過暗諭。
簡潔者為上,複雜者次之,繁澀者為下。
平舖善於層疊,勻散勝過稠密;以致輕鬆易讀。
特例難免但不可打破原則,務求純淨卻不可不切實際。
斷勿使錯誤靜靜流逝,除非有意如此。
在模擬兩可之間,拒絕猜測的誘惑。
總會有一種明確的寫法,最好也只有一種,
但或須細想方可得。
凡事雖應三思後行,但坐而言不如起而行。
難以解釋的實作方式,必定是壞方法。
容易解釋的實作方式,可能是好主意。
命名空間讚,吾人多實用。

3 The Riverleaf and RiverSMS
RiverLeaf是一个目标,以Python为主要开发语言和开发环境的,起初面向Web开发。

RiverSMS是RiverLeaf的第一个Web开发项目,提供Web界面的基于SMS&Contacts&Calendar的系统。主要采用Turbogears作为开发框架。
4 Database
4.1 ORM
4.1.1 Active Record VS Data Mapping Mode
Active Record ORMs are too limited
– Poor performance
– One class, one table
– Don't play nice with triggers
● SQLAlchemy solves these problems
– But the docs are over 100 pages long (not
including generated API info)
● You probably know SQL already
– If you don't, you're going to need to eventually
4.2 SQLAlchemy
选择SQLAlchemy作为RiverSMS的ORM模块。
D:\Riverleaf-Python2.5package>easy_install -U SqlAlchemy
Searching for SqlAlchemy
Reading http://cheeseshop.python.org/pypi/SqlAlchemy/
Couldn't find index page for 'SqlAlchemy' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading http://cheeseshop.python.org/pypi/
Reading http://cheeseshop.python.org/pypi/SQLAlchemy/0.3.7
Reading http://www.sqlalchemy.org
Best match: SQLAlchemy 0.3.7
Downloading http://cheeseshop.python.org/packages/source/S/SQLAlchemy/SQLAlchemy
-0.3.7.tar.gz#md5=f3314698a270e8fc6a69fe95e98cf34e
Processing SQLAlchemy-0.3.7.tar.gz
Running SQLAlchemy-0.3.7\setup.py -q bdist_egg --dist-dir c:\docume~1\game\local
s~1\temp\easy_install-h9ootl\SQLAlchemy-0.3.7\egg-dist-tmp-ptk5fe
zip_safe flag not set; analyzing archive contents...
Adding sqlalchemy 0.3.7 to easy-install.pth file

Installed d:\python25\lib\site-packages\sqlalchemy-0.3.7-py2.5.egg
Processing dependencies for SqlAlchemy
4.2.1 Alchemy with MS Access?
搜索了Google,都搜不到Alchemy支持MS Access的任何踪迹。看来,我还是需要自己去写一个Alchemy with MS Access了。为什么放着Alchemy + SQLite或其他的现成支持不用呢?其实,还不是为了兼容GsmSMS系统?
4.3 SQLObject
虽然我选择SQLAlchemy作为RiverSMS的ORM模块,但是SQLObject仍然是需要安装的,因为Tg-admin.exe需要。
D:\Riverleaf-Python2.5package>easy_install -U SqlObject
Searching for SqlObject
Reading http://cheeseshop.python.org/pypi/SqlObject/
Couldn't find index page for 'SqlObject' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading http://cheeseshop.python.org/pypi/
Reading http://cheeseshop.python.org/pypi/SQLObject/0.7.6
Reading http://sqlobject.org
Reading http://cheeseshop.python.org/pypi/SQLObject/0.9.0b2
Reading http://sqlobject.org/devel/
Reading http://cheeseshop.python.org/pypi/SQLObject/
Reading http://cheeseshop.python.org/pypi/SQLObject/0.8.3
Reading http://sqlobject.org/
Best match: SQLObject 0.10dev
Downloading http://svn.colorstudy.com/SQLObject/trunk#egg=SQLObject-0.10dev
Doing subversion checkout from http://svn.colorstudy.com/SQLObject/trunk to c:\d
ocume~1\game\locals~1\temp\easy_install-zcdf9n\trunk
Processing trunk
Running setup.py -q bdist_egg --dist-dir c:\docume~1\game\locals~1\temp\easy_ins
tall-zcdf9n\trunk\egg-dist-tmp-qytrvr
warning: no files found matching 'examples\*.py'
warning: no files found matching '*.py' under directory 'tests'
zip_safe flag not set; analyzing archive contents...
sqlobject.classregistry: module references __file__
sqlobject.manager.command: module references __file__
Adding sqlobject 0.10dev-r2652 to easy-install.pth file
Installing sqlobject-admin script to D:\Python25\Scripts

Installed d:\python25\lib\site-packages\sqlobject-0.10dev_r2652-py2.5.egg
Processing dependencies for SqlObject
5 Version Control
5.1 Mercurial
软件的版本控制一定会慢慢的有需要。既然想要一直Pythonize下去,那么,我为什么不选用mercurial(水银)来做我的版本控制系统呢?[1]
5.2 SVN
不过SVN现在很流行,号称是“CVS的下一代”。
SVN被包含在Riverleaf-Python2.5package中。因为easy_install在线安装的时候,需要用到SVN下在某些模块的Dev版本。如:
Best match: SQLObject 0.10dev
Downloading http://svn.colorstudy.com/SQLObject/trunk#egg=SQLObject-0.10dev
Doing subversion checkout from http://svn.colorstudy.com/SQLObject/trunk to c:\docume~1\game\locals~1\temp\easy_install-zcdf9n\trunk

6 更新的技术!!!
6.1 Trac: Integrated SCM and Project Management
Trac is an enhanced wiki and issue tracking system for software development projects. Trac uses a minimalistic approach to web-based software project management. Our mission is to help developers write great software while staying out of the way. Trac should impose as little as possible on a team's established development process and policies.
6.2 Now running on Django trunk
http://seaaboveskybelow.org/weblog/2006/sep/09/now-running-on-django-trunk/
这两天把运行本站的代码升级到了trunk,重写工作大部分是丢弃代码;-} ,用新的特性,代码更简洁和清晰了。这次重构/升级很愉快,django社区往less magic as possible前进的工作进行的很好。随着自己渐渐变成个”老“pythoner,对Zen中的:
Explicit is better than implicit.[2]
There should be one-- and preferably only one --obvious way to do it.
也愈加会心。另外我用contrib中新添加的sitemap生成了本站的 google sitemap , 花了大概两分钟。原来用google的python脚本花了大概20分钟 ;-)
7 Creating Web Site: RiverSMS
7.1
this seems related to your logging configuration.
7.2

[1] 不过暂时没有时间考虑这个问题,所以只是先简单的记一笔。21:00 April 25, 2007
[2] 什么是Explicit?什么是Implicit?之前看到INET6的Blog上写了使用Python+Turbogears写的“书站”的例子,说“都是implicit的,更利于复用”,但这里为什么说“Explicit is better than implicit”?21:31 April 25, 2007.

No comments: