I found interesting the way Python's requests library does the status_code
data structure initialization (see code here). I am reusing it in a hobby project, but instead of using just one configuration variable I want it to initialize a few of them, code below:
from .models import LookupDict
# Copying Requests data structure and some refactor to data initialization
network_types = LookupDict(name='network_types')
_network_types = {
1: ('GSM',),
2: ('UMTS',),
3: ('LTE',)
}
def _fill(_globals_var, globals_var):
for value, titles in _globals_var.items():
for title in titles:
setattr(globals_var, title, value)
def _init():
for _globals_var, globals_var in [
(_network_types, network_types)
]:
_fill(_globals_var, globals_var)
_init()
So far it's just defined network_types
variable but it could initialize as many as you want with both functions _init
and _fill
.
LookupDict
is pretty much the same as the requests
implementation (see code here)
Any comments would be very appreciated. Thanks in advance!