Seriattrs extension compatible with MongoDb
Project description
DbClass module for automatic serialization and deserialization
DbClass module is ment to be used in conjunction with databases or json which require serialization to save data to and deserialization to get data from.
Installation
You can install the db_class
package using pip:
pip install db_class
Example
Here's an example of how to use the simulate_game
function:
import json
import sys
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from src import DbClass
from src import DbClassLiteral
def test_serialize_literal():
@dataclass
class Bar(DbClassLiteral):
dictionary: dict
date: datetime
decimal: Decimal
@dataclass
class Foo(DbClass):
dictionary: dict
date: datetime
decimal: Decimal
bar: Bar
foo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))
serialized = foo.get_db_representation()
try:
json.dump(serialized, sys.stdout)
except:
assert False
deserialized = Foo.from_dict(serialized)
assert deserialized == foo
def test_serialize():
@dataclass
class Bar(DbClass):
dictionary: dict
date: datetime
decimal: Decimal
@dataclass
class Foo(DbClass):
dictionary: dict
date: datetime
decimal: Decimal
bar: Bar
foo = Foo({}, datetime.now(), Decimal(1), Bar({}, datetime.now(), Decimal(1)))
serialized = foo.get_db_representation()
foo.bar = foo.bar._id
try:
json.dump(serialized, sys.stdout)
except:
assert False
deserialized = Foo.from_dict(serialized)
assert deserialized == foo
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
Built Distribution
File details
Details for the file mongo-db-operator-0.0.3.tar.gz
.
File metadata
- Download URL: mongo-db-operator-0.0.3.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 398668945857316d620731ea40732b656463ca53159295d16dc7c52baf5a1ec4 |
|
MD5 | b79f6f09b5303694b2264b7ff904e3ad |
|
BLAKE2b-256 | 1082bc55f5b899a84fdfbcc3df510f8c07b785c747c65e8269bbb6ca9cfefc2d |
Provenance
File details
Details for the file mongo_db_operator-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: mongo_db_operator-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f85edea858639a128264bce63eeeea90d3086dbb167549a8e2ec989ef9a3605f |
|
MD5 | c9ea2d5248bebe510b48c20c33da5248 |
|
BLAKE2b-256 | 84011b033b174a77df6b02540c4e2cd6e4b2987673f62bbc5ed8efcf51f51c1b |