Package: conjector
Python Config InjectorWhat is thisIt is a simple library to inject non-sensitive configurations into class variables.
Basically, it's likeBaseSettingsinpydanticlibrary but for constants injson,yaml,tomloriniformats.conjectorcan work with different Python types (liketuple,datetime,dataclassand so on) and recursively cast config values to them.More information about the library and all features you can find in the officialdocumentation.When to useIf you deal with constants in your code, like error messages, default values for something, numeric coefficients, and so on.If you hate global variables, and you like non-python files to store static information.If you want to have an easy way to manage different constants depending on environments (liketest,dev,prod).If you like type hints and clean code.How to installTo install this library just enter:pipinstallconjectorBy default,conjectorwork only with the builtinjsonandinideserializers.
To work withyamlortoml(if you are usingpython <= 3.10):pipinstallconjector[yaml]# orpipinstallconjector[toml]# or faster version of jsonpipinstallconjector[json]How to useFor injecting values you need only the decoratorpropertiesunder a target class.
By default, the library will search a config fileapplication.ymlin the same directory
where your file with the used decorator is located, like below:project_root
|---services
|   |   email_message_service.py
|   |   application.yml
|.....Example:services/application.yml:default_text_style:size:14weight:boldfont:"TimesNewRoman"color:-128-128-128language_greetings:-language:englishtext:hello-language:germantext:hallo-language:frenchtext:bonjourwellcome_message:"{greeting}!Thankyouforregistration,{username}!"mailing_frequency:days:5hours:12services/email_message_service.py:fromtypingimportTypedDictfromdataclassesimportdataclassfromdatetimeimporttimedeltafromconjectorimportproperties@dataclassclassTextStyle:size:intweight:strfont:strcolor:tuple[int,int,int]|strclassGreetingDict(TypedDict):language:strtext:str@propertiesclassEmailMessageService:default_text_style:TextStylelanguage_greetings:list[GreetingDict]wellcome_message:strmailing_frequency:timedelta|None# And using these class variables in some methods...And that's how will look an equivalent of the code above but with "hard-coded" constants, without config files and@propertiesdecorator:classEmailMessageService:default_text_style=TextStyle(size=14,weight="bold",font="Times New Roman",color=(128,128,128))language_greetings=[GreetingDict(language="english",text="hello"),GreetingDict(language="german",text="hallo"),GreetingDict(language="french",text="bonjour"),]wellcome_message="{greeting}! Thank you for registration,{username}!"mailing_frequency=timedelta(days=5,hours=12)# And using these class variables in some methods...All config values will be inserted and cast according to the type annotations once during the application or script start.Different environmentsUsing this library it's easy to manage different environments and corresponding config files.
It could be done like so:importosfromconjectorimportproperties@properties(filename=os.getenv("CONFIG_FILENAME","application.yml"))classSomeEnvDependingService:env_depend_var:strIn this case, you can setCONFIG_FILENAME=application-dev.ymlin env variables, andconjectorwill use that file.About contributingYou will makeconjectorbetter if you open issues or create pull requests with improvements.