Skip to main content

97 posts tagged with "python"

View All Tags

· 2 min read

身穿冒險服裝,身纏蟒蛇的漫畫少女 (萌) 會讓你聯想到什麼?

荒島求生,畢業旅行,還是純情房東俏房客這些讓人臉紅心跳的日系 AVG 遊戲?

再怎麼想,大概也不會想到我要介紹的卻是 Ren'py 這個免費的 AVG 冒險遊戲設計工具吧 :-D

Ren'py 透過主視窗就可以產生新專案,一個空白的專案就包含了一般 AVG 遊戲常見的選單,記錄,讀出等功能 (嗯嗯... 可惜沒看到看圖功能 XD) 或是將設計好的遊戲發佈成 Windows, Mac, Linux 等各平台的套件,非常方便將自己製作的遊戲與其他人分享.

Ren'py 的文件還不錯 (甚至還有互動遊戲版說明 :-D), 腳本的寫作與編譯也相當方便. 而且這個工具支援中文 (另外抓螢火飛的中文字型即可). 需要進階功能的話,可以使用 Python 語言撰寫加強程式,擴展性極佳:-D 如果想要設計一套 AVG 遊戲,Ren'py 實在是相當棒的選擇.

PS: 看看可愛的蟒蛇還咬著遊戲手把,有些人可能已經猜到,這個工具是基於 pygame 來開發的.

· One min read

拿到一個現成的資料庫,最快速的就是直接存取啦.

TurboGears 中除了預先定義 schema 外,也可以直接使用 SQLAlchemy 中的 SqlSoup 模組來直接存取資料庫.

之前我有寫過英文版的,今天 trace 一下 sqlsoup 後發現還有更簡單的方法:直接取用 metadata.

1. 修改 dev.cfg 中的 sqlalchemy.dburi , 指到對應的資料庫

2. 在 your project/model.py 中,寫下

from turbogears.database import metadata from sqlalchemy.ext.sqlsoup import SqlSoup

soup = SqlSoup(metadata)

3. 使用 shell 存取資料

$ tg-admin shell

poll = soup.poll.select() print poll

· 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 相比,可以看出它好用的地方.