3 Ways to Perfect the Kids OTT User Experience

The kids OTT space is becoming increasingly competitive. Giants like Netflix, Amazon, and Disney are investing in children’s entertainment, while smaller players like Hopster, PlayKids, and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Refactoring Python Flask Environment Variables with Environs module

Here is the original config.py file before refactoring.

As you can see, it is not very DRY and really needed some refactoring. This is where environs comes in.

First install environs. I use pipenv so if you use another environment manager and package installer you can, of course, follow your normal procedure for installing python modules.

When we want to read variables out of the env object we do something like this:

With a default value, this can cut some repeated code, but what I really like is with env.prefixed . You can use prefixes of your choice in a function to grab all the variables with that prefix. Here’s how I am using it.

Here is the completely refactored config.py using all the ideas shared above.

It is sooooo much shorter and easier to read and it just makes me feel better looking at it compared to the original.

Let’s also compare the .env files.

Here is the original:

And here is the refactored version:

Again, easier to read and shorter.

Here is how I have set up the new environment variables in travis, since they are different with the refactor than in the previous iteration of this project.

Environs is very easy to use and implement and for me its a no-brainer for future projects. What have you done with Environs?

Add a comment

Related posts:

How to install an SSL Certificate on Cisco ASA 5500 series?

This comprehensive tutorial provides step by step instructions on how to generate a CSR code and install an SSL Certificate on Cisco ASA 5500 series. On top of that, you will also learn a few…

Basics of ES6 Destructuring

Destructuring is a feature of the ECMAScript 2016 standard, commonly referred to as ES6, that affords JavaScript developers a large degree of flexibility when writing code. The main benefit…