Skip to main content

TurboGears 的測試框架

· One min read

TurboGears 裡集成了 nose 作為測試工具. nose 支持 unitest /doctest, 可以對資料庫處理 (Model), 生成的頁面 (View), cherrypy 輸出結果 (Controller) 作測試.

搭配 selenium4gears 直接測試網頁介面的能力,讓 TurboGears 擁有了完整的測試方法.

TurboGears 可以直接集成成熟的現成 python 套件,這是使用 TurboGears 的好處.

TurboGears 支持各種 Ajax 套件

· One min read

網路上許多 Ajax 套件,例如 Dojo, Rico, 提供了比 TurboGears 預設的 mochikit 更多視覺上或是功能上的便利.

要讓 TurboGears 框架支援更多這些 Ajax 函式庫超容易。只要將解壓縮後的函式庫放到 Turbogears/static/js 資料夾下。然後在要使用時在 html 中以 script type="text/javascript" src = "/tg_js/script.js" 路徑加入即可

例如要用 Dojo 的 WYSIWYG 網頁編輯器功能,

先在 html body 中加入

div class="dojo-Editor" some content /div

然後再在 title 中加入 dojo 連結

script type="text/javascript" src="/tg_js/dojo/dojo.js

並呼叫載入要使用的 dojo-Editor 功能:

script type="text/javascript" dojo.require("dojo.widget.Editor"); /script

從分享書籤到分享內容- unAPI

· 2 min read

現在不少 web 服務 (amazon, del.icio.us, flickr) 都提供有 API 或 RSS, 共通之處是這些 web 服務都使用了 XML 標記語言,解析起來類似. 但是問題來了,這些種類 web service 的標籤格式雖然都遵照 XML 標記語言規範了, 但各 web service 的格式還是都稍有不同,programmer 要使用這些 web 服務的時候,每要支援一個服務就得重寫一個 Adapter.

unAPI是一個新提出來的抽象 API, 希望透過統一介面操作這些 API, 讓 programmer 得以更輕鬆用同一個介面做出能線上分享書籍介紹,參照圖片,剪貼網頁的平台.(拿 windows 軟體來比喻的話就像是 Microsoft OneNote 那樣的功能) programmer 開發的速度加快了,使用者也更容易得到更好的體驗 (或是更奇怪的體驗?).

雖然之前已經有不少 web 2.0 網站打著可以將許多 web service 或是將 feed, blog post "燒" 在一起來用來看, 但是被 "綁" 在特定平台上的感覺總是不夠自在. 因此我感覺 unAPI 相當具有成功的機會.

unAPI 離我們有點遠,覺得太抽象?還有screencast可以參考喔.

What straws make you choose TurboGears?

· 2 min read

I wonder to know the reason why people here choose TurboGears? I'd like to share mine.

1. Python Battery Included With TurboGears I can apply >5000 python modules on my web application natually. "Battery Included" facilitate me focusing on my major job to getting things done.

ex: Wiki20: "docutils" module is helpful for wiki syntax TurboBlog: PIL "ImageFonts" module is helpful for blog anti-spam stuff.

2. Flexibility from Prototpying to Merchandising In small app or prototyping stage, I just want to see something work first. TurboGears give me the flexibility to do most of my web programming by only edit controller.py. I can adopt Template, ORM, AJAX.... latter if I really need them. I don't want to take care of "framework" stuff such as admin controll, templating, url mapping, or database reference at first (which Django did).

ex: I've written some short tutorials to teach myself turbogears' features without template. (in chinese). http://gasolinfred.pbwiki.com/%5BTurboGearsInMinutes

3. Easy to start, Plenty for Learn Model Designer , CatWalk, help me leave from the first headache of database backend web programming. http://trac.turbogears.org/turbogears/wiki/QuickPrototyping

And after learning how "controllers" and "expose" works (I think its the minimum set of turbogears core), I can construct a workable web app and enhance it during my learning curve.

Hypy, hypertext in python

· One min read

今天剛寫完 "Making a Time Tracker in TurboGears", 重新來看 Hypy 這個小樣板程式.

在 TurboGears 中可以這樣使用 hypy

0  from turbogears import controllers
0 from turbogears import expose
0 import hypy
1 class Root(controllers.RootController):
2 @expose()
3 def hypyd(self):
4 template="""
5 @html:
7 @head:
8 @title:
9 ${title}
10 @body:
11 Hello TurboGears!
12 """
13 context = (title = "Hello hypy")
14 web = hypy.parse(template,context)
15 return web

原本 template 字串樣版用 html 寫的話簡直就是糊在一起,但是改用 Hypy 寫之後整體就清楚多了 Hypy 的語法跟 kid 的基本語法接近。又能乾淨地在程式碼中內嵌網頁,很適合當作一個取代字串樣版介於 controller 與 template 的中間形式.

只是這樣是否就又要使用者多學一種語言,反而增加學習難度?這就是相當見仁見智的事了.