-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathhubway.py
80 lines (65 loc) · 3.4 KB
/
hubway.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
import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
class hubway(dml.Algorithm):
contributor = 'cma4_lliu_saragl_tsuen'
reads = []
writes = ['cma4_lliu_saragl_tsuen.hubway']
@staticmethod
def execute(trial = False):
'''Retrieve some data sets (not using the API here for the sake of simplicity).'''
startTime = datetime.datetime.now()
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('cma4_lliu_saragl_tsuen', 'cma4_lliu_saragl_tsuen')
url = 'https://secure.thehubway.com/data/stations.json'
response = urllib.request.urlopen(url).read().decode("utf-8")
r = json.loads(response)
stations = r["stations"]
s = json.dumps(r, sort_keys=True, indent=2)
repo.dropCollection("cma4_lliu_saragl_tsuen.hubway")
repo.createCollection("cma4_lliu_saragl_tsuen.hubway")
repo['cma4_lliu_saragl_tsuen.hubway'].insert_many(stations)
repo['cma4_lliu_saragl_tsuen.hubway'].metadata({'complete':True})
print(repo['cma4_lliu_saragl_tsuen.hubway'].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('cma4_lliu_saragl_tsuen', 'cma4_lliu_saragl_tsuen')
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('hub', 'https://secure.thehubway.com/data/')
this_script = doc.agent('alg:cma4_lliu_saragl_tsuen#hubway', {prov.model.PROV_TYPE:prov.model.PROV['SoftwareAgent'], 'ont:Extension':'py'})
resource = doc.entity('dat:hubway', {'prov:label':'Hubway Station Data', prov.model.PROV_TYPE:'ont:DataResource', 'ont:Extension':'json'})
get_stations = doc.activity('log:uuid'+str(uuid.uuid4()), startTime, endTime)
doc.wasAssociatedWith(get_stations, this_script)
doc.usage(get_stations, resource, startTime, None,
{prov.model.PROV_TYPE:'ont:Retrieval'}
)
hubway = doc.entity('dat:cma4_lliu_saragl_tsuen#hubway', {prov.model.PROV_LABEL:'Hubway Locations', prov.model.PROV_TYPE:'ont:DataSet'})
doc.wasAttributedTo(hubway, this_script)
doc.wasGeneratedBy(hubway, get_stations, endTime)
doc.wasDerivedFrom(hubway, resource, get_stations, get_stations, get_stations)
repo.logout()
return doc
hubway.execute()
doc = hubway.provenance()
print(doc.get_provn())
print(json.dumps(json.loads(doc.serialize()), indent=4))
## eof