summaryrefslogtreecommitdiff
path: root/examples/elementtree/adjacency_list.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/elementtree/adjacency_list.py')
-rw-r--r--examples/elementtree/adjacency_list.py40
1 files changed, 20 insertions, 20 deletions
diff --git a/examples/elementtree/adjacency_list.py b/examples/elementtree/adjacency_list.py
index 3b9e4c523..a3ad42778 100644
--- a/examples/elementtree/adjacency_list.py
+++ b/examples/elementtree/adjacency_list.py
@@ -11,7 +11,7 @@ from sqlalchemy import (MetaData, Table, Column, Integer, String, ForeignKey,
Unicode, and_, create_engine)
from sqlalchemy.orm import mapper, relationship, Session, lazyload
-import sys, os, StringIO, re
+import sys, os, io, re
from xml.etree import ElementTree
@@ -56,7 +56,7 @@ class Document(object):
self.element = element
def __str__(self):
- buf = StringIO.StringIO()
+ buf = io.StringIO()
self.element.write(buf)
return buf.getvalue()
@@ -120,11 +120,11 @@ class ElementTreeMarshal(object):
def __set__(self, document, element):
def traverse(node):
n = _Node()
- n.tag = unicode(node.tag)
- n.text = unicode(node.text)
- n.tail = unicode(node.tail)
+ n.tag = str(node.tag)
+ n.text = str(node.text)
+ n.tail = str(node.tail)
n.children = [traverse(n2) for n2 in node]
- n.attributes = [_Attribute(unicode(k), unicode(v)) for k, v in node.attrib.iteritems()]
+ n.attributes = [_Attribute(str(k), str(v)) for k, v in node.attrib.items()]
return n
document._root = traverse(element.getroot())
@@ -150,23 +150,23 @@ for file in ('test.xml', 'test2.xml', 'test3.xml'):
doc = ElementTree.parse(filename)
session.add(Document(file, doc))
-print "\nSaving three documents...", line
+print("\nSaving three documents...", line)
session.commit()
-print "Done."
+print("Done.")
-print "\nFull text of document 'text.xml':", line
+print("\nFull text of document 'text.xml':", line)
document = session.query(Document).filter_by(filename="test.xml").first()
-print document
+print(document)
############################################ PART VI - Searching for Paths #########################
# manually search for a document which contains "/somefile/header/field1:hi"
-d = session.query(Document).join('_root', aliased=True).filter(_Node.tag==u'somefile').\
- join('children', aliased=True, from_joinpoint=True).filter(_Node.tag==u'header').\
+d = session.query(Document).join('_root', aliased=True).filter(_Node.tag=='somefile').\
+ join('children', aliased=True, from_joinpoint=True).filter(_Node.tag=='header').\
join('children', aliased=True, from_joinpoint=True).filter(
- and_(_Node.tag==u'field1', _Node.text==u'hi')).one()
-print d
+ and_(_Node.tag=='field1', _Node.text=='hi')).one()
+print(d)
# generalize the above approach into an extremely impoverished xpath function:
def find_document(path, compareto):
@@ -188,11 +188,11 @@ def find_document(path, compareto):
return query.options(lazyload('_root')).filter(_Node.text==compareto).all()
for path, compareto in (
- (u'/somefile/header/field1', u'hi'),
- (u'/somefile/field1', u'hi'),
- (u'/somefile/header/field2', u'there'),
- (u'/somefile/header/field2[@attr=foo]', u'there')
+ ('/somefile/header/field1', 'hi'),
+ ('/somefile/field1', 'hi'),
+ ('/somefile/header/field2', 'there'),
+ ('/somefile/header/field2[@attr=foo]', 'there')
):
- print "\nDocuments containing '%s=%s':" % (path, compareto), line
- print [d.filename for d in find_document(path, compareto)]
+ print("\nDocuments containing '%s=%s':" % (path, compareto), line)
+ print([d.filename for d in find_document(path, compareto)])