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

from ez_settings.ez_settings import EZSettingsBase

settings = EZSettingsBase("/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 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 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 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 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 import EZSettingsBase

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

Checking if a setting exists

from ez_settings.ez_settings 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 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 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.0.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

ez_settings-1.0.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ez_settings-1.0.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for ez_settings-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b814486e98edda3607c4a9a3399de375459d6688537142be53f1424e08f621d7
MD5 960c744f70dde1d8e87652c6342f4350
BLAKE2b-256 5621987ca325568c7594e201c6ed5830fc718735bfcba2ddceb8c0007f78701d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ez_settings-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

File hashes

Hashes for ez_settings-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 01603499b47ec1267adceb920e6d595860051189c2e5a013bfb7c51319021f47
MD5 2f8159b16e80a227efe19ea0e0a7d8b6
BLAKE2b-256 fe5a02a8ae5163f1e423534f2bcc1392b11fbf7ccd599b0113b26f5652458dcc

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