by da39a3ee · 3y ago · 0 comments
Say I want to model in Python some collection of key-value pairs. It's a singleton -- I don't need a class or namedtuple that can be instantiated. So for example, I just want something like:
SingletonDataContainer Config:
root_directory: str = "/tmp"
should_frobnicate: bool = False
So of course, it could be a dict. But I want type annotations, and dotted attribute access syntax (and pretty syntax-highlighting!). It's tempting to use a class with class attributes for this, but that seems wrong since then you end up with something that can be instantiated, but which you never actually want to instantiate.A module variable set to a SimpleNamespace in the typing package is also an option.
from importlib.machinery import ModuleSpec
import importlib.util
import sys
spec = ModuleSpec("mymodule", loader=None)
module = importlib.util.module_from_spec(spec)
module.__dict__.update({"a": 42})
sys.modules["mymodule"] = module
import mymodule
print(mymodule.a) # -> 42
That's if you want a proper module. But modules can be any type and it's officially supported! import sys
from types import SimpleNamespace
sys.modules["mymodule"] = SimpleNamespace(b=37)
import mymodule
print(mymodule.b) # -> 37
# ^^^ This is literally just calling __getattr__ on the module object. Do with that what you will.
This will cross files as well but you need to make sure the code to create the module runs before you try importing it! glhf> Python? Rules? Unheard of.
OK, since it's Christmas, I'd like to be able to write
Module MyModule
a = 42
b = 37 const config = {
rootDirectory: "/tmp",
shouldFrobnicate: false,
}
I'm far from a python expert, more like a TS dev who occasionally tries to write Python, but when I'm in that situation I find that writing one-off classes seems to be python's way of doing things. I'm not a super big fan of it myself, but it works: from dataclasses import dataclass
def _create_config():
@dataclass(kw_only=True)
class Config:
root_directory: str = "/tmp"
should_frobnicate: bool = False
return Config()
config = _create_config()
I experimented with making a decorator that could do something like this, but never got it working with `mypy` so I gave up: @iife
def config():
@dataclass(kw_only=True)
class Config:
root_directory: str = "/tmp"
should_frobnicate: bool = False
return Config()
the `@iife` would make it so that `config` was a variable, not a `def`, but yeah the whole thing stank so I gave up class Config:
root_directory: str = "/tmp"
should_frobnicate: bool = False
if __name__=="__main__":
print(f"{Config.root_directory=}\n{Config.should_frobnicate=}")