Skip to main content
edited tags
Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257
fix typo
Source Link
Nestor
  • 301
  • 1
  • 3
  • 10

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.

LookipDictLookupDict is pretty much the same as the requests implementation (see code here)

Any comments would be very appreciated. Thanks in advance!

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.

LookipDict is pretty much the same as the requests implementation (see code here)

Any comments would be very appreciated. Thanks in advance!

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!

Source Link
Nestor
  • 301
  • 1
  • 3
  • 10

Config Variables Initialization | Python

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.

LookipDict is pretty much the same as the requests implementation (see code here)

Any comments would be very appreciated. Thanks in advance!