RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Config

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Our next building block is configuration. Now, these web apps come with two prebuilt configurations a production one and a development one,
0:11 but you can make as many of them as you like, and just pass them to the start up for the web app. So here's a typical configuration file,
0:18 we've got some stuff about running our web application in this case when we created it we called it my_webapp,
0:25 not very creative, but that's what it was called. So we can say the main entry point is to use basically the package my web app
0:32 and then we can have our specific things that we care about in our web application, so maybe we need to be able to specify a different db file
0:42 for when we're running in debug versus development rather than when we're running in say production or test.
0:49 So here we can say db files can be db connection, right in this case we're using sqlight but either way, we can put this key in here
0:54 we can also put like an API key. Now a case where this might vary is, if you're using Stripe, so Stripe has both test API keys and production API keys,
1:05 the test one accepts test credit cards and does not charge you or charge whoever's card you put in there,
1:11 but the production one you obviously want it to take a real credit card, not test ones,
1:15 and you actually want it to charge when it says that the charge succeeded. So you very much might want to have different API keys
1:22 say for your Stripe keys and so on. You can also have different includes, here we're using the Pyramid debug tool bar
1:29 and this might only appear in the development.any but not production. Similarly reload templates maybe it's false and production to be faster,
1:36 but true here to be simpler like you make a change to our template file and they just automatically appear, so you don't have to restart the web app.
1:45 Now, once you set this up and you run your app, this will be passed over to our __init__ where we have our main entry point again,
1:52 and inside here we get our config right, this is already set up by Pyramid and we can go to this config and we can say give me the settings
1:59 that I found, or that you found in this configuration file, so I can go get me the settings, so there is a config.getsettings
2:07 and from there I can say let's take that db file and get it out, so config settings is just a standard dictionary, and I'm using the safe way
2:16 rather than the type that will throw a key error if it's not there,
2:19 so the safe way to get db file, so I'll say .get db file and get none if it's not there. Similarly API key get me the API key,
2:26 and now we can just take these and pass them off to whatever part of our web app, whatever subsystem we might need
2:33 so the db file might be passed to the data access layer and the API key may be to the credit card service and things like that.


Talk Python's Mastodon Michael Kennedy's Mastodon