summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorVraj Mohan <radhakrishnan.vrajmohan@gsa.gov>2019-01-16 15:36:54 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-01-16 20:21:48 -0500
commit0717ce9d1dc28b67b4568351f4e98e59a831b1f1 (patch)
tree6847888b4cab8d7dbf3554241d266303215d7a15 /examples
parent313be7c78db070169bd863948f922c74871d7000 (diff)
downloadsqlalchemy-0717ce9d1dc28b67b4568351f4e98e59a831b1f1.tar.gz
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
Diffstat (limited to 'examples')
-rw-r--r--examples/elementtree/__init__.py2
-rw-r--r--examples/elementtree/adjacency_list.py22
-rw-r--r--examples/elementtree/optimized_al.py14
-rw-r--r--examples/elementtree/pickle_type.py (renamed from examples/elementtree/pickle.py)3
4 files changed, 16 insertions, 25 deletions
diff --git a/examples/elementtree/__init__.py b/examples/elementtree/__init__.py
index 82d00ff5a..82bb87361 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 6dd88c797..0f53e0012 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 b66ada19d..b089a0170 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
index ca2c65504..83643c663 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)