Skip to main content

🌴 EZ Settings

EZ Settings is a very simple, lightweight settings module that makes it easy to get and set settings for your application. It's all stored in a JSON file, so any value you want to store has to be able to be serialized in a JSON file.

It works in "base" mode, where you make an object of the EZSettingsBase class and use that throughout your program. Or you can use it as a singleton by defining EZSettings once in the beginen of you program, and then calling it anywhere else.

EZSettingsBase

Let's look at the Base way first:

Initializing

You can provide a path where you want the settings file to be

from ez_settings import EZSettings

settings = EZSettings("/home/applications/my_app/settings.json")

Setting values

I like to make simple classes to store setting names, just because it makes it easier to autocomplete when writing code. You can also just pass in a normal string in the set and get functions.

from ez_settings.ez_settings_base import EZSettingsBase


class Settings:
    NAME = "name"
    POSITION = "position"
    SUPERBOWL_WINS = "superbowl_wins"
    TEAMS = "teams"
    ACTIVE = "active"


settings = EZSettingsBase("/home/applications/my_app/settings.json")

settings.set(Settings.NAME, "Tom Brady")
settings.set(Settings.SUPERBOWL_WINS, 5)

Getting values

from ez_settings.ez_settings_base import EZSettingsBase


class Settings:
    NAME = "name"
    POSITION = "position"
    SUPERBOWL_WINS = "superbowl_wins"
    TEAMS = "teams"
    ACTIVE = "active"


settings = EZSettingsBase("/home/applications/my_app/settings.json")

championships = settings.get(Settings.SUPERBOWL_WINS)

List values

You can append or pop items if the value of your setting is a list

from ez_settings.ez_settings_base import EZSettingsBase


class Settings:
    NAME = "name"
    POSITION = "position"
    SUPERBOWL_WINS = "superbowl_wins"
    TEAMS = "teams"
    ACTIVE = "active"


settings = EZSettingsBase("/home/applications/my_app/settings.json")

# set a list value
settings.set(Settings.TEAMS, ["New England"])

# add to the list value
settings.append(Settings.TEAMS, "Tampa Bay")

# remove from the list values
settings.pop(Settings.TEAMS, "New England")

Deleting a single setting

from ez_settings.ez_settings_base import EZSettingsBase


class Settings:
    NAME = "name"
    POSITION = "position"
    SUPERBOWL_WINS = "superbowl_wins"
    TEAMS = "teams"
    ACTIVE = "active"


settings = EZSettingsBase("/home/applications/my_app/settings.json")
settings.remove(Settings.POSITION)

Wiping all settings

from ez_settings.ez_settings_base import EZSettingsBase

settings = EZSettingsBase("/home/applications/my_app/settings.json")
settings.reset()

Checking if a setting exists

from ez_settings.ez_settings_base import EZSettingsBase

settings = EZSettingsBase("/home/applications/my_app/settings.json")
settings.exists("Injuries")

Singleton

Now let's do the exact same thing, just using the Singleton method

from ez_settings.ez_settings_base import EZSettings
from different_file import ClassThatNeedsSettings

from pathlib import Path

# set up the path to save the settings. Since this is of metaclass Singleton, we can now just always call
# EZSettings from anywhere in our program to get the object that's set up in this line.
EZSettings(Path.home() / "deleteme" / "settings.json")


class Settings:
    NAME = "name"
    POSITION = "position"
    SUPERBOWL_WINS = "superbowl_wins"
    TEAMS = "teams"
    ACTIVE = "active"


if __name__ == "__main__":
    # set some string values
    EZSettings().set(Settings.POSITION, "Quarterback")
    EZSettings().set(Settings.NAME, "Tom Brady")

    # set an int value
    EZSettings().set(Settings.SUPERBOWL_WINS, 5)

    # set a list value
    EZSettings().set(Settings.TEAMS, ["New England"])
    print(EZSettings().get(Settings.TEAMS))

    # add to the list value
    EZSettings().append(Settings.TEAMS, "Tampa Bay")
    print(EZSettings().get(Settings.TEAMS))

    # remove from the list values
    EZSettings().pop(Settings.TEAMS, "New England")
    print(EZSettings().get(Settings.TEAMS))

    # set a bool value
    EZSettings().set(Settings.ACTIVE, True)
    print(EZSettings().get(Settings.ACTIVE))

    # check to see if there's a setting called TOUCHDOWNS
    exists = EZSettings().exists("TOUCHDOWNS")
    print(exists)

    # get all the settings which have the value "True"
    print(EZSettings().get_setting_with_value(True))

    # check if there is a setting called "Sacks"
    print(EZSettings().exists("Sacks"))

    # set and get a dictionary value
    dict_value = {
        "apple": 5,
        "other_dict":
            {
                "playstation": 5,
                "xbox": 360
            }
    }

    EZSettings().set("dict", dict_value)
    print(EZSettings().get_setting_with_value(5))

Let's add another file to our program, with a class and a function that gets the name setting from our settings

> different_file.py

from ez_settings.ez_settings_base import EZSettings


class ClassThatNeedsSettings(object):
    def __init__(self):
        super().__init__()

    def get_a_setting(self):
        # there can only always ever be one instance of our EZSettings, so we can just get a value from it
        # without ever having to know where it came from or where in our program it was set
        print(EZSettings().get("name"))

In our main program, we can make an object of ClassThatNeedsSettings, call the get_a_setting method and it will print out the value of name

    # get an object that's defined in a different file
    another_class_in_another_file = ClassThatNeedsSettings()

    # since our EZSettings is defined as a Singleton, we can use the same EZSettings().get() call in this other file
    # to still get any value we set in this file, without having to have passed a reference to an object to the other
    # class
    another_class_in_another_file.get_a_setting()

Metadata

Release files for ez-settings 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ez-settings 1.2.0
File Size Uploaded
ez_settings-1.2.0.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ez-settings 1.2.0
File Interpreter ABI Platform
ez_settings-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / ez_settings-1.2.0.tar.gz

Download URL ez_settings-1.2.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d64457e63e02c095d2963563e4ab57d4d2854c674d681568f82f804fee3e9c0e
BLAKE2b-256 checksum
How to use checksums
5eea4c8e6daff23d9a39487c63d97888c4ac9121098379282b186c886e4ff477
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / ez_settings-1.2.0-py3-none-any.whl

Download URL ez_settings-1.2.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
085a5083fee5d70f6f9e01f14a175721e57778691b375aa641f833fa00c46bce
BLAKE2b-256 checksum
How to use checksums
a4a141a87116c24959ed0f4f9f2b29a261b3e65752574ef5d49428240fb562d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page