 So I'm Adrian. I'm a Montreal Python member. I work for Ludia, a casual gaming company based in Holport in Montreal We have about two million people playing daily to our games and we do basically our Python backend using Pyramid for our Facebook games and Well, I want to share with you guys Our experience well a tiny window about our experience with Pyramid Framework we actually really like but which can be painful so Pyramid the web framework doesn't have any opinion about your application. It lets you choose any persistence layer Let's say you want to use a SQL with an object relation mapper You can use a SQL at Kimi or if you want to use direct as a driver it even lets you write in files if you want to You can use any templating system and there is no specific layout It provides scaffolds comments with different scaffolding so that you can have you know Templates, but you can do basically whatever you want and organize your project the way you want It Pyramid really focus on the web the web aspects which well, it's a web framework It makes sense it really focus on processing HTTP requests and ending view dispatch so I Might have some Python code to you So just a super simple example. Can you see that? So It's it so the the way it works. It's it's quite well simple Using a configurator a configurator you you basically define your roots and then by scanning the module it automatically automatically map The the your views to the roots being matched In those two examples you can see that I've been using at the the line 18 a direct a response object to return direct HTML It's really a bad example because actually don't do that You might prefer the second form where you return a dictionary and you use the renderer on the line 21st On the line 21 the renderer to specify what what rendering process you want to you want to use in that case is JSON, but Pyramid provides template adapters so you can use Mako Jinja or you can even write your own and extend it So this is a really quick presentation for those that we don't know about Pyramid. It's it's the The good aspect of it is that it it takes no It really don't don't don't put you in rails in in some rails Well, it's a bad bad bad world job, but it don't put you in Wales It lets you like define the way you want you where you want to work this said So it really focus on that should be handling and and let you choose any anything you want but the the main problem is you must choose and and well at Lidia we've been working with different teams and at the beginning they were you know Kind of fusing Pyramid but in different ways which can become a pain in the neck If if you are like switching to a from a project to another one Some people prefer to organize their project by features some others by components like views and model. I don't you know, it's it's Organizing a project is really something personal and it's kind of a can be kind of a religion. So The the I would advise to set conventions or at least Document about them and and so that you don't you don't get lost in your own projects the second part I want to Talk about is the the advanced dispatch because basically in this in this tiny example You see you've seen that we have two routes based on a pattern slash and slash info but the Pyramid is really provided really advanced dispatch system Which lets you basically it lets it lets you define a route is not only defined by its pattern It can you can you can define predicates? so that you can With one route and different predicates dispatch the same dispatch the same route to different views So again, I guess an example will be better than a French accent so so like let's look at this so in That example Basically, I had We have one route which is like edit a profile. You see that the pattern I can have like a some match some variable in my pattern and then basically what I do is I use the predicate request method to Dispatch differently depending on what request method I the request Is so and this is really it's a good example in that in that case for example, let's say you are submitting a form The HTTP see holder here location which will return the 303 Which is going to be mapped on this which is going to show the form being filled and It's really nice because if the user, you know try to refresh the page You're not going to have any outlet. Ah, be careful. You're going to post twice the same and Also, you have a really good separation of concern You don't have to define tons of different routes depending on the situation Basically, you define a route and then after you can separate The the the view the processing so you don't have stuff such as if request dot method equal post How do this else if it's get I'll do that Which can be seen in all the other language. I won't name To I think you have another example and I do yeah a cool part also of the the advance dispatch and the Predicates is that you can actually have predicates based on security features so here for example, I have like a user view which by the way, I had it a Regular expression in the pattern. Well, you can do basically whatever you want with the the pattern and then after depending Let's say there is a administrator Connecting to the application. Well, you don't want again You don't want in the same view You don't want to do like if my user is an administrator I'm gonna do I'm gonna had some information you might prefer to separate this in different views So the first one may be like a simple profile view and the second one being being, you know Some user detail view which basically I also map to a different maker template so that I can I don't know ban a user or you know giving some Power-ups I'm a do game. So we have stuff like this and it's really handy again because it permits to have Really separation between your stuff another really cool thing I did not much mention is that those view config decorators Do not modify the function they Register the function inside the registry of the application But they don't know they do not modify The function it might it might seem not important But it is when you because you can basically unique test this Really easily because basically what I'm well here. I'm returning a empty deck But basically in my test I can I can really test the dict itself rather than an HTML page Do I have another example? I don't think I do no I don't So the Adams advanced dispatch aspect of pyramid is really powerful. I'm really presenting a tiny part of it the So you can do predicates on the on request you can you can even add your own predicates, which is really handy. We've been Well, I don't have time to talk about this. You can do predicates on notification the the main Problem with this it's it can become really complex because the more you had roots and the more you have pages It can be really tedious work to know a where Where is my slash users input or patch is mapped and Hopefully pyramid provide Some scripts so that you can list your roots and and views and one is p-routes for example, and it's it's put in French so it's always funny and You also have complexity in authentication the authentication system is not as Flexible to because you can only you can only set one authentication policy, but again you can extend it The next aspect I wanted to talk to you about is is really a poor super powerful Feature about pyramid. I really throw it as a as super obscure and I didn't want to touch it, but oh No, I want to talk about. Yeah, this is not in the good order Sorry guys, so an other really nice aspect about pyramid is the way you compose your application and the way you extend it so basically the pyramid provides an include a way to include your modules and your sub modules and You can really like a break apart different component of your application. So again, I Might have a really nice example That's it. So here what I do rather than then directly define my route my is I use the include Method from the configurator object, which I pass a module name and I could even like pass a callable, but It's it's Generally use a module name and what it does it what it means the the interface is really simple The module has to provide an include me method which take config as a first power meter And then after you can basically you can subcontract So you could do like in your main you're gonna say config dot include model config dot include views So that and then the views is going to do like what what is about the views? And you don't mix up the roots the the the model part the SQL session handling And for example in this one at the line 14, I include pyramid SQL commutility what I do behind the hood is I'm hiding this SQL say SQLization attribute to the request which lazily open when I use it and Which is also which is a closed as soon as the request processing is finished and This is only because I've been using this line. So We really use this way of organizing all codes. So basically we break apart every everything and we do we use the include to compose our application There is another example where basically it's part of the Lidia history where we had to do The same game twice and the producer where really like hey guys we have to do it really fast So just do a copy and paste and we were like no we're not about to do cause some copy advice And so why we did and it's not obviously Elegant, but the thing is in real life you have to you know deliver is always better than than perfect So what we did is we adapt the first application so that it can be Includable by the second one and and it worked basically we did what it's at the line to we include a wall application Reusing the wall root views and then after we just like override Whatever we wanted to override so that we can change the tiny details of branding stuff like this The next aspect I want to talk to you about is so Yep, yes, the only pain was pyramid is when you write pyramid extensions So that kind of a pyramid is good such as pyramid is good at community You're not writing WSGI extensions. You're really writing pyramid stuff It's it's just just so that you know because The next part has really so the power of traversal so this is probably one of my favorite Pyramid feature probably because it's the last I've discovered so Traversal is a is a is a more advanced that a simple URL dispatch We have seen before to define route and register views Traversal basically is an algorithm Which works in two steps first the context discovery and then the view dispatch we've seen basically what it did it from a From a root root object associated with the request it then Traverse it using the dick like get items method. So in that example Here is what the python operation under the hood when we're trying to reach this user message so So this for example would at the end The context discovery would discover a user message Which then you can use predicates the context predicate then you can use sorry So yet then you can use the contact the context predicate on the view config decorator to my peak to a to a view and Have tons of example of it. I'm not obviously have time but So basically how it works is you set a root factory and then after the root factory received the request and You provide the get item so that you can traverse it if you raise a key error basically it's gonna raise an HTTP not found and Then after you discover like the user collection. So you continue the the traversal algorithm returning a user resource And then when you're found your users name resource You can then map it to your views the really and the part again is that you can basically it's really Andy for doing a rest API like Rest API and again, you can really separate it and you can really separate the you do really a good separation of concern the It's really provide a hierarchical API in terms of documentation and code Basically, you might be use your the URI to to traversal operation The HTTP methods became the views and the domain logic of the model it's less common and simple than dispatch plus the the the Scripts that you use to list all the roots and all the views Disappeared because basically you'd be roots would not list any roots. You don't define any more roots you just define a root object, which is going to be traversed and One one problem, too, is you have to write always the same Usain ball of plate code. So It's kind of tedious because you you have to repeat it or create a framework with So in conclusion Pyhamed as if you if you need to to work on some API is a really really elegant and neat framework which permits you to really organize well the project and We definitely like put more join developers hearts than than pain So just just use it and if you have any question