Pyramid

17
Pyramid Python hack-a-thon 2010.11 aodag

description

 

Transcript of Pyramid

Page 1: Pyramid

PyramidPython hack-a-thon 2010.11

aodag

Page 2: Pyramid

自己紹介

小田切篤@aodag

株式会社ビープラウドに勤めてます。ちょっと前まで名古屋にいました

http://aodag.posterous.com/http://blog.aodag.jp/http://facebook.com/aodag

Page 3: Pyramid

Pyramid

Webアプリケーションフレームワーク

元は、repoze.bfg2010/11/5 repozeプロジェクトからpylonsプロジェクトに移動このときにPyramidに名称変更

Pyramidの最新バージョン 1.0a3repoze.bfgの最終バージョン1.3

Page 4: Pyramid

インストール

$ virtualenv \ --no-site-packages \ --distribute \ newproject$ cd newproject$ bin/pip install pyramid

Page 5: 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')

Page 6: Pyramid

ちょっと追加

def details_view(context, request): return Reponse('THis is detail')

.....config = Configure(root_factory=get_root)config.add_view(details_view, name='details').....

Page 7: Pyramid

簡単な流れ

/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)

Page 8: Pyramid

モデル定義

from persistent import Persistent

class Document(Persistent): def __init__(self, title, contents): self.title = title self.contents = contents

Page 9: Pyramid

データ追加

@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))

Page 10: Pyramid

モデル表示

@view_config(for_=Document, renderer="template/document.pt")def document_view(context, request): return dict(title=context.title, contents=context.contents)

Page 11: Pyramid

テンプレート

<html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <h1>${title}</h1> <p>${contents}</p> </body></html>

Page 12: Pyramid

テスト(1)

def setup(): global config, context config = Configure() config.begin() context = Folder()

def teardown(): config.end()

Page 13: Pyramid

テスト(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'テスト'

Page 14: Pyramid

テストを走らせる

noseを使う

$ bin/pip install nose$ bin/noserunner newproject

Page 15: Pyramid

情報源

http://docs.pylonshq.com/http://pypi.python.org/pypi/pyramid/http://docs.repoze.org/bfg/

Page 16: Pyramid

コミュニティ

日本のコミュニティ

Pylons-ja ちょっと開店休業状態

Google Grouphttp://groups.google.com/group/pylons-ja

Facebook Grouphttp://www.facebook.com/home.php?sk=group_157480254294440

Page 17: Pyramid

ご清聴ありがとうございました