Skip to main content

A tiny dictionary-like file storage system.

Project description

peridata

peridata is a simple, tiny (<5KiB!) data storage system using JSON and files, meant to be easy to use, strict on typing, and to allow easy migrations via changing properties.

usage

Create a property dictionary:

from peridata import Property, PersistentStorage

epic_property_dict = {"nuclear_bombs": Property[int](default=1)}

Then, create a PersistentStorage instance:

ps = PersistentStorage(epic_property_dict, Path(".") / "data" / "makesurethisisafileloc.json")

You can then use it like a dictionary; however, adding new keys will error, and typing is checked:

print(ps["nuclear_bombs"]) # Output: 1 (since it's the default set earlier)
# Wait, we're British. That's it, we're invading France.
ps["nuclear_bombs"] -= 1
print(ps["nuclear_bombs"]) # Output: 0
# I'm gonna make a complex amount of nuclear bombs.
ps["nuclear_bombs"] = 2+1j # TypeError: Invalid type for property 'nuclear_bombs'. Expected int, got complex
# Fine, I'll try to store my tea in here, then.
ps["tea"] = Tea(Tea.Brand.YORKSHIRE, quantity=10000).serialize() # KeyError: 'Invalid property: tea'

The data is automatically saved to the file location:

// in data/makesurethisisafileloc.json
{
  "nuclear_bombs": 0
}

And, loading the PersistentStorage again will take that instead of the default:

epic_property_dict = {"nuclear_bombs": Property[int](default=1)} # The properties stay the same.
ps = PersistentStorage(epic_property_dict, Path(".") / "data" / "makesurethisisafileloc.json") # The file location is also the same.
print(ps["nuclear_bombs"]) # Output: 0

You can also protect data; then, if you use write_unprivileged, you will get an error on writing:

epic_property_dict = {"nuclear_bombs": Property[int](default=1),
                      "tea": Property[str](default=Tea(Tea.Brand.YORKSHIRE, quantity=1).serialize(), protected=True)} # The library will manage setting new dictionary values, if you add them.
ps = PersistentStorage(epic_property_dict, Path(".") / "data" / "makesurethisisafileloc.json") # The file location still the same.
ps.write_unprivileged("nuclear_bombs", 100) # This works, since by default protected=False.
# I'm gonna annoy this brit so hard.
ps.write_unprivileged("tea", Coffee().serialize()) # PermissionError: The property 'tea' is protected and cannot be modified from write_unprivileged().

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

peridata-1.3.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peridata-1.3.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file peridata-1.3.0.tar.gz.

File metadata

  • Download URL: peridata-1.3.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for peridata-1.3.0.tar.gz
Algorithm Hash digest
SHA256 b2bc2094bf7f2a5a545e95c020db0390114553382b345e188005d9fe984eac09
MD5 be5d85c08b4b2e776c07867496732c63
BLAKE2b-256 0533e4af816cf7f623c29db05f1c4184d881d0eb4356db0ced2655ae3b4e64b5

See more details on using hashes here.

File details

Details for the file peridata-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: peridata-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for peridata-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22f92d6f2cbfb4629d37a62664dc0f65d1d1427c69c0bfeecd3854ecf6530455
MD5 b80814a6333e43bbe659b0b2b6eb2de6
BLAKE2b-256 c0cc92f20bbd3644694b0cbdb4e25fc29afa248a94283c0414be77f8e0ca2166

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page