Skip to main content

Small utility library to provide easy access to files and key-value store in an application directory

Project description

Python App Data

The py-appdata aims to solve the issue of storing simple application data and accessing it in an easy way.

The core idea is to expose a simple interface that has two properties

  • A get/set interface to simple values
  • A simple way of saving and retrieving files from the application directory

Stores

The AppData class provides an interface to two storage-abstractions: A file system and a key-value store. This lets you easily save/retrieve files as well as setting/loading key/values which is a common app-data usage.

Get started

from appdata import appdata
from datetime import datetime, timezone

# defaults to app_data
appdata.set_file_store("my_app_data_dir")

# defaults to kv_store
appdata.set_key_value_store("my_store")

appdata["last_login"] = datetime.utcnow().replace(tzinfo=timezone.utc)

with appdata.write("some_file.txt") as f:
    f.write("Mjello")

Auto save

The AppData object will auto save as a default. This means, whenever the KeyValue-store has been altered, it will automatically flush. This can be disabled, but then you are in charge of flushing. This can either be via the context manager or directly via the save method.

from appdata import appdata

appdata.set_auto_save(False)

with appdata:
    appdata["kv1"] = "something"
    appdata["kv2"] = "something else"
    
# Or directly

appdata["kv3"] = "something completely else"
appdata.save()

Dataclass support

If you like type hints, you can register a dataclass instance as a proxy for the key-value store

from appdata import appdata
from datetime import datetime, timezone
from dataclasses import dataclass


# Define the dataclass
@dataclass
class KV:
    last_login: datetime = None


# Create an object to be referenced and register it to appdata
kv = KV()
appdata.register(kv)


# The KeyValue object now acts as a proxy, and can be used throughout your project
kv.last_login = datetime.utcnow().replace(tzinfo=timezone.utc)

Custom KeyValue Store

You can create a custom KeyValue store by extending the KeyValueStore base class, or the DictKeyValueStore if you want to use a dict internally, and serialize it in a special way. Remember to registering it to the AppData object.

from appdata import appdata
from appdata.kv_store import KeyValueStore

class MyKeyValueStore(KeyValueStore):
    
    ...


# defaults to app_data
appdata.set_key_value_store(MyKeyValueStore())

Custom File Store

You can create a custom file store by extending the FileStore class and registering it to the AppData object

from appdata import appdata
from appdata.file_store import FileStore

class MyFileStore(FileStore):
    
    ...


# defaults to app_data
appdata.set_file_store(MyFileStore())

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

py-appdata-0.0.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

py_appdata-0.0.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file py-appdata-0.0.4.tar.gz.

File metadata

  • Download URL: py-appdata-0.0.4.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for py-appdata-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f68152e13127951bb38a0bc3a9e0559e62db653dc22c1c79f0869d2f3b72a9c2
MD5 1a7043c673e62f544dd69b157ab89f57
BLAKE2b-256 9320beb6160ee4e77b733cbf15bbcde8429ab7847531b543618c754a6660946a

See more details on using hashes here.

File details

Details for the file py_appdata-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: py_appdata-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.13 Linux/5.13.0-1031-azure

File hashes

Hashes for py_appdata-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f7fd43d38374adc0d8a8d3930f8ce5a908affb3699b0d29099a2c1a5428b8535
MD5 3509cf4cfd02d69a1dbb9f3da9756e1c
BLAKE2b-256 8a71b4dd5b3c65f6934a27bdc2fe91eb44b5341fc1ed9e5a3e06bd4996355957

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