summaryrefslogtreecommitdiff
path: root/requests_cache/backends
diff options
context:
space:
mode:
authorRoman Haritonov <reclosedev@gmail.com>2016-08-18 22:27:17 +0300
committerGitHub <noreply@github.com>2016-08-18 22:27:17 +0300
commit8395378c5a9df5ebcb56ffcdf35fc6bfb97f9e82 (patch)
treed82617ae4483e38c91ac4e77b9faa5256b726c67 /requests_cache/backends
parent3d45cd801164270c1653c228fb71222108299a99 (diff)
parentac4e5abb7334e4b5b3771351bdc1af798564d7f8 (diff)
downloadrequests-cache-8395378c5a9df5ebcb56ffcdf35fc6bfb97f9e82.tar.gz
Merge pull request #72 from LeadSift/master
Added support for pymongo 3,
Diffstat (limited to 'requests_cache/backends')
-rw-r--r--requests_cache/backends/storage/mongodict.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/requests_cache/backends/storage/mongodict.py b/requests_cache/backends/storage/mongodict.py
index 172d9a0..e9d3484 100644
--- a/requests_cache/backends/storage/mongodict.py
+++ b/requests_cache/backends/storage/mongodict.py
@@ -6,13 +6,18 @@
Dictionary-like objects for saving large data sets to ``mongodb`` database
"""
+
from collections import MutableMapping
try:
import cPickle as pickle
except ImportError:
import pickle
-from pymongo import Connection
+# Use PyMongo 3 if present
+try:
+ from pymongo import MongoClient
+except AttributeError:
+ from pymongo import Connection as MongoClient
class MongoDict(MutableMapping):
@@ -30,7 +35,7 @@ class MongoDict(MutableMapping):
if connection is not None:
self.connection = connection
else:
- self.connection = Connection()
+ self.connection = MongoClient()
self.db = self.connection[db_name]
self.collection = self.db[collection_name]
@@ -45,7 +50,7 @@ class MongoDict(MutableMapping):
def __delitem__(self, key):
spec = {'_id': key}
- if self.collection.find_one(spec, fields=['_id']):
+ if self.collection.find_one(spec, {'_id': True}):
self.collection.remove(spec)
else:
raise KeyError
@@ -54,7 +59,7 @@ class MongoDict(MutableMapping):
return self.collection.count()
def __iter__(self):
- for d in self.collection.find(fields=['_id']):
+ for d in self.collection.find({}, {'_id': True}):
yield d['_id']
def clear(self):
@@ -63,7 +68,6 @@ class MongoDict(MutableMapping):
def __str__(self):
return str(dict(self.items()))
-
class MongoPickleDict(MongoDict):
""" Same as :class:`MongoDict`, but pickles values before saving
"""