Skip to main content

巫毒編程 (voodoo programming)

· One min read

有兩種設計軟體的方式,一種設計方式是將軟體設計得很簡單,因此很明顯地沒有缺點; 另一種設計方式是將軟體設計得很複雜,因此看不出明顯的缺點. ——C.A.R. Hoare

今天到書局買了本簡體的程序員雜誌,裡面提到了很有趣的 "巫毒編程 (voodoo programming)" 方法.

"巫毒編程 (voodoo programming)" 說的是一種很糟糕的寫程式方法.

感覺 "巫毒編程" 的形容實在是蠻形象的,想想當寫程式時要是遇到程式一直跑不出來又找不到原因,心情超煩悶的時候打鍵盤是不是就痛苦地像在刻咒怨一樣?

如果這時試著像對草人扎針一樣隨意改些程式的變數,按下編譯 (Compile) 然後期望程式可以正常跑 (詛咒成功). 嘿嘿嘿,是不是很有巫毒儀式的感覺呢?

update from April/16/2006

預定了 TurboGears 書

· 2 min read

今天到天瓏書局預定了 11/9 號出版的 "Rapid Web Applications with TurboGears".

其實這本書早在 9 月多時主要作者 Mark Ramm 透過封閉的 TurboGearsBook group 提供各章節草稿時我就看過了.

參與 TurboGearsBook Group 的要求就是在享受預覽草稿的權利同時也要扮演 Reviewer/Editor 的角色提出意見.

在書籍不是 OpenSource 的情況下,這種兼顧社群與提昇質量的方式還蠻值得參考的.

當時內容還不太完整,不過已經勾勒出大部分的輪廓了。由於 TurboGears 是個整合許多 Python 模組的框架,因此裡面除了框架本身內容,實際範例外,還專章提到了諸如 TurboGear 如何的使用 CherryPy, SQLObject, SQLAlchemy, Formencode 等模組.

另外測試的部份除了講到 nosetests 與 TurboGears 為方便 Model, Controller, Viewer 各層測試而加入的 testutils 測試函式庫外,還提到 Selenium, Mechanize 等使用者經驗測試部份. 算是我相當期待的一本書.

TurboEntity 版 SimpleBlog

· 2 min read

這幾天 TurboEntity 這個類似 ActiveMapper 的模組一出現馬上獲得不小的關注. 因為它可以用幾乎與 SQLObject 數量相當的程式碼來定義 ORM, 又能完全繼承 SQLAlchemy 的效能和彈性.

Lee McFadden 從上上週開始在他的 Blog 上連載了 3 篇 'SimpleBlog' 系列文章,引導如何使用 SQLAlchemy + TurboGears 製作簡單的 Blog 系統. 他採用了接近基本 SQLAlchemy 的設定方式,因此程式碼有點多.

我用 TurboEntity 照著 'SimpleBlog Part I' 中的範例改寫後,獲得以下程式碼 (model.py):

>
> from turboentity import *
> from docutils.core import publish_parts
>
> class Post(Entity):
> title = Column(Unicode(50))
> content = Column(Unicode)
> post_date = Column(DateTime, default=datetime.now())
> is_published = Column(Boolean, default=False)
>
> @property
> def html_content(self):
> return publish_parts(self.content,writer_name="html")["html_body"]
>

在其它部份保持不變的情形下,拿來跟原文比較可以發現使用 TurboEntity 寫的定義 ORM 程式,就程式碼行數上實在不輸 SQLObject. 難能可貴地是同樣能保持很高的可讀性.

TurboEntity 網站上還有使用 TurboEntity 版本做 TurboGears Identity Model 的例子,看來轉換到從 SQLObject 轉換到 SQLAlchemy 已經不再是太令人躊躇的事了.

TurboGears in Minutes - Getting new project Start

· 2 min read

I decide to start a new track of TurboGears Tutorial (In English), which is light weight enough so it will be easy to follow.

Why not start this tutorial on TurboGears Documentation Site ?

It just because I feel more comfortable with blogger's editor, and I'll feel less guilty if I mess these tutorial up :-D

Oops, 5 seconds passed, let's go back to the topic.

Start a new project

To start a new project (new site) with TurboGears, you'd use "tg-admin quickstart" command for it. The syntax is:

$ tg-admin quickstart [projectname] Let's create a "TurboHello" (sound's powerful, isn't it? :-D) project for example:

$ tg-admin quickstart TurboHello The console will prompt some choices. Feel free to press "enter" button (let defaults) and proceed to the next step.

Start the web server

The 'TurboHello' folder was created by your console. After the project was created, enter the 'TurboHello' folder, and you could start the TurboGears development web server with following command:

$ ./start-TurboHello.py

The web server is running, browse http://localhost:8080/ to see the project welcome page:

Pretty and easy, right?

The project structure

While entering the 'TurboHello' folder, you'll see the project skeleton laid there. Those files can be categorized for 4 purposes:

The document was merged to http://docs.turbogears.org/1.0/GettingStarted/BigPicture

TurboGears followed MVC design pattern (search if you don't know that), which seperate the web application design into three different roles, to build the default quickstart project. TurboGears also offer you the flexibility to 'not use the MVC' as well (will be shown in next minutes).

We'd always focus on part 4: "the Development part" in TurboGears.