Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Part 1
Lecture: Building Block: config

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Another key building block is configuration.
0:02 In any real web application you are going to have specific details
0:07 that either you want to keep out of code
0:09 because you want to make them editable and configurable,
0:11 or more likely you want to have different versions
0:14 based on where your app is running.
0:16 Is it being used in development? Is it being run in production? Things like that.
0:22 So here is a standard configuration file,
0:23 this will be in development.ini or production.ini, take your pick;
0:27 you can make these files up and hand then off to the web server.
0:30 Notice here that we have two values that we've added,
0:34 we've added db_file, which is going to be a relative path
0:37 to a SQLite database file, and api_key.
0:41 Maybe this is our MailChimp or Stripe API key.
0:44 For whatever reason, our application is going to need this data to work.
0:48 So how do we get to it?
0:49 Well, over in the __init__ method we saw we have this "main"
0:53 and it creates a configuration, we've already used this for a routing
0:56 but it also has other functions, for example we can say get us the settings,
1:01 which are the settings out of this section of ini file
1:05 and then we can do things like just say get the db_file or get the api_key,
1:09 config settings as a dictionary and it's populated with all the values that you see here.
1:14 Then you save this pass around your application and use it wherever you need them.