Skip to main content

bson_extra

A package that provides timezone support and custom type handling functionality for MongoDB bson -> https://api.mongodb.com/python/current/api/bson/index.html

MongoDB bson is great - but it doesn't allow for certain custom types - specifically timezone support. This package rectifies that.

This package provides a new dumps and loads method that overrides the ~bson.json_util.dumps and ~bson.json_util.load functions in a few key places to support timezones - everything else works in the same way as in the core package.

Hooks are also provided so that you can handle custom type handling as necessary.

Installation

pip install bson_extra

Usage

bson_extra handle timezone aware and timezone naive datetimes.
bson_extra retains all existing functionality of ~bson.json_util.dumps - meaning you can provide json_options to configure the output as required. Hooks are also provided so that you can implement your own custom handling as required.

Dumping a timezone aware datetime

>>> from datetime import datetime
>>> from bson_extra import bson_extra
>>> import pytz

>>> timezone = pytz.timezone("America/New_York")
>>> dt = datetime(2020,6,20,12,30,45).astimezone(timezone)

>>> bson_extra.dumps(dt)
'{"$date": 1592652645000, "$zone": "America/New_York", "$offset": {"total_seconds": -14400.0}}'

Loading a timezone aware datetime

>>> from datetime import datetime
>>> from bson_extra import bson_extra

>>> dumped_data = '{"$date": 1592652645000, "$zone": "America/New_York", "$offset": {"total_seconds": -14400.0}}'
>>> bson_extra.loads(dumped_data)
datetime(2020, 6, 20, 7, 30, 45, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)

Dumping a timezone naive datetime

>>> from datetime import datetime
>>> from bson_extra import bson_extra

>>> dt = datetime(2020,6,20,12,30,45)

>>> bson_extra.dumps(dt)
'{"$date": 1592656245000, "$zone": null, "$offset": null}'

Loading a timezone naive datetime

>>> from datetime import datetime
>>> from bson_extra import bson_extra

>>> dumped_data = '{"$date": 1592656245000, "$zone": null, "$offset": null}'
>>> bson_extra.loads(dumped_data)
datetime(2020, 6, 20, 7, 30, 45)

Handling custom types

What if you want to handle a specific type that isn't currently supported by bson?
bson_extra supplies some hooks to enable you to do this easily by subclassing BsonExtra

>>> from bson_extra import BsonExtra
>>> class CustomBsonExtra(BsonExtra):
>>>     def dump_object_hook(self, obj, json_options):
>>>         if isinstance(obj, int):
>>>             # By default `bson`  dumps integers as a string
>>>             result = {"$integer": obj}
>>>         else:
>>>             result = super(CustomBsonExtra, self).dump_object_hook(obj, json_options)
>>>         return result
>>>
>>>     def load_object_hook(self, dct, *args, **kwargs):
>>>         result = super(CustomBsonExtra, self).load_object_hook(dct, *args, **kwargs)
>>>         if "$integer" in dct:
>>>              return int(dct['$integer'])
>>>         return result
>>>
>>> # By default `bson` dumps integers as strings
>>> bson_extra = CustomBsonExtra()
>>> bson_extra.dumps(1)
>>> '{"$integer": 1}'
>>>
>>> bson_extra.loads('{"$integer": 1}')
>>> 1

Running the tests

python setup.py test

Metadata

Release files for bson-extra 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bson-extra 0.0.4
File Size Uploaded Provenance
bson_extra-0.0.4.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bson-extra 0.0.4
File Interpreter ABI Platform
bson_extra-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / bson_extra-0.0.4.tar.gz

Download URL bson_extra-0.0.4.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8b801e9a5e26ecd63eb6af7058f10960efaba232bdc13b35c28380b99002eefe
BLAKE2b-256 checksum
How to use checksums
4b0da876e61b13ea3ae019cf96ffd858b22d27f780f17b334f429469ca5444ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Release files / bson_extra-0.0.4-py3-none-any.whl

Download URL bson_extra-0.0.4-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
971665075574f4412e5fcdc7f0a48e094150198749bf0d8e913b1cfa2ea61f24
BLAKE2b-256 checksum
How to use checksums
77c129aad8e5ef38ab44f959532214603e7150d37ca12d0b5b08a6775874b329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Security for bson-extra 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page