From 0717ce9d1dc28b67b4568351f4e98e59a831b1f1 Mon Sep 17 00:00:00 2001 From: Vraj Mohan Date: Wed, 16 Jan 2019 15:36:54 -0500 Subject: [PATCH] Reinstate elementtree example Partially fixes sqlalchemy/sqlalchemy#4426. Closes: #4441 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4441 Pull-request-sha: ed90f3b77c2d0f4cc2cd54f40f5af29b489977d2 Change-Id: Ic32e5d8020073da055b12a6aeb61e698a97dc504 --- examples/elementtree/__init__.py | 2 +- examples/elementtree/adjacency_list.py | 22 ++++++++----------- examples/elementtree/optimized_al.py | 14 +++++------- .../elementtree/{pickle.py => pickle_type.py} | 3 +-- 4 files changed, 16 insertions(+), 25 deletions(-) rename examples/elementtree/{pickle.py => pickle_type.py} (97%) diff --git a/examples/elementtree/__init__.py b/examples/elementtree/__init__.py index 82d00ff5ad..82bb873610 100644 --- a/examples/elementtree/__init__.py +++ b/examples/elementtree/__init__.py @@ -20,6 +20,6 @@ E.g.:: print document .. autosource:: - :files: pickle.py, adjacency_list.py, optimized_al.py + :files: pickle_type.py, adjacency_list.py, optimized_al.py """ diff --git a/examples/elementtree/adjacency_list.py b/examples/elementtree/adjacency_list.py index 6dd88c7973..0f53e0012d 100644 --- a/examples/elementtree/adjacency_list.py +++ b/examples/elementtree/adjacency_list.py @@ -10,14 +10,16 @@ along any path with a given structure of attributes, basically a (very narrow) subset of xpath. This example explicitly marshals/unmarshals the ElementTree document into -mapped entities which have their own tables. Compare to pickle.py which uses -pickle to accomplish the same task. Note that the usage of both styles of -persistence are identical, as is the structure of the main Document class. +mapped entities which have their own tables. Compare to pickle_type.py which +uses PickleType to accomplish the same task. Note that the usage of both +styles of persistence are identical, as is the structure of the main Document +class. """ # PART I - Imports/Configuration -import io +from __future__ import print_function + import os import re from xml.etree import ElementTree @@ -91,12 +93,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 @@ -175,7 +171,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() @@ -213,7 +209,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 @@ -228,7 +224,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..b089a01708 100644 --- a/examples/elementtree/optimized_al.py +++ b/examples/elementtree/optimized_al.py @@ -9,7 +9,8 @@ # PART I - Imports/Configuration -import io +from __future__ import print_function + import os import re from xml.etree import ElementTree @@ -29,7 +30,7 @@ from sqlalchemy.orm import relationship from sqlalchemy.orm import Session -e = create_engine("sqlite://", echo=True) +e = create_engine("sqlite://") meta = MetaData() # PART II - Table Metadata @@ -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.py b/examples/elementtree/pickle_type.py similarity index 97% rename from examples/elementtree/pickle.py rename to examples/elementtree/pickle_type.py index ca2c655049..83643c663c 100644 --- a/examples/elementtree/pickle.py +++ b/examples/elementtree/pickle_type.py @@ -11,7 +11,6 @@ are identical, as is the structure of the main Document class. """ import os -import sys from xml.etree import ElementTree from sqlalchemy import Column @@ -76,4 +75,4 @@ session.commit() document = session.query(Document).filter_by(filename="test.xml").first() # print -document.element.write(sys.stdout) +ElementTree.dump(document.element) -- 2.47.2