Simple Environment Variable Parsing
Project description
If you use Heroku and/or subscribe to the tenets of the 12 Factor App you’ll be using a lot of environment variable-based configuration in your app. os.environ is a great choice to start off with but over time you’ll find yourself duplicating quite a bit of code around handling raw environment variables.
envparse aims to eliminate this duplicated, often inconsistent parsing code and instead provide a single, easy-to-use wrapper that handles:
Casting environment variables to a type:
MAIL_ENABLED=1 mail_enabled = env('MAIL_ENABLED', cast=bool) if mail_enabled: send_mail(...)Specifying defaults:
max_rows = env('MAX_ROWS', cast=int, default=100) rows = query(limit=max_rows)Proxying values, useful in Heroku for wiring up the environment variables they provide to the ones that your app actually uses:
MAILGUN_SMTP_LOGIN=foo # Heroku provides this with add-on SMTP_LOGIN={MAILGUN_SMTP_LOGIN} # App uses proxied variable smtp_login = env('SMTP_LOGIN') assert smtp_login == 'foo'
Now if you switch to using Mandrill as an email provider, instead of having to modify your app, you can simply make a configuration change:
SMTP_LOGIN={MANDRILL_UESRNAME}
Define a schema so you can only need to provide the type and defaults once:
MAIL_ENABLED=0 SMTP_LOGIN=bar # Bind schema to Env object to get schema-based lookups env = Env(MAIL_ENABLED=bool, SMTP_LOGIN=(str, 'foo')) assert env('MAIL_ENABLED') is False assert env('SMTP_LOGIN') == 'bar' ...later in the code... if env('MAIL_ENABLED'): send_email(...)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file envparse-0.1.6.tar.gz.
File metadata
- Download URL: envparse-0.1.6.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c69b6e07947660705a981a1c7d2bcd95c31f8c863e54acaec9d0235cffe32199
|
|
| MD5 |
2b2d556f195a0d502b8213b767fa55c8
|
|
| BLAKE2b-256 |
27834839190da401b4cb29bcd807bcb3578964897130e38d0fa0e760177595f2
|