[Flask] SDLC configuration

Badri Sunderarajan badrihippo at gmail.com
Tue Aug 9 04:36:24 EDT 2016


I think having Config as a class is just a convenient way to have 
multiple configs without repeating code (so you can just inherit the 
repeating bits). From what I can make out, Flask doesn't actually use it 
as a normal class (eg. call __new__ and stuff).

Maybe it'll be better in this case to forget about trying to make it 
object-oriented (come on, it's just a config file!) and run it as a 
normal script. So you can have

FLOWBOT_ENV = os.environ.get('FLOWBOT_ENV', None)
if FLOWBOT_ENV is not None:
     self.PROJECTS = load_active_projects(FLOWBOT_ENV)
     self.SCHEDULED_JOBS = load_active_scheduled_jobs(FLOWBOT_ENV)

This is usually not recommended because it'll do the loading according 
to the environment variable and give the same values for all the configs 
(eg. if FLOWBOT_ENV == 'foo' then it'll load 'foo' files for /all/ of 
Config, DevConfig, ProductionConfig, ...). But in this case it doesn't 
matter because you know you're choosing the /config/ itself based on the 
environment variable as well. So basically, the other configs will 
sometimes be wrong, but only when you're not using them.

This can get confusing for later developers, though, so make sure you 
explain it in the comments! Or it might be better to drop the whole idea 
of inheriting classes and do the whole config as a script:

FLOWBOT_ENV = os.environ.get('FLOWBOT_ENV', None)

# generic config options here

if FLOWBOT_ENV == 'dev':
     # dev-specific options here
elif FLOWBOT_ENV == 'production':
     # production-specific options here
elif FLOWBOT_ENV == 'qa':
     # qa-specific options here
# ...

(This will need app.config.from_pyfile instead of from_object). 
Inheritance would be useful if you're going to be doing multi-level 
inheritance (eg. having a TestingConfig and BackupServerDevConfig 
subclassing DevConfig), but in your case I think you'll be equally fine 
with the script style.

Basically it's just a config script so I guess you could just use 
whichever style seems to work better for you. Also, remember it's 
interpreted line-by-line. So something you set later will override what 
you set earlier anyways.

DEBUG = False
# (some other code)
if something:
     DEBUG = True

# if something was True then DEBUG will end up as turned on,
# even if it was off to start with

Sorry if that was a bit long coz I was thinking as I typed! Hope it helps.

—Badri/Hippo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160809/723de508/attachment.html>


More information about the Flask mailing list