Skip to main content

Simple object-oriented config library, where your settings are objects

Project description

object-settings

Simple-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 & usage

This package is on PyPi, so you can just do pip install object-settings

After which it will be available with just the module name settings

import settings
settings.setup("Your app name")
    
your_option1 = settings.Toggle("Your first option label")
your_option2 = settings.Number("Your second option label")

Just simple objects

For 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:

if settings.Toggle("Update app automatically", default=True):
    # do update

(it doesn't matter if the same setting is initialized multiple times)

Integration

The setting objects support "equals"-checking with actual values:

speed = settings.Number("Speed limit", 5)

print(speed == 5)
>> True
print(speed == 3)
>> False

In addition, they work with many type-specific operations:

for selection in settings.Multichoice():
    ...

if settings.Toggle():
    ...

Automatic storing

When 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, .json and .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 (uses appdirs package 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 validation

When a new value is set, it automatically gets validated and raises a ValueError if it doesn't pass:

update_interval = settings.Number("Update interval", default=5)
update_interval.set("Daily")
>> ValueError

This 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")
>> ValueError

Listen for changes

If 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.

Sections

Optionally, 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 separate settings_gui package that has pre-made settings menus for both tkinter and ttk, with GTK and others to come. 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)

Ttk Nice-looking ttk (theme: Sun Valley dark)

Tkinter Bare tkinter works too

And you can get this automatically for all your defined settings by just calling one function (SettingsFrame or SettingsWindow, depending on preference)

Or, if you want to get more custom/contextual, you can also use the individual setting widgets and place them around your app (submodule type_frames).

Setting types

List of currently available setting types:

  • Toggle: A boolean True/False
  • Choice: Choose an option (str) from a list
  • MappedChoice: Choose an option (str) from a list, but have a different internal value mapped to it
  • Multichoice: Choose multiple options (str) from a list
  • MappedMultichoice: Choose multiple options (str) from a list, but have different internal values mapped to them
  • Array: An iterable of any arbitrary strings
  • Text: Just a basic text value
  • Path: A file path whose existence can be checked
  • Number: An integer that can be set or incremented and decremented

You can also inherit from the BaseSetting class to easily create custom ones.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

object-settings-4.3.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

object_settings-4.3-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file object-settings-4.3.tar.gz.

File metadata

  • Download URL: object-settings-4.3.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for object-settings-4.3.tar.gz
Algorithm Hash digest
SHA256 39893d3836683632763a0a725b5991f217d7f0e5f92a92104ba9be1c45787f9d
MD5 433390e4369636f2df6368cc3f86fd85
BLAKE2b-256 7dba6bce51fabedf1ee74ea381094b11722ef20b405f53b053652b9d726faed9

See more details on using hashes here.

Provenance

File details

Details for the file object_settings-4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for object_settings-4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 682cf3664c0b4e8bc830592eb1d247c0e41e7a7192183e0d14de40441b02f2cf
MD5 d263e8f5d8dd8cbc9c413eeb81df4dd9
BLAKE2b-256 5031c502219991482b64e3cd4bd0977ec3797010fb6e95be887988723986eb2d

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page