A Python3.13+ library that wraps cattrs for a modular approach to constructing objects with the ability to string data through the process.
Install from pypi:
> python -m pip install strcs
Documentation at https://strcs.readthedocs.io/
fromtypingimportAnnotated, NewTypeimportattrsimportstrcsreg=strcs.CreateRegister() creator=reg.make_decorator() Number=NewType("Number", int) Word=NewType("Word", str) @attrs.define(frozen=True)classMaths(strcs.MetaAnnotation): multiply: intdefcalculate(self, val: int) ->Number: returnNumber(val*self.multiply) classThing: pass@attrs.defineclassConfig: thing: Annotated[Thing, strcs.FromMeta("thing")] words: list[Word] some_number: Annotated[Number, Maths(multiply=2)] contrived1: strcontrived2: strsome_other_number: int=16@creator(Number)defcreate_number(val: object, /, annotation: Maths) ->strcs.ConvertResponse[Number]: ifnotisinstance(val, int): returnNonereturnannotation.calculate(val) @creator(Word)defcreate_word(val: object, /, word_prefix: str="") ->strcs.ConvertResponse[Word]: ifnotisinstance(val, str): returnNonereturnWord(f"{word_prefix}{val}") @creator(Config)defcreate_config(val: object, /) ->strcs.ConvertResponse[Config]: ifnotisinstance(val, dict): returnNoneresult=dict(val) if"contrived"inresult: contrived=result.pop("contrived") result["contrived1"], result["contrived2"] =contrived.split("_") returnresultthing=Thing() meta=strcs.Meta({"thing": thing, "word_prefix": "the_prefix__"}) config=reg.create( Config,{"words": ["one", "two"], "some_number": 20, "contrived": "stephen_bob"}, meta=meta, ) print(config) assertisinstance(config, Config) assertconfig.thingisthingassertconfig.words== ["the_prefix__one", "the_prefix__two"] assertconfig.some_number==40assertconfig.some_other_number==16assertconfig.contrived1=="stephen"assertconfig.contrived2=="bob"To have a virtualenv that has everything needed in it:
> source run.sh activate
To run tests, linting, formatting, type checking:
> ./test.sh > ./lint > ./format > ./types