Package: ez-settings
🌴 EZ SettingsEZ 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 theEZSettingsBaseclass and use that throughout your program. Or you can use it as a singleton by definingEZSettingsonce in the beginen of you program, and then calling it anywhere else.EZSettingsBaseLet's look at theBaseway first:InitializingYou can provide a path where you want the settings file to befromez_settingsimportEZSettingssettings=EZSettings("/home/applications/my_app/settings.json")Setting valuesI 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 thesetandgetfunctions.fromez_settings.ez_settings_baseimportEZSettingsBaseclassSettings: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 valuesfromez_settings.ez_settings_baseimportEZSettingsBaseclassSettings: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 valuesYou can append or pop items if the value of your setting is a listfromez_settings.ez_settings_baseimportEZSettingsBaseclassSettings:NAME="name"POSITION="position"SUPERBOWL_WINS="superbowl_wins"TEAMS="teams"ACTIVE="active"settings=EZSettingsBase("/home/applications/my_app/settings.json")# set a list valuesettings.set(Settings.TEAMS,["New England"])# add to the list valuesettings.append(Settings.TEAMS,"Tampa Bay")# remove from the list valuessettings.pop(Settings.TEAMS,"New England")Deleting a single settingfromez_settings.ez_settings_baseimportEZSettingsBaseclassSettings: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 settingsfromez_settings.ez_settings_baseimportEZSettingsBasesettings=EZSettingsBase("/home/applications/my_app/settings.json")settings.reset()Checking if a setting existsfromez_settings.ez_settings_baseimportEZSettingsBasesettings=EZSettingsBase("/home/applications/my_app/settings.json")settings.exists("Injuries")SingletonNow let's do the exact same thing, just using the Singleton methodfromez_settings.ez_settings_baseimportEZSettingsfromdifferent_fileimportClassThatNeedsSettingsfrompathlibimportPath# 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")classSettings:NAME="name"POSITION="position"SUPERBOWL_WINS="superbowl_wins"TEAMS="teams"ACTIVE="active"if__name__=="__main__":# set some string valuesEZSettings().set(Settings.POSITION,"Quarterback")EZSettings().set(Settings.NAME,"Tom Brady")# set an int valueEZSettings().set(Settings.SUPERBOWL_WINS,5)# set a list valueEZSettings().set(Settings.TEAMS,["New England"])print(EZSettings().get(Settings.TEAMS))# add to the list valueEZSettings().append(Settings.TEAMS,"Tampa Bay")print(EZSettings().get(Settings.TEAMS))# remove from the list valuesEZSettings().pop(Settings.TEAMS,"New England")print(EZSettings().get(Settings.TEAMS))# set a bool valueEZSettings().set(Settings.ACTIVE,True)print(EZSettings().get(Settings.ACTIVE))# check to see if there's a setting called TOUCHDOWNSexists=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 valuedict_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 thenamesetting from our settings> different_file.pyfromez_settings.ez_settings_baseimportEZSettingsclassClassThatNeedsSettings(object):def__init__(self):super().__init__()defget_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 setprint(EZSettings().get("name"))In our main program, we can make an object ofClassThatNeedsSettings, call theget_a_settingmethod and it will print out the value ofname# get an object that's defined in a different fileanother_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# classanother_class_in_another_file.get_a_setting()