carmenbianca, i want to create a #python module but i need a 🦆
problem: sometimes i write a program that can be configured with json/yaml/toml/whatever. but i hate dealing with raw dictionaries and validating the data myself (or checking whether ["my"]["deep"]["key"] exists before accessing it). creating my own Config class to do that stuff for me is boring and repetitive.
what i want to do is this:
class MyConfig:
foo: str
bar: intmy_config = MyConfig.from_dict({"foo": "hello", " bar": 1})
... and have that be validated, ready to go, with convenience functions and everything. if a type doesn't match, raise an error. (also, some more validators like selections, or 'value matches regex'.)
i know attrs can do something like that, but the attributes are stored on the object instead of in an internal dict, and it's not a perfect match. pydantic has BaseSettings, but pydantic is so thoroughly overengineered that i don't want it.
but when i sit down to think about writing a module to do what i want, i realise i'm just reimplementing attrs, but lighter, without attributes stored on the object, and with certain convenience functions.
ought i just build my module on top of attrs, somehow? does my dream module already exist?
Add comment