Skip to main content

· 2 min read

Purpose

Have an exist database and want migrate to TurboGears? Or you've been using TurboGears SQLObject model and want to migrate to SQLAlchemy?

Solution

TurboGears newly support SQLAlchemy 0.2 module(>0.9a6), it provide a slick solution: SqlSoup !

What SqlSoup does?

SqlSoup inspects your database and reflects its contents to class, no model definitions are necessary!

What does that mean?

It means you only need to specify a uri for database, and call SqlSoup to do the rest.

What's the Benifits?

SqlSoup maps database tables to classes automatically. I'm not sure how efficient it is, but at least you can use SqlSoup by following ways:

  • To use existing Database design tools to design my database
  • To use SqlSoup in design phase, and code to SQLAlchemy class when the databse is settled down.
  • To painless migrate SQLObject generated database to SQLAlchemyTest Code

Try the test code, it is thin.

in model.py

1 from sqlalchemy.ext.sqlsoup import SqlSoup

2 uri = "sqlite://%(current_dir_uri)s/devdata.sqlite" 3 soup = SqlSoup(uri)

Explain

line 1 import SqlSoup from sqlalchemy module,

line 2 specify the uri link to the database (I believe it can be improved)

line 3 the real code calling SqlSoup connect to uri

line 4 and line 5 shows I call the table "poll" from database

if you can't execute is , maybe you should install sqlalchemy first

$ easy_install sqlalchemy

or try the TurboGears way:

1 from turbogears import config 2 from sqlalchemy.ext.sqlsoup import SqlSoup

3 uri = config.get('sqlalchemy.dburi') 4 soup = SqlSoup(uri) All you need to do is specify the dburi for sqlsoup.

Let's test what we got

poll = soup.poll.select() print poll

Reference

That's all you need in TurboGears. To get further usage , you can refer to Introducing SqlSoup

ps: Actually TurboGears has done nothing particularly to support SqlSoup, I just use it as other modules.

· One min read

剛看到一個 Python FAQ 系統. 當然這並不是最好的 FAQ 系統,不過裡面除了一般的分類索引之外,還有個非常有趣的功能是 "Show me a random article! (隨機文章)", 這功能有點像一些 IDE 剛開啟的時候出現隨機的 FAQ 視窗,每次開啟時都有不同的訊息.

· 2 min read

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

>
> from turboentity import *
> from docutils.core import publish_parts
>
> class Post(Entity):
> class turboentity:
> tablename = "posts"
>
> title = Column(Unicode(50))
> content = Column(Unicode)
> post_date = Column(DateTime, default=datetime.now())
> is_published = Column(Boolean, default=False)
> comments = OneToMany('Comment',backref='posts')
>
> @property
> def html_content(self):
> return publish_parts(self.content,writer_name="html")["html_body"]
>
> class Comment(Entity):
> class turboentity:
> tablename = "comments"
>
> author_name = Column(Unicode(255), nullable=False)
> author_email = Column(Unicode(255), nullable=False)
> author_url = Column(String(255))
> comment_date = Column(DateTime, default=datetime.now())
> content = Column(Unicode)
> post = ManyToOne("Post",backref='comments')
>

要表示 post-comments 關係只需分別在兩個類別中宣告 OneToMany - ManyToOne 即可,真是方便呀.

另外當定義好 TurboEntity 類別後,物件自動可以用傳值的方式輸入內容,不需一個個各別指定喔.

例如一般的寫法是這樣子:

p = Post.get (1) # 取得條目 c = Comment () # 新建一條評論 c.post = p # 這條評論屬於 p 條目 c.author_name = "CommentDude1" # 這條目的作者是 c.author_email = "[email protected]" # 這條目作者的郵件信箱是 c.content = "Great post! Keep them coming!" # 這條目的內容 c.flush () 可以直接改成 p = Post.get (1) c = Comment (post=post, author_name="Bob", author_email="[email protected]", content="Bob loves this site.", author_url="http://bob.example.com/") c.flush()

TurboEntity改寫的程式和原本 Tutorial 相比,可以看出它好用的地方.

· 4 min read

明天公司補放國父誕辰紀念日,所以有空到 Google 翻翻最近有沒什麼有趣的文章。我找到了一篇 TurboGears vs Rails. 文章名稱夠聳動,雖然這篇文章有點舊了 (用的是 0.9 的預覽版 TurboGears, 或稱為未進化型態版本 XD), 不過裡面對 TurboGears 和 Rails 的特性確實抓得頗準:

The Pythonic way is "explicit over implicit". Everything is out for show: you know what modules are imported, you know what methods are exposed, you know what columns are defined and so on. It may take more keystrokes but the extra code let's you know what is happening when things go wrong. python 語言的風格是 '直率的比含糊的好'. 所以所有的過程都可以被檢視:妳曉得如何導入使用的模組,函式怎麼對應到網址上,妳也曉得資料物件如何定義等等。妳可能需要多打一些字 (註:事實上不多), 但是這些額外的程式碼能讓妳在發生錯誤時更容易地知道自己的程式到底發生了什麼事.

The the Rails way is the opposite: take the burden off the developer, don't bother them with the petty details that get in the way and add to the line noise. Rails 的寫作方式則相反:把讓開發者困擾的因子都去除掉,不要在開發中用細節來干擾程式碼.

事實上去年 5 月底時,我在看過 OnLamp 網站上的 Ruby On Rails 教學後,相當驚訝現在網頁開發的進步 (之前有一年沒寫動態網頁了). 所以我也趁等待完成論文前的時間歔空寫了篇 Ruby On Rails 教學。不過在 7 , 8 月 Django, TurboGears 這些 Python 框架相繼出現後,我發現除了可以使用熟悉的 Python 語言風格來寫程式之外,以後也可以透過網頁介面來使用大量的 Python 模組實在非常吸引我。而當 TurboGears 框架出現 ToolBox 這神奇的工具箱後,我開始漸漸投入了 TurboGears 開發的行列.

能展現自我特點的是創意而不是程式碼 在我的觀念裡,能展現自我特點的是創意而不是程式碼 (也許因為我不是個天才程式設計師吧 XD). Rails 也是個相當吸引人的框架 (差不多靠一己之力拉拔 Ruby 語言 XD). 但是 python 的 "應該會有一個 -- 最適當的一個的方式來實現" 哲學比較接近我的想法。在閱讀其他人的 TurboGears 專案時只要不用到太進階的 Python 語言功能,基本上都非常易於閱讀與修改. (例如目前 TurboGears 最進階的教學文件: SQLalchemy 版的 SimpleBlog, 花一些時間就可以改寫成 TurboEntity 版 SimpleBlog 之一, 之二)