-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipy_init.py
64 lines (47 loc) · 1.66 KB
/
ipy_init.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
import sys
from pymongo import MongoClient
from adios_db.util.db_connection import connect_mongodb
from adios_db.data_sources.oil.estimations import OilEstimation
from pprint import PrettyPrinter
pp = PrettyPrinter(indent=2, width=120)
client = connect_mongodb({'mongodb.host': 'localhost',
'mongodb.port': 27017,
'mongodb.database': 'oil_database',
'mongodb.alias': 'oil-db-app'})
db = client.oil_database
records = db.imported_record
oils = db.oil
def deep_get(obj, attr_path, default=None):
if isinstance(attr_path, str):
attr_path = attr_path.split('.')
attrs, current = attr_path, obj
try:
for p in attrs:
current = current[p]
return current
except KeyError:
return default
def to_field(value):
'''
Some fields, like the reference, contain newlines. This messes up
our attempt at parsing a single row per record because a newline is
our row delimiter.
'''
try:
return ' '.join(value.split('\n'))
except Exception:
return value
for attr in ('ID', 'Name', 'Reference', 'Sample Date', 'Location', 'Product Type', 'Labels', 'API'):
sys.stdout.write(f'{attr}\t');
sys.stdout.write('\n');
for o in oils.find():
sys.stdout.write(f'{o["_id"]}\t')
for attr in ('name', 'reference.reference', 'sample_date', 'location',
'product_type', 'labels', 'API'):
v = deep_get(o['metadata'], attr)
if v is None:
sys.stdout.write('\t')
else:
v = to_field(v)
sys.stdout.write(f'{v}\t')
sys.stdout.write('\n');