Minimalistic ORM for JSON/YAML/Pickle file based DB
Project description
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
Release history Release notifications | RSS feed
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.10.tar.gz
(75.1 kB
view details)
Built Distribution
labml_db-0.0.10-py3-none-any.whl
(116.3 kB
view details)
File details
Details for the file labml_db-0.0.10.tar.gz
.
File metadata
- Download URL: labml_db-0.0.10.tar.gz
- Upload date:
- Size: 75.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22a05ef25d7c9926912ed425e132c22c4687c40704bd1abf879ce9601dfec457 |
|
MD5 | b6744b6d1e96808b16413b7bb9cbf36b |
|
BLAKE2b-256 | d07fa6ed99b0002e18da7e1066f094ad42156e735e5f0996a2d629a75ec080f9 |
File details
Details for the file labml_db-0.0.10-py3-none-any.whl
.
File metadata
- Download URL: labml_db-0.0.10-py3-none-any.whl
- Upload date:
- Size: 116.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dd6510d7fcb502e20d20c426ec12529b1199e49c1695f9c1a5ec74a79703683 |
|
MD5 | d515e4df791b8d529d59e901d12160b9 |
|
BLAKE2b-256 | b57906a1bbd94aa9a69eb62bd4cb93443bf965535141c33da46b3bd9eb473f3f |