Construct and Python - Part 2

This code demonstrates how to interact with the client proxy using the RDFLib package:

  1. from construct.proxy import proxy
  2. from construct.constructservice import ServiceError
  3. from rdflib import RDF, Namespace, Literal
  4. from rdflib.Graph import ConjunctiveGraph
  5. FOAF = Namespace(“http://xmlns.com/foaf/0.1/”)
  6. exampleNS = Namespace(“http://www.example.com/”)
  7. # Create a new Proxy object.
  8. proxy = proxy()
  9. print “Executing Script”
  10. try:
  11. # Generate a piece of FOAF RDF
  12. store = ConjunctiveGraph()
  13. store.bind(“foaf”, “http://xmlns.com/foaf/0.1/”)
  14. store.add((exampleNS["~joebloggs"], RDF.type, FOAF["Person"]))
  15. store.add((exampleNS["~joebloggs"], FOAF["name"], Literal(“Joe Bloggs”)))
  16. store.add((exampleNS["~joebloggs"], FOAF["nick"], Literal(“joe”)))
  17. store.add((exampleNS["~joebloggs"], FOAF["givenname"], Literal(“Joe”)))
  18. store.add((exampleNS["~joebloggs"], FOAF["family_name"], Literal(“Bloggs”)))
  19. data = store.serialize(format=“nt”)
  20. # Send the FOAF RDF to the data store.
  21. if proxy.insert(data):
  22. print “The following data were added correctly:”
  23. print data
  24. else:
  25. print “Problem encountered when adding the following data:”
  26. print data
  27. except ServiceError, e:
  28. print e
  29. # Close the proxy.
  30. 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.

0 Responses to “Construct and Python - Part 2”


  1. No Comments
  1. 1 Construct and Python - Part 1 at Construct

Leave a Reply