Slide 1

Slide 1 text

Use Pyramid Like a Pro Keith Yang @keitheis

Slide 2

Slide 2 text

@keitheis Keith Yang • architect.py in • Organizer/Coordinator in • Web Lead in PyCon Taiwan 2013 2

Slide 3

Slide 3 text

Some fun fact • New EC Startup  • Re-built on Nov.2012, Apr. 2013 online • Agile style  • Powered by Pyramid 3

Slide 4

Slide 4 text

Taipei.py • Monthly Meetup • to Weekly Meetup Taipei.py 的奇幻漂流 4

Slide 5

Slide 5 text

Core

Slide 6

Slide 6 text

Choice  Like Human Being

Slide 7

Slide 7 text

Being What? Alien?  1. Know your future 2. Know your scale 3. Know your tool sets 4. Know your team, goal, and time frames from future import everything 7

Slide 8

Slide 8 text

Choose What?  1. Web Framework(s) 2. SQL NoSQL ORM Query Helper ALL REDIS? 3. Message Queues and/or Background Workers 4. Template Session API? RESTful RESTlike Too many to choose but usually or eventually you have to 8

Slide 9

Slide 9 text

Architecture, Architecture, Architecture.

Slide 10

Slide 10 text

That's why Flexibility Counts

Slide 11

Slide 11 text

The Power of Pyramid (Pylons Project) 1. Flexibility: from minimal to full stack seriously ready 2. Views conQg & varies routes 3. Add-ons and friends 4. Reliability, Agilibility, Simplicity and Community 5. Fun to learn 11

Slide 12

Slide 12 text

Python 3 Ready from Pyramid 1.3

Slide 13

Slide 13 text

What Pyramid is NOT Not the Qrst web framework you should learn together with your Qrst programming language (e.g. Python). 13

Slide 14

Slide 14 text

Rumors “Use NoSQL and you will be good with schema free.” Imagine a type free programming language, which int(“0”) + str(1) will be so Qne. 14

Slide 15

Slide 15 text

Rumors (cont.) “Why you should use this web framework” Said, Django, Flask, Web2py, Bottle, Rails, ... all the excellent choices. Also known as「不要欺騙小孩子」  課本上有寫「政黨體質的健全、民主、清廉與執政能力關係人民的幸福」 15

Slide 16

Slide 16 text

 不解釋。 I say: “Just use Python”

Slide 17

Slide 17 text

Fundamental Pyramid School • Understand Pyramid Request • Separated logic of views, data and business logics For any MVC-like Framework M or C is not for business logic 17

Slide 18

Slide 18 text

Fundamental Pyramid School (cont.) • Super powerful Views • Flexible Routes • Context driven 18

Slide 19

Slide 19 text

Database: to SQL • Postgres, professional or enthusiastic • MariaDB, new MySQL candidates, or lazy at Qrst • SQLite for development, personal app, or no production-level needed 19

Slide 20

Slide 20 text

Database: to NoSQL • To name a few: • MongoDB, Riak, Cassandra, Redis, Hadoop, ... • When you arrived there you'll learn these class. Before that, just keep calm on them. 20

Slide 21

Slide 21 text

Database: None Perfect No default binding ORM, no bother. 21

Slide 22

Slide 22 text

Have you tried Taiwan Mango? To ORM • SQLAlchemy ORM • Read What is Pony ORM?¶ • Django ORM (not really a choice here) • Peewee Django ORM like • MongoEngine, MongoKit, Ming, ... 22

Slide 23

Slide 23 text

Not to ORM • SQLAlchemy Core • MoSQL v0.2 by mosky, also on (yesterday v0.1.6) • Pure SQL Dialects: hard code driver Query (sqlite3, psycopg2, mysql-python, ...) • Pure NoSQL Dialects 23

Slide 24

Slide 24 text

Templates: Mako or Jinja2 • Mako Templates - pythonic, or not, depending on you with its nature by Mike Bayer (author of Alembic) • Jinja2 by Armin Ronacher, also Flask author 1. Elegant 2. Strict, more or less, magic 24

Slide 25

Slide 25 text

Templates (cont.) - Plim 1. by Maxim Avanov  2. Ported from Slim template 3. Clean HTML usage 4. Built on top of Mako Templates 5. Fun and half hell on debug mode Guess what? You'll learn how to guess by using it  25

Slide 26

Slide 26 text

Templates (cont.) - Plim -for row in ['apple', 'banana', 'pineapple'] tr -for col in ['juice', 'muffin', 'pie'] td ${row.capitalize()} ${col} That's all. No . 01. 02. 03. 04. 26

Slide 27

Slide 27 text

Templates (cont.) - Chameleon ZPT, Kid or Genshi lovers should give it a look ${row.capitalize()} ${col} 01. 02. 03. 04. 05. 27

Slide 28

Slide 28 text

Too many to introduce (again) • Celery, Python RQ • RebbitMQ, ZeroMQ • Redis, memcached • Beaker, retools (both by Ben Bangert , Pylons 1 author) 28

Slide 29

Slide 29 text

Oh, Sentry • by David Cramer (our Keynote speaker) • Awesome error events handle • Host by yourself or getsentry.com 29

Slide 30

Slide 30 text

My better practices • Separate the (DB) models and behaviors • Reproduce-able development database (Fixtures) • Development mode optimization • Plim template Double-edged swords  use them carefully. 30

Slide 31

Slide 31 text

My better practices (cont.) • Explicit architecture (lib, tasks, schemas ... folders) with explicit nameing • Focus on a utc module handles datetime & timezone • Fundamental doc: README and doc/**/*.rst • Avoid mess sys or deploy stuff in code repository if possible 31

Slide 32

Slide 32 text

Minimal Viable Code 

Slide 33

Slide 33 text

My worser lesson 1 def ugly_customized_routes(config, resource_route, path=None, actions="ICRUD", resource_name=None): ... Not aesthetic route helper 01. 02. 03. 04. 05. 33

Slide 34

Slide 34 text

My worser lesson 2 print("This is too embarrassing to print") Lacking CI tests  34

Slide 35

Slide 35 text

Debug mode if debug_notfound_route: from .views.notfound import ( NotFoundDebugView as NotFoundView ) else: from .views.notfound import NotFoundView config.add_notfound_view(NotFoundView) 01. 02. 03. 04. 05. 06. 07. 35

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

SQL Master 37

Slide 38

Slide 38 text

Logging in the @ow 38

Slide 39

Slide 39 text

Debug Debugtoolbar # Disable development performance killer debugtoolbar.panels = pyramid_debugtoolbar.panels.sqla.SQLADebugPanel ... #pyramid_debugtoolbar.panels.introspection.IntrospectionD #pyramid_debugtoolbar.panels.tweens.TweensDebugPanel #pyramid_debugtoolbar.panels.routes.RoutesDebugPanel 01. 02. 03. 04. 05. 06. 07. 39

Slide 40

Slide 40 text

Before your ?rst release   Practice • pshell (the force on production, use it carefully) • Play Command-Line Pyramid • Alembic Database Migration scripts (if using SQLAlchemy) 40

Slide 41

Slide 41 text

Save  Your Self In Time

Slide 42

Slide 42 text

Feel the power of Alembic • by Mike Bayer (author of SQLAlchemy) • Use op and sa instead your deQned Models from alembic import op import sqlalchemy as sa Because YMWV (Your model WILL vary) between revisions. 01. 02. 42

Slide 43

Slide 43 text

alembic -c dev.ini revision --autogenerate -m "magic hour"

Slide 44

Slide 44 text

Encourage Refactor and Clean Code • Feature development helps some • Clean code, testing and refactor help everyone 44

Slide 45

Slide 45 text

From Web app to API • Reusable permission control • Customized API render (extra work on JSON response) @view_defaults(renderer='api', permission='account') class APIResourceView(APIBaseView, APIPagingMixin): ... 01. 02. 03. 45

Slide 46

Slide 46 text

From Web app to API (cont.) • Views for API Forbidden and NotFound config.add_forbidden_view( APIForbiddenView, path_info='/api/') config.add_notfound_view( APINotFoundView, path_info='/api/') (no months or years needed for working) 01. 02. 03. 04. 46

Slide 47

Slide 47 text

API V2 Routes Ready from .api.routes import api_routes config.include(api_routes, '/api/v1') from .api.v1.routes import api_routes as api_v1_routes config.include(api_v1_routes, '/api/v1') from .api.routes import api_routes as api_v2_routes config.include(api_v2_routes, '/api/v2') 01. 02. 01. 02. 03. 04. 47

Slide 48

Slide 48 text

哭吧你 哭吧你 現在每個人可獲得1 Pyramid。 更彈性。要怎樣就怎樣。 更多元。做不完的決定。 更多頁。為什麼不立即開始讀讀不完的文件呢? # Rickr 梗 “ 48

Slide 49

Slide 49 text

Much More  @pylons 49

Slide 50

Slide 50 text

Thank You 