Skip to main content

PyMDict

https://badge.fury.io/py/pymdict.svg https://travis-ci.org/ipazc/pymdict.svg?branch=master https://coveralls.io/repos/github/ipazc/pymdict/badge.svg?branch=master

Advanced Python Mongo Dict. A Python dictionary based on a MongoDB. It allows to treat a collection as a dictionary in Python, with extensive capabilities, like allowing basic queries, bulk operations and versioning (forks).

INSTALLATION

Currently it is only supported Python3.4 onwards. It can be installed through pip:

$ pip3 install pymdict

USAGE

In order to run, a MongoDB server is required.

A Mongo-based dictionary can be instantiated as follows:

>>> from pymdict.mongo_dict import MongoDict
>>>
>>> m = MongoDict("custom_id", mongo_host="localhost", mongo_port=27017)

Once m is instantiated, it can be used as a normal dictionary.

>>> m["key"] = "value"
>>> m[44] = "value2"
>>> m["key2"] = "value3"
>>> m["number"] = 44

>>> print(list(m.keys()))
["key", 44, "key2, "number"]

>>> for key, value in m.items():
...     print("{}: {}".format(key, value))
key: value
44: value2
key2: value3
number: 44

In addition, there are advanced functionalities like queries:

>>> for key, value, _ in m('key % ey'):
...     print("{}: {}".format(key, value))
key: value
key2: value3

>>> for key, value, _ in m('key % ey or value = 44'):
...     print("{}: {}".format(key, value))
key: value
key2: value3
number: 44

Queries also support to query with sub-dict elements:

>>> m["first"] = {"example": 44}
>>> m["second"] = {"example": 45}
>>> m["third"] = {"example": 46}

>>> for key, value, _ in m('value.example > 44 and value.example < 46'):
...     print("{}: {}".format(key, value))
second: 45

(TODO: Check the wiki page for more information about the query syntax)

Note that all the stores and removals are stored within a MongoDB. This means for each addition,edit and removal there is at least one connection to the MongoDB backend. In order to optimize it, a bulk operation can be used to wrap such amount of operations in a single connection:

>>> with m.bulk(buffer_size=100) as m:
...     for x in range(2000):
...         m["key{}".format(x)] = {"example": x}

Also, a mongo dict can be forked without the need to copy its content. This is specially useful if the target dict is extremely big and a copy is wanted. Note that a fork is an immediate process, and it allows to override or remove elements without modifying an original dictionary. It is achieved by applying a versioning technique with the dictionaries and it is still in an experimental state.

(TODO: More information about forking and versioning in the wiki page)

>>> m['foo'] = "bar"
>>> fork = m.fork()
>>> print(fork['foo'])
bar
>>> fork['foo'] = "foo"
>>> print(fork['foo'], m['foo'])
foo bar

Metadata

Release files for pymdict 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 pymdict 0.0.4
File Size Uploaded
pymdict-0.0.4.tar.gz 15.1 kB Details

Release files / pymdict-0.0.4.tar.gz

Download URL pymdict-0.0.4.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f7b5df2bacef92546aa4523516292d6d846e3a0c0206c6f2ede971e02ec6f889
BLAKE2b-256 checksum
How to use checksums
041938d5f8be87fef8c6a7233f2164957669afb39eae2590aabc4c03c2b3bb3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.4 This release

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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