Skip to main content

Shared data for Python

Project description

Shared

Shared is a Python library to store, expose, read, and edit collections of data.

Expose your data:

from shared import Shared

shared = Shared("my-store")
shared.dict = {"author": "alexrustic", "year": 2020}
shared.save()

Read data:

from shared import Shared

shared = Shared("my-store", readonly=True)
print(shared.dict)  # {"author": "alexrustic", "year": 2020}

Edit data:

from shared import Shared

shared = Shared("my-store")
shared.dict["year"] = 2021
shared.save()

Access data from the command line:

$ shared "my-store" dict
{"author": "alexrustic", "year": 2021}

Dict is just one of the available collections of data:

from shared import Shared

shared = Shared("my-store")
# list
shared.list.append("hello")
shared.list.append("friend")
# set
shared.set = {"red", "blue"}

shared.save()

And yes, you can read the content from the command line interface:

$ shared "my-store" list
["hello", "friend"]

$ shared "my-store" set
{"red", "blue"}

But wait... can you store binary data with Shared ?

Yes, you can:

from shared import Shared

shared = Shared("my-store")
with open("/home/alex/selfie.png", "rb") as file:
    data = file.read()
    shared.add_bin("selfie", data)

# binary data are available in the 'bin' collection
for name, path in shared.bin.items():
    print(name, path)

From the command line, you can copy this binary data into an arbitrary file:

$ shared "my-store" bin "selfie" > "/home/alex/new.png"

Or you can do the same thing but in the opposite way: store the binary data from the command line

$ shared "my-store" bin "selfie" "/home/alex/selfie.png"

Then programmatically copy this binary data into an arbitrary file:

from shared import Shared
from shutil import copyfile

shared = Shared("my-store")
src = shared.bin["selfie"]
dst = "/home/alex/new.png"
# copy the content from src to dst
copyfile(src, dst)

Do you care about the space available on your hard drive ? Well you can easily delete the binary data:

from shared import Shared

shared = Shared("my-store")
# delete only the 'selfie' binary data
shared.del_bin("selfie")
# or just clear binary data
shared.del_bin()

You can decide to be a badass and delete the whole store:

from shared import Shared

shared = Shared("my-store")
shared.del_store()

Do you like this library ? Guess what, it's available on PyPI:

pip install shared

No dependency... No voodoo magic... Be curious, explore the source code to discover the rest ;)

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

shared-0.0.2.tar.gz (7.4 kB view hashes)

Uploaded Source

Built Distribution

shared-0.0.2-py3-none-any.whl (7.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