Skip to main content

Minimalistic ORM for JSON/YAML/Pickle file based DB

Project description

https://badge.fury.io/py/labml_db.svg https://pepy.tech/badge/labml_db

LabML DB

LabML DB is a minimalistic ORM database that uses JSON, YAML or Pickle files. It uses Python typehints as much as possible to help with static checking, and IDE features like autocompletion.

You can install this package using PIP.

pip install labml_db

Example

from labml_db import Model, Index


class Project(Model['Project']):
    name: str
    experiments: int

    @classmethod
    def defaults(cls):
        return dict(name='', experiments=0)


class User(Model['User']):
    name: str
    projects: List[Key[Project]]
    # This is an optional property, will automatically default to None
    occupation: Optional[str]

    @classmethod
    def defaults(cls):
        # Name is not in defaults and not optional.
        # It will be considered a required property
        return dict(projects=[])


class UsernameIndex(Index['User']):
    pass

You can configure it to use JSON/YAML/Pickle files

Model.set_db_drivers([
    FileDbDriver(PickleSerializer(), User, Path('./data/user')),
    FileDbDriver(YamlSerializer(), Project, Path('./data/project'))
])
Index.set_db_drivers([
    FileIndexDbDriver(JsonSerializer(), UsernameIndex, Path('./data/UserNameIndex.yaml'))
])

You can user get_all and Key.load to retrieve models, and save to save models.

proj = Project(name='nlp')
user = User(name='John')
user.projects.append(proj.key)
user.occupation = 'test'
user.save()
proj.save()

keys = User.get_all()
print([k.load() for k in keys])
keys = Project.get_all()
print([k.load() for k in keys])

And index is a hash-map between strings and keys.

user_key = UsernameIndex.get('John')
if not user_key:
    user = User(name='John')
    user.save()
    UsernameIndex.set(user.name, user.key)
else:
    print(user_key.load())

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

labml_db-0.0.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

labml_db-0.0.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file labml_db-0.0.3.tar.gz.

File metadata

  • Download URL: labml_db-0.0.3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for labml_db-0.0.3.tar.gz
Algorithm Hash digest
SHA256 38ea4db10e0d5b6a038eebc43210d3ecdc0414842798a6c1de2196dcbb10c343
MD5 78b6c1c68277ea904ec9cb51d2302f6a
BLAKE2b-256 f9da4a10f1da2ad1a18c8339b212dea27b0fde072412acc48f7c73849dba1139

See more details on using hashes here.

File details

Details for the file labml_db-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: labml_db-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for labml_db-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 993b79150a700973c2b52d2690c913447636f8162744c5197af7ce09181baaac
MD5 6639c58333d3aaaa0acbde7a2aa12c65
BLAKE2b-256 abea8b21d78d7d9a1443bf898a248b25f5a87a7ccc8cd97758e2c4fb60f8b8c6

See more details on using hashes here.

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