forked from PaddlePaddle/continuous_evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
66 lines (50 loc) · 1.7 KB
/
db.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import bson
from pymongo import MongoClient
class MongoDB(object):
def __init__(self, dbname, host='localhost', port=27017):
self.client = MongoClient(host, int(port))
self.db = getattr(self.client, dbname)
def table(self, table):
''' table might be a string or a Mongo table object. '''
if isinstance(table, str):
table = getattr(self.db, table)
return table
# def insert_one(self, table, key, json):
# key['_value'] = json
# self.table(table).insert_one(key)
def insert_one(self, table, record):
self.table(table).insert_one(record)
def remove(self, table, cond):
self.table(table).remove(cond)
def find_one(self, table, cond):
'''
Find one record.
cond: dic
something like {'author': 'Mike'}
'''
return self.table(table).find_one(cond)
def find_sections(self, table, cond, sections, key, limit=-1):
if limit == -1:
return self.table(table).find(cond, sections).sort(key)
else:
return self.table(table).find(cond, sections).sort(key).limit(limit)
def find(self, table, cond):
'''
Find records.
cond: dic
something like {'author': 'Mike'}
'''
return self.table(table).find(cond)
def finds(self, table, cond):
'''
Find records.
cond: dic
something like {'author': 'Mike'}
'''
return [r for r in self.table(table).find(cond)]
if __name__ == '__main__':
import _config
from pprint import pprint
db = MongoDB(_config.db_name)
records = db.finds(_config.table_name, {'type': 'kpi'})
pprint(records)