]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Reinstate elementtree example
authorVraj Mohan <radhakrishnan.vrajmohan@gsa.gov>
Wed, 16 Jan 2019 20:36:54 +0000 (15:36 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Jan 2019 01:21:48 +0000 (20:21 -0500)
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
examples/elementtree/adjacency_list.py
examples/elementtree/optimized_al.py
examples/elementtree/pickle_type.py [moved from examples/elementtree/pickle.py with 97% similarity]

index 82d00ff5ad7d393de4fd51cdca3f9afd6f5274be..82bb873610f607f3add3824a10d2622f2c8eac1a 100644 (file)
@@ -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
 
 """
index 6dd88c7973d603b625bd6c3bb15b734701151805..0f53e0012de08ad362b3131618593e527f2f44ae 100644 (file)
@@ -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:
 
index b66ada19d04e249a2d8464d378a47bc6c39a529a..b089a0170898a203fc31968aa4b425fa141110c1 100644 (file)
@@ -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:
 
similarity index 97%
rename from examples/elementtree/pickle.py
rename to examples/elementtree/pickle_type.py
index ca2c6550494ea305a82fa5701e9f3f0a83d287d5..83643c663c0e6bc2fd71f21db8ec9f0add2b39a0 100644 (file)
@@ -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)