Package: privateprefs
PrivatePrefsEasily Keep API Keys out of GitHubprivateprefskeeps sensitive data like API Keys, email addresses, usernames, etc, out
of your code, so sensitive data can't accidentally get added to private or public repositories like GitHub,
GitLab, Bitbucket, etc.Table of ContentsInstallationGetting StartedPython DocsCLI DocsLicenseThe ProblemSpammers and scammers can scrape code from public and even private repos😱secret_api_key="mfnc80imW4RawjYwVLsArx"my_email="darren@spammed.com"# @spammers please send me lots of spam and @hackers feel free to hack my accounts!# thanks, Your Next Victim.The SolutionUseprivateprefsand never hardcode sensitive data in your code again😎importprivateprefsasprefssecret_api_key=prefs.load("secret_api_key")my_email=prefs.load("my_email")# @spammers sorry, spammers no spamming and @hackers sorry, hackers no hacking!# thanks, Not Your Victim.before this solution will work we need toinstallprivateprefsuse the command line to save/store sensitive data in key-value pairs.the data is stored in a data.ini file stored on your computerthe data.ini is stored outside your project and won't get added to reposuse python to load the values by calling the corresponding keysThis will become clear after youInstallprivateprefsand follow along with theGetting Startedguide below.InstallationUsepipto installpipinstallprivateprefsGetting StartedSave Valuerun commandprivateprefssave"secret_api_key""mfnc80imW4RawjYwVLsArx"This saves/sets thevalue"mfnc80imW4RawjYwVLsArx"to thekey"secret_api_key"Load Valuepython scriptimportprivateprefsasprefsprefs.load("secret_api_key")# "mfnc80imW4RawjYwVLsArx"This loads/gets thevalue"mfnc80imW4RawjYwVLsArx"from thekey"secret_api_key"Show Me The Datarun commandprivateprefsdatareturnskey-value data pairs are stored in the data.ini file located at:
C:\Users\USER_NAME\AppData\Local\privateprefs\data.ini (running windows)

data.ini file contents:
+----------------+------------------------+
|      KEY       |         VALUE          |
+----------------+------------------------+
| secret_api_key | mfnc80imW4RawjYwVLsArx |
+----------------+------------------------+The file path will very on different operating systems but here are the most common:C:\Users\USER_NAME\AppData\Local\privateprefs\data.ini (on windows)~/Library/Application Support/privateprefs/data.ini (on mac)~/.local/share/privateprefs/data.ini (on linux)The data table will list all saved/stored key-value pairs in the data.ini file.Open the Data Filerun commandprivateprefsopenThis will open the data.ini file in the default application associated with .ini files.
If your operating system ask you to choose an application, you can use a simple text editor
or your IDE of choice like PyCharm, IDLE, Visual Studio Code, etc.opened data.ini file (in PyCharm)We can add, remove, update, and delete key-value pairs by directly editing the data.ini file.
Let's add the keymy_emailwith a value ofdarren@spammed.comto the file manually.data.ini with newly added lineFor those who don't like to use the cli (command line interface) feel free to edit the data.ini file directly, but
please follow a few rules to ensure you don't break the packageThe first line must contain[privateprefs]the next lines must bemy_key = my valuepairsdon't add quotes to strings likemy_key = "my value"(don't do this!)it's best to use the same name rules for keys as withPython variables nameDelete Valueprivateprefsdelete"secret_api_key"Delete All Values-- WARNING ALL SAVED DATA WILL BE PERMANENTLY DELETED --# don't run if you have saved data you want to keepprivateprefsdelete_allConclusionYou now have a basic understanding of howprivateprefsworks and how to use it.
We left out a few Python functions and CLI commands, but they are covered in the docs
below if you're interested.Python DocsThe following Python code assumes we have the following two saved
key-value pairsrun these commands to add some data for testingprivateprefssavemy_key"my value"privateprefssavemy_other_key"my other key"run these commands to remove the test data when doneprivateprefsdeletemy_key
privateprefsdeletemy_other_keyLoad Valuefromprivateprefsimportloadmy_value=load("my_key")print(my_value)Return thevaluefor the givenkeyorNoneif the key doesn't exist.Load All Valuesfromprivateprefsimportload_keysmy_value_dict=load_keys()my_value=my_value_dict["my_key"]my_other_value=my_value_dict["my_other_key"]print(f"My Value:{my_value}")print(f"My Other Value:{my_other_value}")Return all saved key-value pairs as a dictionaryLoad Only Filtered Key Valuesfromprivateprefsimportload_keysfilter_keys=["my_key"]my_value_dict=load_keys(filter_keys)my_value=my_value_dict["my_key"]print(f"My Value:{my_value}")does_my_other_key_exist='my_other_key'inmy_value_dict.keys()print(f"does_my_other_key_exist:{does_my_other_key_exist}")Only returns key-value pairs that are in thefilter_keyslistLoad All Values as List of Tuplesfromprivateprefsimportload_keysmy_tuple_list=load_keys(return_as_list=True)formy_tupleinmy_tuple_list:print(f"Tuple:{my_tuple}")my_key=my_tuple[0]my_value=my_tuple[1]print(f"key:{my_key}")print(f"value:{my_value}\n")Return all saved key-value pairs as a dictionaryDelete Valuefromprivateprefsimportdeletedelete("my_key")Delete the value for a given keyDelete All Values-- WARNING ALL SAVED DATA WILL BE PERMANENTLY DELETED --fromprivateprefsimportdelete_all# don't run if you have saved data you want to keepdelete_all()Delete all saved/stored valuesPython Note:Note there are no functions to save values using Python; this is by design, by forcing
everyone to save values only using the CLI or editing the data.ini file ensures that
hardcoded values will never end up in version control repositories.But never say never. If you dig around in the package core, you can find the save functions,
but using the internal Python save functions means hard-coding sensitive data directly in
Python and defeating the purpose of this package.CLI DocsHelpprivateprefs-hDisplay the potential arguments and options to use.Saveprivateprefssavemy_key"My Value"Save a key-value pair to the data.ini file, quotes required if there are spaces in the string.Loadprivateprefsloadmy_keyLoad a value for the given key.Deleteprivateprefsdeletemy_keyDelete the value for the given key.Delete Allprivateprefsdelete_allDeletes all values.DataprivateprefsdataDisplay the data.ini file path and its contents as a data table.OpenprivateprefsopenOpen the data.ini file in the application associated with opening .ini file extension.Pre-Uninstallprivateprefspre_uninstallRemoves all files and folders created by this package.Licenseprivateprefsis distributed under the terms of theMITlicense.