This code demonstrates how to interact with the client proxy using the RDFLib package:
-
from construct.proxy import proxy
-
from construct.constructservice import ServiceError
-
from rdflib import RDF, Namespace, Literal
-
from rdflib.Graph import ConjunctiveGraph
-
FOAF = Namespace(“http://xmlns.com/foaf/0.1/”)
-
exampleNS = Namespace(“http://www.example.com/”)
-
# Create a new Proxy object.
-
proxy = proxy()
-
print “Executing Script”
-
try:
-
# Generate a piece of FOAF RDF
-
store = ConjunctiveGraph()
-
store.bind(“foaf”, “http://xmlns.com/foaf/0.1/”)
-
store.add((exampleNS["~joebloggs"], RDF.type, FOAF["Person"]))
-
store.add((exampleNS["~joebloggs"], FOAF["name"], Literal(“Joe Bloggs”)))
-
store.add((exampleNS["~joebloggs"], FOAF["nick"], Literal(“joe”)))
-
store.add((exampleNS["~joebloggs"], FOAF["givenname"], Literal(“Joe”)))
-
store.add((exampleNS["~joebloggs"], FOAF["family_name"], Literal(“Bloggs”)))
-
data = store.serialize(format=“nt”)
-
# Send the FOAF RDF to the data store.
-
if proxy.insert(data):
-
print “The following data were added correctly:”
-
print data
-
else:
-
print “Problem encountered when adding the following data:”
-
print data
-
except ServiceError, e:
-
print e
-
# Close the proxy.
-
proxy.close()
If this code has run correctly the following should be printed:
Executing Script
The following data were added correctly:
<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/givenname> “Joe”.
<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/nick> “joe”.
<http://www.example.com/~joebloggs> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>.
<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/name> “Joe Bloggs”.
<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/family_name> “Bloggs”.
This post is the second part of a tutorial on construct and python. Back to Part 1 of this tutorial or on to Part 3.
![[del.icio.us]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/google.png)
![[StumbleUpon]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.construct-infrastructure.org/wp-content/plugins/bookmarkify/email.png)
0 Responses to “Construct and Python - Part 2”
Leave a Reply