Share
Embed
Transcript
40 slides
-
lostpropertyhq.com Learn something new A quick look into the Pyramid
web framework / Created byRach Belaid @rachbelaid -
Who I am. Python charmer, Git Lover and a PostgreSQL
Fan. I build web stuff and love learning something new. work at Lost property -
I may be harsh! ... but I mean well Django
is a great framework! It allowed me to make my living from Python, a language that I ❤ -
So why look somewhere else then? To discover other solutions
to common problems You are a python developer and not a Django developer It's fun and exicting Due to frustrations To extend your mind and toolbelt with new tools To come back with a fresh eyes -
I was living a happy life and had my own
swiss army knife as a toolkit -
Modular Flexible Agnostic, Not opiniated Extensible Fast Full Control For
small and big webapps -
Simple example f r o m w s g i
r e f . s i m p l e _ s e r v e r i m p o r t m a k e _ s e r v e r f r o m p y r a m i d . c o n f i g i m p o r t C o n f i g u r a t o r f r o m p y r a m i d . r e s p o n s e i m p o r t R e s p o n s e d e f h e l l o _ w o r l d ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' H e l l o w o r l d ! ' ) i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : c o n f i g = C o n f i g u r a t o r ( ) c o n f i g . a d d _ r o u t e ( ' h o m e ' , ' / ' ) c o n f i g . a d d _ v i e w ( h e l l o _ w o r l d , r o u t e _ n a m e = ' h o m e ' ) a p p = c o n f i g . m a k e _ w s g i _ a p p ( ) s e r v e r = m a k e _ s e r v e r ( ' 0 . 0 . 0 . 0 ' , 8 0 8 0 , a p p ) s e r v e r . s e r v e _ f o r e v e r ( ) -
Same example but another way f r o m w
s g i r e f . s i m p l e _ s e r v e r i m p o r t m a k e _ s e r v e r f r o m p y r a m i d . c o n f i g i m p o r t C o n f i g u r a t o r f r o m p y r a m i d . r e s p o n s e i m p o r t R e s p o n s e f r o m p y r a m i d . v i e w i m p o r t v i e w _ c o n f i g @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' h o m e ' ) d e f h e l l o _ w o r l d ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' H e l l o w o r l d ! ' ) i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : c o n f i g = C o n f i g u r a t o r ( ) c o n f i g . a d d _ r o u t e ( ' h o m e ' , ' / ' ) c o n f i g . s c a n ( ) # r u n t h e a p p a p p = c o n f i g . m a k e _ w s g i _ a p p ( ) s e r v e r = m a k e _ s e r v e r ( ' 0 . 0 . 0 . 0 ' , 8 0 8 0 , a p p ) s e r v e r . s e r v e _ f o r e v e r ( ) -
TIMTOWTDI. Blasphemy !! with pyramid there is more than one
way to do things!!! I want my tools to bend to my needs and not me to them. -
Battery not included. Nor the shell, nor the wheels. Time
to go shopping and get the best out there!! -
Routing View != Route Base on method, xhr, param, custom
function(predicate) -
Routing Example via Method @ v i e w _
c o n f i g ( r o u t e _ n a m e = ' t e s t ' , r e q u e s t _ m e t h o d = ' G E T ' ) d e f h e l l o _ w o r l d ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' H e l l o w o r l d ! ' ) @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' t e s t ' , r e q u e s t _ m e t h o d = ' P O S T ' ) d e f t h a n k s _ y o u ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' T h a n k s y o u ' ) i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : c o n f i g = C o n f i g u r a t o r ( ) c o n f i g . a d d _ r o u t e ( ' t e s t ' , ' / t e s t ' ) c o n f i g . s c a n ( ) -
Routing Example with param @ v i e w _
c o n f i g ( r o u t e _ n a m e = ' a c t i o n ' , m a t c h _ p a r a m = ' a c t i o n = h e l l o ' ) d e f h e l l o _ v i e w ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' H e l l o ' ) @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' a c t i o n ' , m a t c h _ p a r a m = ' a c t i o n = b y e ' ) d e f b y e _ v i e w ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' B y e B y e ' ) @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' a c t i o n ' , m a t c h _ p a r a m = ' a c t i o n ' ) d e f b y e _ v i e w ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' L e t ’ s c o n t i n u e ' ) i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : c o n f i g = C o n f i g u r a t o r ( ) c o n f i g . a d d _ r o u t e ( ' a c t i o n ' , ' / { a c t i o n : \ w + } ' ) c o n f i g . s c a n ( ) -
Routing Example with predicate d e f u s e
l e s s _ p r e d i c a t e ( i n f o , r e q u e s t ) : f r o m d a t e t i m e i m p o r t d a t e t o d a y = d a t e . t o d a y ( ) r e t u r n t o d a y . d a y = = 2 9 & & t o d a y . m o n t h = = 2 @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' h o m e ' , c u s t o m _ p r e d i c a t e = u s e l e s s _ p r e d i c a t e ) d e f u s e l e s s _ v i e w ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' L e a p y e a r ' ) i f _ _ n a m e _ _ = = ' _ _ m a i n _ _ ' : c o n f i g = C o n f i g u r a t o r ( ) c o n f i g . a d d _ r o u t e ( ' h o m e ' , ' / ' ) c o n f i g . s c a n ( ) -
Routing with class and method too @ v i e
w _ c o n f i g ( r o u t e _ n a m e = ' t e s t ' ) d e f t e s t ( r e q u e s t ) : r e t u r n R e s p o n s e ( ' o k ' ) c l a s s T e s t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , r e q u e s t ) : s e l f . r e q u e s t = r e q u e s t @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' t e s t ' ) d e f t e s t ( s e l f ) : r e t u r n R e s p o n s e ( ' o k ' ) @ v i e w _ c o n f i g ( r o u t e _ n a m e = ' t e s t ' ) c l a s s T e s t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , r e q u e s t ) : s e l f . r e q u e s t = r e q u e s t d e f _ _ c a l l _ _ ( s e l f ) : r e t u r n R e s p o n s e ( ' o k ' ) -
SQLAlchemy, the only true ORM Once you go SQLAlchemy, you
never go back. Finally an ORM that allows you to write better queries. -
Admin Sorry, there is none ... but you may have
a chance with FormAlchemy. -
Hooks Tweens, Event, Finished, Callbacks, Response Adapter ... to play
ther role of middleware, context processor django -
Tweens A tween (a contraction of the word “between”) is
a bit of code that sits between the Pyramid router’s main request handling function and the upstream WSGI -
Response Adaptor to control how Pyramid treats the view result
f r o m p y r a m i d . r e s p o n s e i m p o r t R e s p o n s e d e f s t r i n g _ r e s p o n s e _ a d a p t e r ( s ) : r e s p o n s e = R e s p o n s e ( s ) r e t u r n r e s p o n s e # c o n f i g i s a n i n s t a n c e o f p y r a m i d . c o n f i g . C o n f i g u r a t o r c o n f i g . a d d _ r e s p o n s e _ a d a p t e r ( s t r i n g _ r e s p o n s e _ a d a p t e r , s t r ) -
Events loads of events : NewResponse, ApplicationCreated, ... f r
o m p y r a m i d . e v e n t s i m p o r t s u b s c r i b e r f r o m p y r a m i d . e v e n t s i m p o r t B e f o r e R e n d e r @ s u b s c r i b e r ( B e f o r e R e n d e r ) d e f a d d _ g l o b a l ( e v e n t ) : e v e n t [ ' m y k e y ' ] = ' f o o ' -
Subscriber Predicates to narrow the set of circumstances in which
a subscriber to an event will be called -
Finished Callback Will be call no matter what happens! i
m p o r t l o g g i n g l o g = l o g g i n g . g e t L o g g e r ( _ _ n a m e _ _ ) d e f l o g _ c a l l b a c k ( r e q u e s t ) : " " " L o g i n f o r m a t i o n a t t h e e n d o f r e q u e s t " " " l o g . d e b u g ( ' R e q u e s t i s f i n i s h e d . ' ) r e q u e s t . a d d _ f i n i s h e d _ c a l l b a c k ( l o g _ c a l l b a c k ) -
Authentication & Authorization policy Clear separation between Authorization and Authentication
Powerfull ACL Resource Tree dependency Or build your own -
Config Imperative configuration or .ini file for non dev colleagues
or even ZCML(XML) .ini file can support to serve multi wsgi app (paste legacy) -
Interrested in Pyramid? Have a chat. Check the doc, tutorial.
Come to the Pyramid London Meetup. -
A Final Word Frameworks are just tools. Use the ones
which make you happy to code.