privateprefs
keeps 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 Contents
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.
import privateprefs as prefs
secret_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 to
- install
privateprefs
- use the command line to save/store sensitive data in key-value pairs.
- the data is stored in a data.ini file stored on your computer
- the data.ini is stored outside your project and won't get added to repos
- use python to load the values by calling the corresponding keys
This will become clear after you Install privateprefs
and follow along with the
Getting Started guide below.
Use pip
to install
pip install privateprefs
privateprefs save "secret_api_key" "mfnc80imW4RawjYwVLsArx"
This saves/sets the value "mfnc80imW4RawjYwVLsArx"
to the key "secret_api_key"
import privateprefs as prefs
prefs.load("secret_api_key") # "mfnc80imW4RawjYwVLsArx"
This loads/gets the value "mfnc80imW4RawjYwVLsArx"
from the key "secret_api_key"
privateprefs data
key-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.
privateprefs open
This 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.
We can add, remove, update, and delete key-value pairs by directly editing the data.ini file.
Let's add the key my_email
with a value of darren@spammed.com
to the file manually.
For 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 package
- The first line must contain
[privateprefs]
- the next lines must be
my_key = my value
pairs - don't add quotes to strings like
my_key = "my value"
(don't do this!) - it's best to use the same name rules for keys as with Python variables name
privateprefs delete "secret_api_key"
# don't run if you have saved data you want to keep
privateprefs delete_all
You now have a basic understanding of how privateprefs
works 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.
The following Python code assumes we have the following two saved key-value pairs
privateprefs save my_key "my value"
privateprefs save my_other_key "my other key"
privateprefs delete my_key
privateprefs delete my_other_key
from privateprefs import load
my_value = load("my_key")
print(my_value)
Return the value
for the given key
or None
if the key doesn't exist.
from privateprefs import load_keys
my_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 dictionary
from privateprefs import load_keys
filter_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' in my_value_dict.keys()
print(f"does_my_other_key_exist: {does_my_other_key_exist}")
Only returns key-value pairs that are in the filter_keys
list
from privateprefs import load_keys
my_tuple_list = load_keys(return_as_list=True)
for my_tuple in my_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 dictionary
from privateprefs import delete
delete("my_key")
Delete the value for a given key
from privateprefs import delete_all
# don't run if you have saved data you want to keep
delete_all()
Delete all saved/stored values
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.
privateprefs -h
Display the potential arguments and options to use.
privateprefs save my_key "My Value"
Save a key-value pair to the data.ini file, quotes required if there are spaces in the string.
privateprefs load my_key
Load a value for the given key.
privateprefs delete my_key
Delete the value for the given key.
privateprefs delete_all
Deletes all values.
privateprefs data
Display the data.ini file path and its contents as a data table.
privateprefs open
Open the data.ini file in the application associated with opening .ini file extension.
privateprefs pre_uninstall
Removes all files and folders created by this package.
privateprefs
is distributed under the terms of the MIT license.