Debian Codegraph @

python/example.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # example.py - Redland Python 2.0 example code
4 #
5 # Copyright (C) 2000-2005 David Beckett - http://www.dajobe.org/
6 # Copyright (C) 2000-2005 University of Bristol - http://www.bristol.ac.uk/
7 #
8 # This package is Free Software and part of Redland http://librdf.org/
9 #
10 # It is licensed under the following three licenses as alternatives:
11 # 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12 # 2. GNU General Public License (GPL) V2 or any newer version
13 # 3. Apache License, V2.0 or any newer version
14 #
15 # You may not use this file except in compliance with at least one of
16 # the above three licenses.
17 #
18 # See LICENSE.html or LICENSE.txt at the top of this package for the
19 # full license terms.
20 #
21 #
22
23 import RDF
24
25 storage=RDF.Storage(storage_name="hashes",
26 name="test",
27 options_string="new='yes',hash-type='memory',dir='.'")
28 if storage is None:
29 raise Exception("new RDF.Storage failed")
30
31 #RDF.debug(1)
32
33 model=RDF.Model(storage)
34 if model is None:
35 raise Exception("new RDF.model failed")
36
37 statement=RDF.Statement(RDF.Uri("http://www.dajobe.org/"),
38 RDF.Uri("http://purl.org/dc/elements/1.1/creator"),
39 RDF.Node("Dave Beckett"))
40 if statement is None:
41 raise Exception("new RDF.Statement failed")
42
43 model.add_statement(statement)
44
45 # Match against an empty statement - find everything
46 for s in model.find_statements(RDF.Statement()):
47 print "found statement:",s
48
49 test_file='../data/dc.rdf'
50
51 print "Parsing URI (file)", test_file
52 uri=RDF.Uri(string="file:"+test_file)
53
54 parser=RDF.Parser('raptor')
55 if parser is None:
56 raise Exception("Failed to create RDF.Parser raptor")
57
58 count=0
59 for s in parser.parse_as_stream(uri,uri):
60 model.add_statement(s)
61 count=count+1
62
63 print "Parsing added",count,"statements"
64
65 print "Printing all statements"
66 for s in model.as_stream():
67 print "Statement:",s
68
69 q = RDF.Query("PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c WHERE {?a dc:title ?c}")
70 print "Querying for dc:titles:"
71 for result in q.execute(model):
72 print "{"
73 for k in result:
74 print " "+k+" = "+str(result[k])
75 print "}"
76
77 print "Writing model to test-out.rdf as rdf/xml"
78
79 # Use any rdf/xml parser that is available
80 serializer=RDF.Serializer()
81 serializer.set_namespace("dc", RDF.Uri("http://purl.org/dc/elements/1.1/"))
82 serializer.serialize_model_to_file("test-out.rdf", model)
83
84 print "Serialized to ntriples as a string size",len(model.to_string(name="ntriples", base_uri="http://example.org/base#")),"bytes"
85
86 print "Done"