-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamend_headers.py
90 lines (61 loc) · 2.85 KB
/
amend_headers.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import csv
from settings import *
from collections import OrderedDict
## this actually shouldn't be an ordered dict
## because it's only ordered by day in the original
## and so should be sorted by numeric filing number
## before processing
infilepath = HEADER_DUMP_FILE
outfileheaders = ['filing_number', 'is_superseded', 'amended_by', 'last_amendment', 'report_number', 'filer_committee_id_number', 'form_type', 'date_signed', 'coverage_from_date', 'coverage_through_date', 'comment']
outfile = open(AMENDED_HEADER_FILE, 'w')
writer = csv.DictWriter(outfile, fieldnames=outfileheaders, extrasaction='ignore')
writer.writeheader()
filing_amendment_dict = {}
with open(infilepath, 'r') as infile:
dw = csv.DictReader(infile)
for i, row in enumerate(dw):
filing_number = row.get('filing_number', None)
this_filing = {
'amended_by':[],
'is_superseded':False,
'report_number':row['report_number'],
'filer_committee_id_number':row['filer_committee_id_number'],
'form_type':row['form_type'],
'date_signed':row['date_signed'],
'coverage_from_date':row['coverage_from_date'],
'coverage_through_date':row['coverage_through_date'],
'comment':row['comment']
}
amends = row.get('amends', None)
if amends:
#print("filing number %s amends %s" % (filing_number, amends))
this_filing['amends'] = amends
filing_amendment_dict[filing_number]=this_filing
# sort the filings
sorted_filings = OrderedDict(sorted(filing_amendment_dict.items(), key=lambda t: int(t[0])))
# save space? Maybe too late...
filing_amendment_dict = {}
for filing in sorted_filings:
amends = sorted_filings[filing].get('amends', None)
if amends:
try:
sorted_filings[amends]
except KeyError:
#print("original out of set, ignoring %s" % amends)
continue
#print("Marking original %s as amended by %s" % (amends, filing))
sorted_filings[amends]['is_superseded'] = True
sorted_filings[amends]['last_amendment'] = filing
prior_amendments = sorted_filings[amends]['amended_by']
if prior_amendments:
for prior_amendment in prior_amendments:
sorted_filings[prior_amendment]['is_superseded'] = True
sorted_filings[prior_amendment]['last_amendment'] = filing
prior_amendment_amended_by_list = sorted_filings[prior_amendment]['amended_by']
if filing not in prior_amendment_amended_by_list:
sorted_filings[prior_amendment]['amended_by'].append(filing)
sorted_filings[amends]['amended_by'].append(filing)
for filing in sorted_filings:
this_row = sorted_filings[filing]
this_row['filing_number'] = filing
writer.writerow(this_row)