Skip to main content

Easy-to-use database using dicts

Project description

DictDataBase

Downloads Downloads Downloads

DictDataBase is a simple but fast and secure database for handling dicts (or PathDicts for more advanced features), that uses json files as the underlying storage mechanism. It is also multiprocessind and multithreading safe, due to the employed locking mechanisms.

Import

import DictDataBase as DDB

Configuration

There are 3 configuration options. Set storage_directory to the path of the directory that will contain your database files:

DDB.config.storage_directory = "./ddb_storage" # Default value

If you want to use compressed files, set use_compression to True. This will make the db files significantly smaller and might improve performance if your disk is slow. However, the files will not be human readable.

DDB.config.use_compression = False # Default value

If you set pretty_json_files to True, the json db files will be indented and the keys will be sorted. It won't affect compressed files, since the are not human-readable anyways.

DDB.config.pretty_json_files = True # Default value

You can specify your own json encoder and decoder if you need to. The standard library json module is sufficient most of the time. However, alternatives like orjson might be more performant for your use case. The encoder function should take a dict and return a str or bytes. The decoder function should take a string and return a dict.

DDB.config.custom_json_encoder = None # Default value
DDB.config.custom_json_decoder = None # Default value

Create dicts

Before you can access dicts, you need to explicitly create them.

Do create ones that already exist, this would raise an exception. Also do not access ones that do not exist, this will also raise an exception.

user_data_dict = {
	"users": {
		"Ben": {
			"age": 30,
			"job": "Software Engineer"
		},
		"Sue": {
			"age": 21:
			"job": "Student"
		},
		"Joe": {
			"age": 50,
			"job": "Influencer"
		}
	},
	"follows": [["Ben", "Sue"], ["Joe", "Ben"]]
})
DDB.create("user_data", db=user_data_dict)
# There is now a file called user_data.json (or user_data.ddb if you use compression)
# in your specified storage directory.

Read dicts

d = DDB.read("user_data")
# You now have a copy of the dict named "user_data"
print(d == user_data_dict) # True

Write dicts

import DictDataBase as DDB
with DDB.session("user_data") as (session, user_data):
	# You now have a handle on the dict named "user_data"
	# Inside the with statement, the file of user_data will be locked, and no other
	# processes will be able to interfere.
	user_data["follows"].append(["Sue", "Ben"])
	session.write()
	# Now the changes to d are written to the database

print(DDB.read("user_data")["follows"])
# -> [["Ben", "Sue"], ["Joe", "Ben"], ["Sue", "Ben"]]

If you do not call session.write(), the database file will not be modified.

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

DictDataBase-1.2.0.tar.gz (8.3 kB view hashes)

Uploaded Source

Built Distribution

dictdatabase-1.2.0-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

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