GistTree.Com
Entertainment at it's peak. The news is by your side.

Show HN: Minimalistic ORM for JSON/YAML/Pickle file based db

0

https://pepy.tech/badge/labml_db

LabML DB

LabML DB is a minimalistic ORM database that makes use of JSON, YAML or Predicament files.
It makes use of Python typehints as worthy as imaginable to support with static checking,
and IDE functions cherish autocompletion.

You would possibly perchance well well presumably install this kit the use of PIP.

Instance

from labml_db import Mannequin, Index


class Project(Mannequin['Project']):
    identify: str
    experiments: int

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


class Individual(Mannequin['User']):
    identify: str
    tasks: List[Key[Project]]
    # Here is an non-fundamental property, will automatically default to None
    occupation: Non-fundamental[str]

    @classmethod
    def defaults(cls):
        # Title will not be any longer in defaults and no longer non-fundamental.
        # This would possibly perchance well well be view a couple of required property
        return dict(tasks=[])


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

You would possibly perchance well well presumably configure it to use JSON/YAML/Predicament files

Mannequin.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 would possibly perchance well well presumably user get_all and Key.load to retrieve models, and assign to assign models.

proj = Project(identify='nlp')
user = Individual(identify='John')
user.tasks.append(proj.key)
user.occupation = 'test'
user.assign()
proj.assign()

keys = Individual.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-plot between strings and keys.

user_key = UsernameIndex.assemble('John')
if no longer user_key:
    user = Individual(identify='John')
    user.assign()
    UsernameIndex.situation(user.identify, user.key)
else:
    print(user_key.load())

Read More

Leave A Reply

Your email address will not be published.