Package: object-settings
object-settingsSimple-to-use object-oriented Python config library, where your settings are objects.Their values have automatic validation and get saved to a file that's seamlessly written and read in the background, so you don't have to worry about any of it. This makes it quick to define and use settings (see examples below).Installation & usageThis package is on PyPi, so you can just dopip install object-settingsAfter which it will be available with just the module namesettingsimportsettingssettings.setup("Your app name")your_option1=settings.Toggle("Your first option label")your_option2=settings.Number("Your second option label")Just simple objectsFor example, you can set a font size at the top of your ui file:font=settings.Number(default=14)...someuilib.Label("Bababooey",size=font.value)...someuilib.Textbox("Lorem ipsum dolor...",font_size=font.value)...Or if a setting is only checked in one place, it can be used without defining a variable:ifsettings.Toggle("Update app automatically",default=True):# do update(it doesn't matter if the same setting is initialized multiple times)IntegrationThe setting objects support "equals"-checking with actual values:speed=settings.Number("Speed limit",5)print(speed==5)>>Trueprint(speed==3)>>FalseIn addition, they work with many type-specific operations:forselectioninsettings.Multichoice():...ifsettings.Toggle():...Automatic storingWhen a setting's value is read/set, object-settings automatically creates and updates a config file on the disk in the background. It can read many file types, like.cfg,.jsonand.yaml. Any file deletions or unparsable external modifications are also handled.By default, the files are saved to a standard config location, depending on the platform (usesappdirspackage for paths). You can also set a custom directory for e.g. running in a Docker container.Setting values are also automatically read from the environment, like from env vars or command line options. The under-the-hood parser system is also very extensible, so you can create and add custom ones for e.g. a custom database.Value validationWhen a new value is set, it automatically gets validated and raises aValueErrorif it doesn't pass:update_interval=settings.Number("Update interval",default=5)update_interval.set("Daily")>>ValueErrorThis validation includes more than just datatypes, for example numbers can have min/max limits, or a path setting can be set to require an existing path:path=settings.Path("Download path",has_to_exist=True)path.set("/nonexistent/directory")>>ValueErrorListen for changesIf you have some update function that you want to be called when a setting is changed, you can add that function as a listener:some_setting.add_listener(your_function)Now the function will be called every time when a new value is set.SectionsOptionally, if you have a lot of settings, you can organize them into sections (which also works well with UIs):download_options=settings.Section("Downloader settings")speed=settings.Number("Speed limit",5,section=download_options)dir=settings.Path("Target directory",'/home/yomama/Downloads',section=download_options)server=settings.Choice("Mirror",["Europe","Asia","America","Africa"],"Asia",section=download_options)Did I mention free GUIs?That's right, this library also includes a separatesettings_guipackage that has pre-made settings menus for various GUI toolkits. They have full integration with the aforementioned systems, like validation and sections.Here's an example of some dummy settings with both libraries:(notice the warning for the misspelt download path)Nice-looking ttk (theme: Sun Valley dark)Bare tkinter works toosettings_guihas subpackages for tkinter and ttk, with at least GTK coming in the future.You can import the subpackage for your toolkit and then use a frame or a full settings window.Example with tkinter:importtkinter,settings,settings_gui.tkintersettings.setup("Crazy app")settings.Toggle("I'm graphical baby!",True)root=tkinter.Tk()settings_gui.tkinter.SettingsWindow(root)root.mainloop()You can also configure some parameters for the gui package to make it fit in:Change options like widget padding by callingsettings_gui.config.config()Change the strings the widgets use (save button, file chooser, ...) by callingsettings_gui.config.strings()Or, if you want to get more custom/contextual, you can also use the individual setting widgets and place them around your app (submoduletype_frames).All setting typesList of currently available setting types:Toggle:
A boolean True/FalseChoice:
Choose an option (str) from a listMappedChoice:
Choose an option (str) from a list, but have a different internal value mapped to itMultichoice:
Choose multiple options (str) from a listMappedMultichoice:
Choose multiple options (str) from a list, but have different internal values mapped to themArray:
An iterable of any arbitrary stringsText:
Just a basic text valuePath:
A file path whose existence can be checkedNumber:
An integer that can be set or incremented and decrementedFloat:
Like Number but, you know, as a float (with adjustable decimal precision)You can also inherit from theBaseSettingclass to easily create custom ones.