-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathmongo.py
More file actions
51 lines (35 loc) · 1.17 KB
/
mongo.py
File metadata and controls
51 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import absolute_import, division, print_function
from .compatibility import basestring
from .compute.mongo import dispatch
try:
from pymongo.collection import Collection
from pymongo import ASCENDING
except ImportError:
Collection = type(None)
__all__ = ['create_index']
@dispatch(object)
def scrub_keys(o):
"""Add an ascending sort key when pass a string, to make the MongoDB
interface similar to SQL.
"""
raise NotImplementedError("scrub_keys not implemented for type %r" %
type(o).__name__)
@dispatch(basestring)
def scrub_keys(s):
return s, ASCENDING
@dispatch(tuple)
def scrub_keys(t):
return t
@dispatch(list)
def scrub_keys(seq):
for el in seq:
if not isinstance(el, (tuple, basestring)):
raise TypeError('indexing keys must be a string or pair of '
'(<column name>, <parameter>)')
yield scrub_keys(el)
@dispatch(Collection, basestring)
def create_index(coll, key, **kwargs):
coll.create_index(key, **kwargs)
@dispatch(Collection, list)
def create_index(coll, keys, **kwargs):
coll.create_index(list(scrub_keys(keys)), **kwargs)