If it won't be simple, it simply won't be. [Hire me , source code] by Miki Tebeka, CEO, 353Solutions
Thursday, March 05, 2020
Using __getattr__ for nicer configuration API
Typically, you'll read configuration from files (such as YAML) and get them as a dictionary. However in Python you'd like to write config.httpd.port and not config['httpd']['port']
__getattr__ is a hook method that's called by Python when regular attribute lookup fails (not to be confused with the lower level __getattribute__, which is much harder to work with). You can use it to wrap the configuration dictionary. Here's a small example.
__getattr__ is a hook method that's called by Python when regular attribute lookup fails (not to be confused with the lower level __getattribute__, which is much harder to work with). You can use it to wrap the configuration dictionary. Here's a small example.
Labels:
configuration,
python
Subscribe to:
Comments (Atom)
Blog Archive
-
2016
(17)
- December (1)
- November (2)
- October (1)
- September (1)
- August (2)
- July (1)
- June (1)
- May (1)
- April (1)
- March (3)
- February (1)
- January (2)
-
2015
(18)
- December (2)
- November (1)
- September (2)
- August (1)
- July (2)
- June (3)
- May (1)
- April (2)
- February (2)
- January (2)
-
2014
(24)
- December (3)
- November (3)
- October (4)
- September (2)
- August (1)
- July (3)
- June (2)
- May (1)
- April (2)
- February (1)
- January (2)
-
2013
(35)
- December (1)
- November (4)
- October (4)
- September (6)
- August (4)
- July (2)
- May (3)
- April (4)
- March (4)
- February (2)
- January (1)
-
2012
(22)
- December (2)
- November (2)
- October (3)
- September (3)
- August (1)
- July (2)
- June (1)
- May (1)
- April (1)
- March (2)
- February (2)
- January (2)
-
2011
(29)
- December (3)
- November (2)
- October (2)
- September (4)
- August (2)
- July (2)
- June (2)
- May (4)
- April (1)
- March (3)
- February (1)
- January (3)
-
2010
(17)
- December (1)
- November (1)
- September (1)
- August (2)
- July (1)
- June (3)
- May (1)
- April (2)
- March (2)
- February (2)
- January (1)
-
2009
(38)
- December (4)
- November (1)
- October (3)
- September (3)
- August (3)
- July (5)
- June (5)
- May (2)
- April (2)
- March (2)
- February (4)
- January (4)
-
2008
(45)
- December (1)
- November (3)
- October (1)
- September (6)
- August (3)
- July (6)
- June (3)
- May (4)
- April (5)
- March (3)
- February (7)
- January (3)