Skip to main content

MongoDB-backed Python dict-like interface

Project description

So you are storing some key-values in a dict but your data became huge than your memory or you want to persist it on the disk? Then mongodict is for you!

As it uses MongoDB to store the data, you get all cool MongoDB things, like shardings and replicas. It uses the pickle module available on Python standard library to serialize/deserialize data and store everything as bson.Binary in MongoDB. You can also provide another codec (serializer/deserializer).

mongodict is tested under Python 2.7.5 and Python 3.3.2.

Installation

As simple as:

pip install mongodict

Usage

As it uses collections.MutableMapping as its base, you just need to change the line which creates your dict. For instace, just replace:

>>> my_dict = {}

with:

>>> from mongodict import MongoDict
>>> my_dict = MongoDict(host='localhost', port=27017, database='my_dict',
                        collection='store')

and then use it like a normal dict:

>>> my_dict['python'] = 'rules'
>>> print my_dict['python']
rules
>>> del my_dict['python']
>>> print my_dict['python']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mongodict.py", line 82, in __getitem__
    raise KeyError(key)
KeyError: u'python'
>>> my_dict['spam'] = 'eggs'
>>> my_dict['ham'] = 'damn'
>>> for key, value in my_dict.items():
...    print '{} = {}'.format(key, value)
...
spam = eggs
ham = damn

If you want to use another codec, you should pass serialize and deserialize functions to the class during the initialization. For example, to use JSON:

>>> import json
>>> json_dict = MongoDict(host='localhost', port=27017,
                          database='json_dict', collection='store',
                          codec=(json.dumps, json.loads))
>>> # use json_dict as usual

Enjoy! :-)

Authentication

If you want to use MongoDB’s authentication to the database MongoDict is connecting to, you just need to provide an auth parameter, as in this example:

from mongodict import MongoDict


my_dict = MongoDict(host='localhost', port=27017, database='mydb',
                    collection='mongodict',
                    auth=('my username', 'my password'))

Why not Redis?

Redis is “remote directory server” - it’s a great piece of software and can do the job if all your data fit on memory. By other side, MongoDB already have mature sharding and replica set features. So, if you need to store lots of key-value pairs that don’t fit on memory, mongodict can solve your problem.

Contributing

You can run the tests either with or without tox.

Without tox

This is the simplest approach: you’ll test only for one Python version. To do it, just execute:

mkvirtualenv --no-site-packages mongodict-without-tox
pip install -r requirements/develop.txt
make test

With tox

With tox you can test for more than one Python version (currently for 2.7 and 3.2). You just need to create a virtualenv, install and run it:

mkvirtualenv --no-site-packages tox-for-mongodict
pip install tox
tox

tox will create one virtualenv for each Python version, install requirements and then run the tests for each of them. Note that you need the python binaries available in your system (2.7 and 3.2) to run the tests.

Author

This software was written and is maintained by Álvaro Justen (aka Turicas). Please contact me at alvarojusten at gmail dot com.

License

It’s licensed under GPL version 3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongodict-0.3.1.tar.gz (18.3 kB view details)

Uploaded Source

File details

Details for the file mongodict-0.3.1.tar.gz.

File metadata

  • Download URL: mongodict-0.3.1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mongodict-0.3.1.tar.gz
Algorithm Hash digest
SHA256 9c66d65fd1cd3e1e03fed9d5c11c6e3603a10b2a60c9dbd703f58ca15d55655b
MD5 57f8bb308156c1891d790bb107eb4f9d
BLAKE2b-256 aa2764b9b903ed125a4c26dd9fc28921e90bfd3eca0bdce91ce8faac502e61e7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page