If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Thursday, July 09, 2020

Using module __dir__ and __getattr__ for configuration

PEP 562 added support for module level __dir__ and __getitem__
  • __dir__ is called when the built-in dir function is called on the module
  • __getattr__ is called when an attribute is not found via the regular attribute lookup
Let's use this to build an environment based configuration module. 
  • Conviruation values has a value, environment key and a function to convert from str to right type
    • I'm going to use dataclasses and populate values from environment in __post_init__
    • Complex data types (such as list) should be JSON encoded in the environment variables
  • All configuration values with start with the c_ prefix
  • __dir__ will return a list configuration variables without the c_ prefix
  • __getattr__ will add the c_ prefix and will look for the varialbes in globals
We're adding c_ prefix and removing it to bypass the regular attribute lookup mechanism. If we'll call a variable http_port and user will write config.http_port, our __dir__ function won't be called.

Here's the code

13 comments:

Aptron Gurgaon said...

Thanks for sharing this Information. Python Course in Gurgaon

Learninbox said...

This article is a great article, thank you for sharing with us Python

Learncodeprograms said...

Very informative blog, thanks for the information.
DO check out Studytonight for more Python tutorials for future reference.

Anonymous said...

How to add two numbers entered by the user in #python | #bcaschool

How to #add two #numbers in #python | #addition in #python | BCA School

Maha Queen said...

Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Custom Web Design | SEO Company Pakistan

SP Global Solution Software Training said...

Thank You for sharing python course

Raj Sharma said...

Python Training in Noida

Rajendra Cholan said...

Data Science Course in Chennai | Infycle Technologies

Data science can be defined as a blend of Mathematics. The entire Data Life Cycle is covered in our Data Science course in Chennai, which includes Data Architecture, Statistics, Advanced Data Analytics, and Machine Learning. To enrol in this course, you must have a basic understanding of statistics and mathematics. We Infycle offer the Data Science course in Chennai with 200% practical classes, pre-mock interview sessions, and more career counseling classes. For a free demo session, call us at 7502633633.
Best python course in Chennai

Ramesh Sampangi said...

Good article. Well-written and informative.
Python Course Training at Hyderabad

Rajendra Cholan said...

If Big Data is a job that you're dreaming of, then we, Infycle are with you to make your dream into reality. Infycle Technologies offers the best Big Data Course Chennai, with various levels of highly demanded software courses such as Java, Python, Hadoop, AWS, etc., in 100% hands-on practical training with specialized tutors in the field. Along with that, the pre-interviews will be given for the candidates, so that, they can face the interviews with complete knowledge. To know more, dial 7502633633 for more.

Best software training in Chennai

aaryan said...
This comment has been removed by the author.
inventateq said...

Python Course online
Python online training

pragyan said...
This comment has been removed by the author.

Blog Archive