-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdataset.py
More file actions
77 lines (55 loc) · 1.88 KB
/
dataset.py
File metadata and controls
77 lines (55 loc) · 1.88 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
'''
This file saves the provided dataset(s) into a series of nosql documents,
using the mongodb framework.
'''
from brain.database.query import NoSQL
class Collection:
'''
This class provides an interface to retrieve, and store various parameters,
of the specified collection, from the mongodb.
Note: this class is invoked within 'base_data.py'
'''
def __init__(self):
'''
This constructor is responsible for defining class variables.
'''
self.list_error = []
self.nosql = NoSQL()
def query(self, collection, operation, payload=None):
'''
This method executes a query, with respect to the desired 'operation'.
@operation, the type of operation:
- insert_one
- insert_many
- update_one
- update_many
- find
- map_reduce
- delete_one
- delete_many
- count_documents
- drop_collection
'''
# execute query
if operation == 'drop_collection':
self.nosql.connect()
response = self.nosql.execute(operation, collection)
elif operation == 'count_documents':
self.nosql.connect(collection)
response = self.nosql.execute(operation, payload)
response['result'] = int(response['result'] or 0)
else:
self.nosql.connect(collection)
response = self.nosql.execute(operation, payload)
# retrieve any error(s)
response_error = self.nosql.get_errors()
# return result
if response_error:
return {
'status': False,
'result': response['result'],
'error': response_error
}
else:
return {'status': True, 'result': response['result'], 'error': None}