Use Pyramid Like a Pro

Keith Yang

Use Pyramid

Like a Pro

Keith Yang

@keitheis

Pyramid Aliens Picture by Felix LaFlamme

@keitheisKeith Yang

biideal logo

Some fun fact

Taipei.py Logo

Taipei.py

Taipei.py 的奇幻漂流

Pyramid Official
Core

Choice
Like
Human Being

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

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

Architecture,
Architecture,
Architecture.

That's why
Flexibility
Counts

The Power of Pyramid (Pylons Project)

  1. Flexibility: from minimal to full stack seriously ready
  2. Views config & varies routes
  3. Add-ons and friends
  4. Reliability, Agilibility, Simplicity and Community

Python 3
Ready
from Pyramid 1.3

What Pyramid is NOT

Not the first web framework
you should learn together with
your first programming language (e.g. Python).

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 fine.

Rumors (cont.)

“Why you should use this web framework”

Said, Django, Flask, Web2py, Bottle, Rails, ...
all the excellent choices.

Also known as「不要欺騙小孩子」
課本上有寫「政黨體質的健全、民主、清廉與執政能力關係人民的幸福」

I say:
“Just use Python”

pythocat icon on github

不解釋。

Fundamental Pyramid School

For any MVC-like Framework
M or C is not for business logic

Fundamental Pyramid School (cont.)

Database: to SQL

Database: to NoSQL

Database: None

Perfect

No default binding ORM, no bother.

To ORM

Have you tried Taiwan Mango?

Not to ORM

Templates: Mako or Jinja2

Templates (cont.) - Plim

  1. by Maxim Avanov
  2. Ported from Slim template
  3. Clean HTML usage
  4. Built on top of Mako Templates
  5. Funand half hell on debug mode

Guess what? You'll learn how to guess by using it

Templates (cont.) - Plim

table: -for row in ['apple', 'banana', 'pineapple']
    tr: -for col in ['juice', 'muffin', 'pie']
        td ${row.capitalize()} ${col}

That's all. No </blah blah things>.

Templates (cont.) - Chameleon

<table>
    <tr tal:repeat="row 'apple', 'banana', 'pineapple'">
        <td tal:repeat="col 'juice', 'muffin', 'pie'">
             ${row.capitalize()} ${col}
        </td>
    </tr>
</table>

ZPT, Kid or Genshi lovers should give it a look

Too many to introduce (again)

Oh, Sentry

My better practices

Double-edged swords use them carefully.

My better practices (cont.)

Minimal Viable Code

My worser lesson 1

def ugly_customized_routes(config, resource_route,
    path=None,
    actions="ICRUD",
    resource_name=None):
    ...

Not aesthetic route helper

My worser lesson 2

print("This is too embarrassing to print")

Lacking CI tests

Debug mode

if debug_notfound_route:
    from .views.notfound import (
      NotFoundDebugView as NotFoundView
    )
else:
    from .views.notfound import NotFoundView
config.add_notfound_view(NotFoundView)
Debugtoolbar

SQL Master

SQL on Debugtoolbar

Logging in the flow

Logging on Debugtoolbar

Debug Debugtoolbar

# Disable development performance killer
debugtoolbar.panels =
    pyramid_debugtoolbar.panels.sqla.SQLADebugPanel
    ...
    #pyramid_debugtoolbar.panels.introspection.IntrospectionDebugPanel
    #pyramid_debugtoolbar.panels.tweens.TweensDebugPanel
    #pyramid_debugtoolbar.panels.routes.RoutesDebugPanel

Before your first release

Practice

Save
Your Self
In Time

Feel the power of Alembic

Because YMWV (Your model WILL vary) between revisions.

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

Encourage Refactor and Clean Code

From Web app to API

@view_defaults(renderer='api', permission='account')
class APIResourceView(APIBaseView, APIPagingMixin):
    ...

From Web app to API (cont.)

config.add_forbidden_view(
    APIForbiddenView, path_info='/api/')
config.add_notfound_view(
    APINotFoundView, path_info='/api/')

(no months or years needed for working)

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

哭吧你

現在每個人可獲得1 Pyramid。

更彈性。要怎樣就怎樣。
更多元。做不完的決定。
更多頁。為什麼不立即開始讀讀不完的文件呢?

# flickr 梗

Much More

Pylons Project
@pylons

PyCon Taiwan 2013



Thank You

Fork me on Github