forked from freelawproject/reporters-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
70 lines (58 loc) · 2.15 KB
/
utils.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
67
68
69
70
import json
import datetime
def suck_out_variations_only(reporters):
"""Builds a dictionary of variations to canonical reporters.
The dictionary takes the form of:
{
"A. 2d": ["A.2d"],
...
"P.R.": ["Pen. & W.", "P.R.R.", "P."],
}
In other words, it's a dictionary that maps each variation to a list of
reporters that it could be possibly referring to.
"""
variations_out = {}
for reporter_key, data_list in reporters.items():
# For each reporter key...
for data in data_list:
# For each book it maps to...
for variation_key, variation_value in data["variations"].items():
try:
variations_list = variations_out[variation_key]
if variation_value not in variations_list:
variations_list.append(variation_value)
except KeyError:
# The item wasn't there; add it.
variations_out[variation_key] = [variation_value]
return variations_out
def suck_out_editions(reporters):
"""Builds a dictionary mapping edition keys to their root name.
The dictionary takes the form of:
{
"A.": "A.",
"A.2d": "A.",
"A.3d": "A.",
"A.D.": "A.D.",
...
}
In other words, this lets you go from an edition match to its parent key.
"""
editions_out = {}
for reporter_key, data_list in reporters.items():
# For each reporter key...
for data in data_list:
# For each book it maps to...
for edition_key, edition_value in data["editions"].items():
try:
editions_out[edition_key]
except KeyError:
# The item wasn't there; add it.
editions_out[edition_key] = reporter_key
return editions_out
def print_json_with_dates(obj):
date_handler = lambda obj: (
obj.isoformat()
if isinstance(obj, datetime.datetime)
or isinstance(obj, datetime.date)
else None)
print json.dumps(obj, default=date_handler, sort_keys=True)