Last active
July 22, 2020 11:06
-
-
Save naltimari/77c30bb686015da988400fc7552db011 to your computer and use it in GitHub Desktop.
Python App Config Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## nice wrapper for options (config) that you keep in code | |
## (example is a hard-coded python dict, but could be ini file, envvars or DB table) | |
options = { | |
'key1': 'value1', | |
'key2': 'value2', | |
'key3': 'value3', | |
'debug': True | |
} | |
class configwrapper(object): | |
def __call__(self, *args): | |
from operator import itemgetter | |
return itemgetter(*args)(self) | |
def __getitem__(self, key): | |
return options.get(key.lower()) | |
get = configwrapper() | |
# from anywhere in your code, you would do this: | |
import config | |
k1,k2 = config.get('Key1', 'Key2') | |
debug = config.get('debug') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment