Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transition from 2to3 setup code to use of six.py as py2/3 compatibility layer #374

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ exclude_lines =
[paths]
source =
rdflib/
build/src/rdflib/
tests =
test/
build/src/test/
examples =
examples/
build/src/examples/
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ before_script:
- if [[ ${TRAVIS_PYTHON_VERSION%%.*} == '2' ]]; then flake8 --exclude=pyRdfa,extras,host,transform,rdfs,sparql,results,pyMicrodata --exit-zero rdflib; fi

script:
# Must run the tests in build/src so python3 doesn't get confused and run
# the python2 code from the current directory instead of the installed
# 2to3 version in build/src.
- if [[ ${TRAVIS_PYTHON_VERSION%%.*} == '2' ]]; then nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib ; fi
- if [[ ${TRAVIS_PYTHON_VERSION%%.*} == '3' ]]; then nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=build/src/rdflib --where=./build/src; fi
- nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib

after_success:
- if [[ $HAS_COVERALLS ]] ; then coveralls ; fi
Expand Down
4 changes: 2 additions & 2 deletions examples/conjunctive_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@

# query the conjunction of all graphs

print 'Mary loves:'
print('Mary loves:')
for x in g[mary : ns.loves/ns.hasName]:
print x
print(x)
4 changes: 2 additions & 2 deletions examples/custom_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

l2=list(g2)[0][2]

print l2
print(l2)

print l2.value == c # back to a python complex object
print(l2.value == c) # back to a python complex object


2 changes: 1 addition & 1 deletion examples/custom_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def customEval(ctx, part):
# Find all FOAF Agents
for x in g.query(
'PREFIX foaf: <%s> SELECT * WHERE { ?s a foaf:Agent . }' % FOAF):
print x
print(x)
4 changes: 2 additions & 2 deletions examples/foafpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

tim = URIRef("http://www.w3.org/People/Berners-Lee/card#i")

print "Timbl knows:"
print("Timbl knows:")

for o in g.objects(tim, FOAF.knows / FOAF.name):
print o
print(o)
2 changes: 1 addition & 1 deletion examples/prepared_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
tim = rdflib.URIRef("http://www.w3.org/People/Berners-Lee/card#i")

for row in g.query(q, initBindings={'person': tim}):
print row
print(row)
4 changes: 2 additions & 2 deletions examples/rdfa_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

g.parse('http://www.worldcat.org/title/library-of-babel/oclc/44089369', format='rdfa')

print "Books found:"
print("Books found:")

for row in g.query("""SELECT ?title ?author WHERE {
[ a schema:Book ;
schema:author [ rdfs:label ?author ] ;
schema:name ?title ]
FILTER (LANG(?title) = 'en') } """):

print "%s by %s"%(row.title, row.author)
print("%s by %s"%(row.title, row.author))
14 changes: 7 additions & 7 deletions examples/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


"""

from __future__ import print_function
from rdflib import Graph, RDF, RDFS, Literal
from rdflib.namespace import FOAF

Expand All @@ -32,20 +32,20 @@

# Resources returned when querying are 'auto-boxed' as resources:

print "Bill's friend: ", bill.value(FOAF.knows).value(FOAF.name)
print("Bill's friend: ", bill.value(FOAF.knows).value(FOAF.name))

# slicing ([] syntax) can also be used:

print "Bill knows: ",
print("Bill knows: ", end=" ")
for friend in bill[FOAF.knows]:
print friend[FOAF.name].next(), " "
print(friend[FOAF.name].next(), " ")

# or even quicker with paths:
print "Bill knows: ",
print("Bill knows: ", end=" ")
for friend in bill[FOAF.knows/FOAF.name]:
print friend
print(friend)

# setting single properties is also possible:
bill[RDFS.label]=Literal("William")

print g.serialize(format='n3')
print(g.serialize(format='n3'))
28 changes: 14 additions & 14 deletions examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@
store.add((donna, FOAF.name, Literal("Donna Fales")))

# Iterate over triples in store and print them out.
print "--- printing raw triples ---"
print("--- printing raw triples ---")
for s, p, o in store:
print s, p, o
print(s, p, o)

# For each foaf:Person in the store print out its mbox property.
print "--- printing mboxes ---"
print("--- printing mboxes ---")
for person in store.subjects(RDF.type, FOAF["Person"]):
for mbox in store.objects(person, FOAF["mbox"]):
print mbox
print(mbox)

# Serialize the store as RDF/XML to the file donna_foaf.rdf.
store.serialize("donna_foaf.rdf", format="pretty-xml", max_depth=3)

# Let's show off the serializers

print "RDF Serializations:"
print("RDF Serializations:")

# Serialize as XML
print "--- start: rdf-xml ---"
print store.serialize(format="pretty-xml")
print "--- end: rdf-xml ---\n"
print("--- start: rdf-xml ---")
print(store.serialize(format="pretty-xml"))
print("--- end: rdf-xml ---\n")

# Serialize as Turtle
print "--- start: turtle ---"
print store.serialize(format="turtle")
print "--- end: turtle ---\n"
print("--- start: turtle ---")
print(store.serialize(format="turtle"))
print("--- end: turtle ---\n")

# Serialize as NTriples
print "--- start: ntriples ---"
print store.serialize(format="nt")
print "--- end: ntriples ---\n"
print("--- start: ntriples ---")
print(store.serialize(format="nt"))
print("--- end: ntriples ---\n")

9 changes: 5 additions & 4 deletions examples/sleepycat_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
persistence.

"""
from __future__ import print_function

from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.store import NO_STORE, VALID_STORE
Expand All @@ -24,7 +25,7 @@
else:
assert rt == VALID_STORE, 'The underlying store is corrupt'

print 'Triples in graph before add: ', len(graph)
print('Triples in graph before add: ', len(graph))

# Now we'll add some triples to the graph & commit the changes
rdflib = Namespace('http://rdflib.net/test/')
Expand All @@ -33,10 +34,10 @@
graph.add((rdflib['pic:1'], rdflib.name, Literal('Jane & Bob')))
graph.add((rdflib['pic:2'], rdflib.name, Literal('Squirrel in Tree')))

print 'Triples in graph after add: ', len(graph)
print('Triples in graph after add: ', len(graph))

# display the graph in RDF/XML
print graph.serialize(format='n3')
print(graph.serialize(format='n3'))

# close when done, otherwise sleepycat will leak lock entries.
graph.close()
Expand All @@ -49,7 +50,7 @@

graph.open(path, create = False)

print 'Triples still in graph: ', len(graph)
print('Triples still in graph: ', len(graph))

graph.close()

Expand Down
4 changes: 2 additions & 2 deletions examples/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

friends = list(graph[person:FOAF.knows * '+'/FOAF.name])
if friends:
print "%s's circle of friends:"%graph.value(person, FOAF.name)
print("%s's circle of friends:"%graph.value(person, FOAF.name))
for name in friends:
print name
print(name)

2 changes: 1 addition & 1 deletion examples/smushing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
o = newURI.get(o, o) # might be linked to another person
out.add((s,p,o))

print out.serialize(format="n3")
print(out.serialize(format="n3"))
2 changes: 1 addition & 1 deletion examples/sparql_query_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# which in turn knows it from reading the RDF/XML file
for row in g.query(
'select ?s where { [] foaf:knows ?s .}'):
print row.s
print(row.s)
# or row["s"]
# or row[rdflib.Variable("s")]

Expand Down
2 changes: 1 addition & 1 deletion examples/sparql_update_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

for x in g.subjects(
rdflib.RDF.type, rdflib.URIRef('http://dbpedia.org/resource/Human')):
print x
print(x)
6 changes: 4 additions & 2 deletions examples/sparqlstore_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""

from __future__ import print_function

from rdflib import Graph, URIRef, Namespace

if __name__ == '__main__':
Expand All @@ -18,5 +20,5 @@
URIRef("http://dbpedia.org/resource/Berlin"),
dbo.populationTotal)

print "According to DBPedia Berlin has a population of", pop
print("According to DBPedia Berlin has a population of", pop)

2 changes: 1 addition & 1 deletion examples/swap_primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

# or spit it back out (mostly) the way we created it:

print primer.serialize(format='n3')
print(primer.serialize(format='n3'))

# for more insight into things already done, lets see the namespaces

Expand Down
8 changes: 4 additions & 4 deletions examples/transitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@
g.add((mom, parent, momOfMom))
g.add((mom, parent, dadOfMom))

print "Parents, forward from `ex:person`:"
print("Parents, forward from `ex:person`:")
for i in g.transitive_objects(person, parent):
print i
print(i)

print "Parents, *backward* from `ex:gm1`:"
print("Parents, *backward* from `ex:gm1`:")
for i in g.transitive_subjects(parent, momOfMom):
print i
print(i)

2 changes: 1 addition & 1 deletion rdflib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test():

try:
del c[500]
except IndexError, i:
except IndexError as i:
pass

c.append(Literal("5"))
Expand Down
3 changes: 2 additions & 1 deletion rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
__all__ = ['IsomorphicGraph', 'to_isomorphic', 'isomorphic',
'to_canonical_graph', 'graph_diff', 'similar']

from six import text_type
from rdflib.graph import Graph, ConjunctiveGraph, ReadOnlyGraphAggregate
from rdflib.term import BNode
try:
Expand Down Expand Up @@ -242,7 +243,7 @@ def _md5_hash(t):
if isinstance(i, tuple):
h.update(_md5_hash(i).encode('ascii'))
else:
h.update(unicode(i).encode("utf8"))
h.update(text_type(i).encode("utf8"))
return h.hexdigest()


Expand Down
Loading