Serialization for objects that TinyDB otherwise couldn't handle
Project description
tinydb-serialization provides serialization for objects that TinyDB otherwise couldn’t handle.
Usage
Creating a Serializer
In this example we implement a serializer for datetime objects:
from datetime import datetime
from tinydb_serialization import Serializer
class DateTimeSerializer(Serializer):
OBJ_CLASS = datetime # The class this serializer handles
def encode(self, obj):
return obj.strftime('%Y-%m-%dT%H:%M:%S')
def decode(self, s):
return datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
Using a Serializer
You can use your serializer like this:
>>> from tinydb import TinyDB
>>> from tinydb_serialization import SerializationMiddleware
>>> from tinydb import Query
>>>
>>> from datetime import datetime
>>>
>>>
>>> serialization = SerializationMiddleware()
>>> serialization.register_serializer(DateTimeSerializer(), 'TinyDate')
>>>
>>> db = TinyDB('db.json', storage=serialization)
>>> db.insert({'date': datetime(2000, 1, 1, 12, 0, 0)})
>>> db.all()
[{'date': datetime.datetime(2000, 1, 1, 12, 0)}]
>>> query = Query()
>>> db.insert({'date': datetime(2010, 1, 1, 12, 0, 0)})
>>> db.search(query.date > datetime(2005, 1, 1))
[{'date': datetime.datetime(2010, 1, 1, 12, 0)}]
For a more complexe example that provides better time support, check out issue #6.
Changelog
v2.0.0 (2020-05-26)
Add TinyDB v4.0.0 support (see pull request #9)
v1.0.4 (2017-03-27)
Don’t modify the original element if it contains a list (see pull request #5)
v1.0.3 (2016-02-11)
Handle nested data (nested dicts, lists) properly when serializing/deserializing (see pull request #3)
v1.0.2 (2016-01-04)
Don’t destroy original data when serializing (see pull request #2)
v1.0.1 (2015-11-17)
Fix installation via pip (see issue #1)
v1.0.0 (2015-09-27)
Initial release on PyPI
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 tinydb-serialization-2.0.0.tar.gz
.
File metadata
- Download URL: tinydb-serialization-2.0.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/5.3.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02d5ebc54652a38c52976658c1d25732112afb5ebbc4c0b730b3abef4b121b59 |
|
MD5 | 5ceb049a6ce435351b810802cc07f0ca |
|
BLAKE2b-256 | 2a7f224ab7ea292460129e205ab316f2e34ec140bbbbbd532ce44ff57b22014a |
File details
Details for the file tinydb_serialization-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: tinydb_serialization-2.0.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/5.3.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1bed979e1566452b51ea065942e1208ea751285064ebb56bbab52e2ffbf3100 |
|
MD5 | eb95cce82485d07cb329458870827907 |
|
BLAKE2b-256 | 408c63cca42a92821f5f3e29f77e494fdb13f1f425c08e208e16611b785b79c3 |