Skip to main content

Minimalistic ORM for JSON/YAML/Pickle file based/redis/mongo 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.15.tar.gz (9.1 kB view hashes)

Uploaded Source

Built Distribution

labml_db-0.0.15-py3-none-any.whl (13.3 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