Skip to main content

Easy settings module

Project description

🌴 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()

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

ez_settings-1.2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

ez_settings-1.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file ez_settings-1.2.0.tar.gz.

File metadata

  • Download URL: ez_settings-1.2.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ez_settings-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d64457e63e02c095d2963563e4ab57d4d2854c674d681568f82f804fee3e9c0e
MD5 4bd0a422d80bd9c248cddd5ce2db0f79
BLAKE2b-256 5eea4c8e6daff23d9a39487c63d97888c4ac9121098379282b186c886e4ff477

See more details on using hashes here.

File details

Details for the file ez_settings-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: ez_settings-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ez_settings-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 085a5083fee5d70f6f9e01f14a175721e57778691b375aa641f833fa00c46bce
MD5 290b570257e3b595d192ba66aa4ecf96
BLAKE2b-256 a4a141a87116c24959ed0f4f9f2b29a261b3e65752574ef5d49428240fb562d6

See more details on using hashes here.

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