From: Vraj Mohan Date: Wed, 16 Jan 2019 17:40:35 +0000 (-0800) Subject: Use ElementTree.dump to handle Python 2 vs 3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=014767d5b5306a7d5c38db17468d9a7804f601f3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Use ElementTree.dump to handle Python 2 vs 3 --- diff --git a/examples/elementtree/adjacency_list.py b/examples/elementtree/adjacency_list.py index a69d69e660..76949c8da1 100644 --- a/examples/elementtree/adjacency_list.py +++ b/examples/elementtree/adjacency_list.py @@ -18,7 +18,7 @@ class. """ # PART I - Imports/Configuration -import io +from __future__ import print_function import os import re from xml.etree import ElementTree @@ -92,12 +92,6 @@ class Document(object): self.filename = name self.element = element - def __str__(self): - buf = io.StringIO() - self.element.write(buf) - return buf.getvalue() - - # PART IV - Persistence Mapping # Node class. a non-public class which will represent the DB-persisted @@ -176,7 +170,7 @@ class ElementTreeMarshal(object): n = _Node() n.tag = str(node.tag) n.text = str(node.text) - n.tail = str(node.tail) + n.tail = str(node.tail) if node.tail else None n.children = [traverse(n2) for n2 in node] n.attributes = [ _Attribute(str(k), str(v)) for k, v in node.attrib.items() @@ -214,7 +208,7 @@ print("Done.") print("\nFull text of document 'text.xml':", line) document = session.query(Document).filter_by(filename="test.xml").first() -print(document) +ElementTree.dump(document.element) # PART VI - Searching for Paths @@ -229,7 +223,7 @@ d = ( .filter(and_(_Node.tag == "field1", _Node.text == "hi")) .one() ) -print(d) +ElementTree.dump(d.element) # generalize the above approach into an extremely impoverished xpath function: diff --git a/examples/elementtree/optimized_al.py b/examples/elementtree/optimized_al.py index b66ada19d0..d6971d64fa 100644 --- a/examples/elementtree/optimized_al.py +++ b/examples/elementtree/optimized_al.py @@ -9,6 +9,7 @@ # PART I - Imports/Configuration +from __future__ import print_function import io import os import re @@ -83,11 +84,6 @@ class Document(object): self.filename = name self.element = element - def __str__(self): - buf = io.StringIO() - self.element.write(buf) - return buf.getvalue() - # PART IV - Persistence Mapping @@ -221,7 +217,7 @@ print("Done.") print("\nFull text of document 'text.xml':", line) document = session.query(Document).filter_by(filename="test.xml").first() -print(document) +ElementTree.dump(document.element) # PART VI - Searching for Paths @@ -237,7 +233,7 @@ d = ( .filter(and_(_Node.tag == "field1", _Node.text == "hi")) .one() ) -print(d) +ElementTree.dump(d.element) # generalize the above approach into an extremely impoverished xpath function: diff --git a/examples/elementtree/pickle_type.py b/examples/elementtree/pickle_type.py index fee5286598..a77d55cda1 100644 --- a/examples/elementtree/pickle_type.py +++ b/examples/elementtree/pickle_type.py @@ -23,7 +23,6 @@ from sqlalchemy import String from sqlalchemy import Table from sqlalchemy.orm import mapper from sqlalchemy.orm import Session -from sqlalchemy.util import py3k e = create_engine("sqlite://") @@ -77,7 +76,4 @@ session.commit() document = session.query(Document).filter_by(filename="test.xml").first() # print -if py3k: - document.element.write(sys.stdout, encoding="unicode") -else: - document.element.write(sys.stdout) +ElementTree.dump(document.element)