summaryrefslogtreecommitdiff
path: root/examples/elementtree/optimized_al.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-12-30 23:53:36 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-01-05 12:53:38 -0500
commiteba16588f2e41d458eb982688140d1cb0b89b27f (patch)
tree0dc8b4c673ee0ad381b28a45cebbdbd2b3671883 /examples/elementtree/optimized_al.py
parent6673a213daa02ade815d00a19e39edc6cee1466a (diff)
downloadsqlalchemy-eba16588f2e41d458eb982688140d1cb0b89b27f.tar.gz
partial cherry-pick of master flake8. clean cherrypick for lib and test,
manually merged exaples. Change-Id: I9532d3b13d13f2769e6ca48eea23dd7d4041f68f (cherry picked from commit e3bdd80c6661c0e95fb67998c57540be667ce761)
Diffstat (limited to 'examples/elementtree/optimized_al.py')
-rw-r--r--examples/elementtree/optimized_al.py52
1 files changed, 32 insertions, 20 deletions
diff --git a/examples/elementtree/optimized_al.py b/examples/elementtree/optimized_al.py
index 586a0be32..e4fbb2ef8 100644
--- a/examples/elementtree/optimized_al.py
+++ b/examples/elementtree/optimized_al.py
@@ -7,7 +7,8 @@
"""
-##################### PART I - Imports/Configuration #########################
+# PART I - Imports/Configuration
+
import io
import os
import re
@@ -30,7 +31,7 @@ from sqlalchemy.orm import Session
e = create_engine("sqlite://", echo=True)
meta = MetaData()
-####################### PART II - Table Metadata #############################
+# PART II - Table Metadata
# stores a top level record of an XML document.
documents = Table(
@@ -70,10 +71,12 @@ attributes = Table(
meta.create_all(e)
-########################### PART III - Model #################################
+# PART III - Model
# our document class. contains a string name,
# and the ElementTree root element.
+
+
class Document(object):
def __init__(self, name, element):
self.filename = name
@@ -85,19 +88,22 @@ class Document(object):
return buf.getvalue()
-########################## PART IV - Persistence Mapping #####################
+# PART IV - Persistence Mapping
+
+# Node class. a non-public class which will represent the DB-persisted
+# Element/SubElement object. We cannot create mappers for ElementTree elements
+# directly because they are at the very least not new-style classes, and also
+# may be backed by native implementations. so here we construct an adapter.
+
-# Node class. a non-public class which will represent
-# the DB-persisted Element/SubElement object. We cannot create mappers for
-# ElementTree elements directly because they are at the very least not new-style
-# classes, and also may be backed by native implementations.
-# so here we construct an adapter.
class _Node(object):
pass
-# Attribute class. also internal, this will represent the key/value attributes stored for
-# a particular Node.
+# Attribute class. also internal, this will represent the key/value attributes
+# stored for a particular Node.
+
+
class _Attribute(object):
def __init__(self, name, value):
self.name = name
@@ -117,10 +123,11 @@ mapper(
},
)
-# the _Node objects change the way they load so that a list of _Nodes will organize
-# themselves hierarchically using the ElementTreeMarshal. this depends on the ordering of
-# nodes being hierarchical as well; relationship() always applies at least ROWID/primary key
-# ordering to rows which will suffice.
+# the _Node objects change the way they load so that a list of _Nodes will
+# organize themselves hierarchically using the ElementTreeMarshal. this
+# depends on the ordering of nodes being hierarchical as well; relationship()
+# always applies at least ROWID/primary key ordering to rows which will
+# suffice.
mapper(
_Node,
elements,
@@ -136,9 +143,12 @@ mapper(
mapper(_Attribute, attributes)
-# define marshalling functions that convert from _Node/_Attribute to/from ElementTree objects.
-# this will set the ElementTree element as "document._element", and append the root _Node
-# object to the "_nodes" mapped collection.
+# define marshalling functions that convert from _Node/_Attribute to/from
+# ElementTree objects. this will set the ElementTree element as
+# "document._element", and append the root _Node object to the "_nodes" mapped
+# collection.
+
+
class ElementTreeMarshal(object):
def __get__(self, document, owner):
if document is None:
@@ -190,7 +200,7 @@ class ElementTreeMarshal(object):
# override Document's "element" attribute with the marshaller.
Document.element = ElementTreeMarshal()
-###################### PART V - Basic Persistence Example ####################
+# PART V - Basic Persistence Example
line = "\n--------------------------------------------------------"
@@ -212,7 +222,7 @@ document = session.query(Document).filter_by(filename="test.xml").first()
print(document)
-######################## PART VI - Searching for Paths #######################
+# PART VI - Searching for Paths
# manually search for a document which contains "/somefile/header/field1:hi"
print("\nManual search for /somefile/header/field1=='hi':", line)
@@ -229,6 +239,8 @@ d = (
print(d)
# generalize the above approach into an extremely impoverished xpath function:
+
+
def find_document(path, compareto):
j = documents
prev_elements = None