-
Notifications
You must be signed in to change notification settings - Fork 0
/
non_registered_precincts.py
97 lines (79 loc) · 4.27 KB
/
non_registered_precincts.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
87
88
89
90
91
92
93
94
95
96
97
# writes to database
# not using bc by precinct
import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
class non_registered_precincts(dml.Algorithm):
contributor = 'carlosp_jpva_tkay_yllescas'
reads = []
writes = ['carlosp_jpva_tkay_yllescas.non_registered_precincts']
@staticmethod
def execute(trial=False):
print("non_registered_precincts")
'''Retrieve some data sets (without API).'''
startTime = datetime.datetime.now()
# Set up the database connection
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('carlosp_jpva_tkay_yllescas', 'carlosp_jpva_tkay_yllescas')
file = 'data/non_registered_Precinct.json'
with open(file, "r", encoding="utf8") as datafile:
json_string = datafile.read()
r = json.loads(json_string)
s = json.dumps(r, sort_keys=True, indent=2)
repo.dropCollection("non_registered_precincts")
repo.createCollection("non_registered_precincts")
repo['carlosp_jpva_tkay_yllescas.non_registered_precincts'].insert_many(r)
repo['carlosp_jpva_tkay_yllescas.non_registered_precincts'].metadata({'complete': True})
print(repo['carlosp_jpva_tkay_yllescas.non_registered_precincts'].metadata())
repo.logout()
endTime = datetime.datetime.now()
return {"start": startTime, "end": endTime}
@staticmethod
def provenance(doc=prov.model.ProvDocument(), startTime=None, endTime=None):
'''
Create the provenance document describing everything happening
in this script. Each run of the script will generate a new
document describing that invocation event.
'''
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('carlosp_jpva_tkay_yllescas', 'carlosp_jpva_tkay_yllescas')
doc.add_namespace('alg', 'http://datamechanics.io/algorithm/') # The scripts are in <folder>#<filename> format.
doc.add_namespace('dat', 'http://datamechanics.io/data/') # The data sets are in <user>#<collection> format.
doc.add_namespace('ont',
'http://datamechanics.io/ontology#') # 'Extension', 'DataResource', 'DataSet', 'Retrieval', 'Query', or 'Computation'.
doc.add_namespace('log', 'http://datamechanics.io/log/') # The event log.
doc.add_namespace('bdp', 'https://data.cityofboston.gov/resource/')
this_script = doc.agent('alg:carlosp_jpva_tkay_yllescas#non_registered_precincts',
{prov.model.PROV_TYPE: prov.model.PROV['SoftwareAgent'], 'ont:Extension': 'py'})
resource = doc.entity('bdp:wc8w-nujj',
{'prov:label': '311, Service Requests', prov.model.PROV_TYPE: 'ont:DataResource',
'ont:Extension': 'json'})
get_non_registered_precincts = doc.activity('log:uuid' + str(uuid.uuid4()), startTime, endTime)
doc.wasAssociatedWith(get_non_registered_precincts, this_script)
doc.usage(get_non_registered_precincts, resource, startTime, None,
{prov.model.PROV_TYPE: 'ont:Retrieval',
'ont:Query': '?type=non_registered_precincts&$select=type,latitude,longitude,OPEN_DT'
}
)
nonRegisteredPrecincts = doc.entity('dat:carlosp_jpva_tkay_yllescas#non_registered_precincts',
{prov.model.PROV_LABEL: 'Registered Voters by Precinct', prov.model.PROV_TYPE: 'ont:DataSet'})
doc.wasAttributedTo(nonRegisteredPrecincts, this_script)
doc.wasGeneratedBy(nonRegisteredPrecincts, get_non_registered_precincts, endTime)
doc.wasDerivedFrom(nonRegisteredPrecincts, resource, get_non_registered_precincts, get_non_registered_precincts, get_non_registered_precincts)
repo.logout()
return doc
'''
# This is example code you might use for debugging this module.
# Please remove all top-level function calls before submitting.
example.execute()
doc = example.provenance()
print(doc.get_provn())
print(json.dumps(json.loads(doc.serialize()), indent=4))
'''
## eof