Created
May 1, 2012 22:35
-
-
Save jmandel/2571997 to your computer and use it in GitHub Desktop.
MedicationList REST App Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client = get_smart_client(smart_oauth_header) | |
# Represent the list as an RDF graph | |
med_lists = client.records_X_medication_lists_GET() | |
lists_q = """ | |
PREFIX dcterms:<http://purl.org/dc/terms/> | |
PREFIX sp:<http://smartplatforms.org/terms#> | |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
select ?l ?s ?sname ?d | |
where { | |
?l a sp:MedicationList. | |
?l sp:medListSource ?s. | |
?s dcterms:title ?sname. | |
optional { ?l dcterms:date ?d } | |
}""" | |
lists = med_lists.graph.query(lists_q) | |
for l in lists: print l | |
def one_list(list_uri): | |
one_list_q = """ | |
PREFIX dcterms:<http://purl.org/dc/terms/> | |
PREFIX sp:<http://smartplatforms.org/terms#> | |
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
SELECT ?med ?name ?quantx ?quant ?quantunit ?freqx ?freq ?frequnit ?inst ?startdate | |
WHERE { | |
""" + list_uri.n3() + """ sp:medication ?med. | |
?med rdf:type sp:Medication . | |
?med sp:drugName ?medc. | |
?medc dcterms:title ?name. | |
?med sp:instructions ?inst. | |
?med sp:quantity ?quantx. | |
OPTIONAL { | |
?quantx sp:value ?quant. | |
?quantx sp:unit ?quantunit. } | |
?med sp:frequency ?freqx. | |
OPTIONAL { | |
?freqx sp:value ?freq. | |
?freqx sp:unit ?frequnit. } | |
?med sp:startDate ?startdate. | |
} | |
""" | |
return med_lists.graph.query(one_list_q) | |
for l in lists: | |
print "\nList " + l[0] | |
for med in one_list(l[0]): | |
print med | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment