Skip to main content

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 的中間形式.

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

Making a Time Tracker in TurboGears

· 2 min read

2006/30/7 Since may people may come from 42, I suggest you to download the full instruction at TurboGears Trac, you'll see full instruction to make it.

This article is motivate by the framework comparison screencast performed by Sean Kelly. I found this screencast from TurboGears group. the screencast compare J2EE , Rails ,Zope ,TurboGears ,and Django. TurboGears did fairly well. but Sean Kelly wasn't using the latest version(~0.9) during his tests.Thus I try to re-imlement the demo by using bunch of exciting new features (Model Designer, CatWalk, DataController...) introduced in the latest TurboGears version.

Origin version in ScreenCast

The screencast implement page like this, there is a add only interface, no editing.

And it cost 2 python source files 49 lines of code, 2 page of templates, 53 lines of code, not a hundred lines of code.

Re-Implement version with latest svn(#885)

Its time catch up with Sean Kelly's demo in latest TurboGears version. The re-implement with latest Turbogears svn like this. TurboGears > 0.9 provide autogenerate Editor(Catwalk) in Admin Panel(Toolbox)

We can get another running mode editor(much usefull for real app) with DataController by several lines of code.

the re-implement version cost 2 python source files (now Turbogears config file is in python) 6 lines of code, 0 page of templates, not a ten lines of code. The re-implement version also include a full-functional database-relation editor.

Turbogears automatically generate pages for you both in admin and practice perspective. In Turbogears you can customize DataController and other widgets' looking and feel to fit your layout style.

用 TurboGears 做個網站時間追蹤應用程式

· One min read

根據 Sean Kelly 的 framework comparison screencast 的影片演示, 他比較了 J2EE, Rails, Zope, TurboGears, and Django 幾個框架. TurboGears 在其中得到了不錯的評價,然而他所使用的 TurboGears 版本還沒引入網頁自動生成資料庫編輯介面 (CRUD) 等 0.9 版新加入,可以大大減少初期開發時間的功能. 因此我利用他的 Time Tracker 例子和資料模型寫了篇用上 TurboGears 0.9 新功能的教學文章 (英文), 貼在 TurboGears Trac 裡,可以前去下載.

QuickPrototyping: Making a Time Tracker in TurboGears http://trac.turbogears.org/turbogears/wiki/QuickPrototyping

深入 TurboGears - 驗證並轉換表單參數 (Validators) 講解草稿

· 2 min read

在 TurboGears 中可以用 "@validate ()" 裝飾方法來處理輸入表單參數的轉值與驗證。使用的好處是除了會自動確認型態的正確性之外,還會將字串轉換成目標型態.

例如拿前一個網頁加法器作例子,原來例子中加總結果必須先將字串轉換成目標型態,因此計算式如下 SUM = (int (A)+int (B)) 我們可以使用 "@validate ()" 來預先先確認輸入資料,並將資料轉換成預期的資料型態. 範例如下:

from turbogears.validators import *
@expose(inputform ="add")
@validate(validators=dict(A=validators.Int(),B=validators.Int()))
def calcit(self,A=0,B=0, SUM=0):
template = """
<form name="add" method="post" action="/calcit">
<input name="A" type="text" value="%d"/> + <input name="B" type="text" value="%d"/>
<input type="submit" value="=" /> <input name="SUM" type="text" value="%d"/>
</form>
"""
SUM = A + B
return template%(A,B,SUM)

@validate(validators=dict(A=Int(),B=Int())) @validate(validators={"A":Int()}, "B":Int())

現在例子中由於已經預先將字串轉換成目標型態,因此可以直接加總 SUM = A + B

這讓程式碼可讀性更進一步增加了.

總結

validate ()" 方法除了可以幫忙我們判斷回傳值型態外,同時也會將回傳值從傳回的字串型態轉換成指定型態供我們處理

除了這些簡單值的字串轉換外,在 TurboGears 中做 email 地址確認時,甚至還能選擇確認 email 主機是否真正存在喔;)