0
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

With no usage shown, no implementation for LookupDict shown and nothing other than your network type values, this is overdesigned and offers nothing beyond

class NetworkType(Enum):
    GSM = 1
    UMTS = 2
    LTE = 3
\$\endgroup\$
4
  • \$\begingroup\$ I agree with you it is overdesigned if I won't use the purpose this was originally designed for by requests, but I just hate to write this, following your example: NetworkType.GSM.value every time I will use any of the config values \$\endgroup\$
    – Nestor
    Aug 28 at 18:15
  • \$\begingroup\$ Wouldn't it just be NetworkType.GSM? That seems pretty straightforward and readable. The value isn't important in enums, so if your use case is to actually use it, just omit the (Enum) from the class. \$\endgroup\$
    – ggorlen
    Aug 28 at 18:28
  • \$\begingroup\$ I actually need the value, thanks for the last tip, I didn't think about it that way \$\endgroup\$
    – Nestor
    Aug 28 at 19:00
  • \$\begingroup\$ PS: LookupDict definition is in the last link \$\endgroup\$
    – Nestor
    Aug 28 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.