Pyramid
-
Upload
atsushi-odagiri -
Category
Technology
-
view
2.638 -
download
0
description
Transcript of Pyramid
PyramidPython hack-a-thon 2010.11
aodag
自己紹介
小田切篤@aodag
株式会社ビープラウドに勤めてます。ちょっと前まで名古屋にいました
http://aodag.posterous.com/http://blog.aodag.jp/http://facebook.com/aodag
Pyramid
Webアプリケーションフレームワーク
元は、repoze.bfg2010/11/5 repozeプロジェクトからpylonsプロジェクトに移動このときにPyramidに名称変更
Pyramidの最新バージョン 1.0a3repoze.bfgの最終バージョン1.3
インストール
$ virtualenv \ --no-site-packages \ --distribute \ newproject$ cd newproject$ bin/pip install pyramid
お約束のHello, Worldfrom paste.httpserver import serve from pyramid.configuration import Configurator from pyramid.response import Response
def hello_world(context, request): return Response('Hello world!')
if __name__ == '__main__': config = Configurator() config.begin() config.add_view(hello_world) config.end() app = config.make_wsgi_app() serve(app, host='0.0.0.0')
ちょっと追加
def details_view(context, request): return Reponse('THis is detail')
.....config = Configure(root_factory=get_root)config.add_view(details_view, name='details').....
簡単な流れ
/hello_world(root_object, request)
/hoge/foo/bar
hello_world(root_object['hoge']['foo']['bar'], request)
/hoge/foo/bar/detailsdetails_view(root_object['hoge']['foo']['bar'],request)
モデル定義
from persistent import Persistent
class Document(Persistent): def __init__(self, title, contents): self.title = title self.contents = contents
データ追加
@view_config(name="add_document"):def add_document(context, request):
params = request.params doc = Document(title=params['title'], contents=params['contents']) context[name] = doc return HTTPFound(location=model_url(doc, request))
モデル表示
@view_config(for_=Document, renderer="template/document.pt")def document_view(context, request): return dict(title=context.title, contents=context.contents)
テンプレート
<html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <h1>${title}</h1> <p>${contents}</p> </body></html>
テスト(1)
def setup(): global config, context config = Configure() config.begin() context = Folder()
def teardown(): config.end()
テスト(2)
@nose.with_setup(setup, teardown)def test_hello(): import newproject.views.add_document req = DummyRequest() req.params = {"name":"first-document", "title":u"テスト", "contents":u"内容"} info = add_document(context, req.params)
assert info.location == 'http://example.com/first-document/' assert context['first-document'] is not None doc = context['first-document'] assert doc.title == u'テスト'
テストを走らせる
noseを使う
$ bin/pip install nose$ bin/noserunner newproject
情報源
http://docs.pylonshq.com/http://pypi.python.org/pypi/pyramid/http://docs.repoze.org/bfg/
コミュニティ
日本のコミュニティ
Pylons-ja ちょっと開店休業状態
Google Grouphttp://groups.google.com/group/pylons-ja
Facebook Grouphttp://www.facebook.com/home.php?sk=group_157480254294440
ご清聴ありがとうございました