summaryrefslogtreecommitdiff
path: root/examples/elementtree
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-01-06 01:19:47 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-01-06 18:23:11 -0500
commit1e278de4cc9a4181e0747640a960e80efcea1ca9 (patch)
tree13d0c035807613bfa07e734acad79b9c843cb8b0 /examples/elementtree
parent1e1a38e7801f410f244e4bbb44ec795ae152e04e (diff)
downloadsqlalchemy-1e278de4cc9a4181e0747640a960e80efcea1ca9.tar.gz
Post black reformatting
Applied on top of a pure run of black -l 79 in I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9, this set of changes resolves all remaining flake8 conditions for those codes we have enabled in setup.cfg. Included are resolutions for all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I4f72d3ba1380dd601610ff80b8fb06a2aff8b0fe
Diffstat (limited to 'examples/elementtree')
-rw-r--r--examples/elementtree/adjacency_list.py83
-rw-r--r--examples/elementtree/optimized_al.py83
-rw-r--r--examples/elementtree/pickle.py42
3 files changed, 123 insertions, 85 deletions
diff --git a/examples/elementtree/adjacency_list.py b/examples/elementtree/adjacency_list.py
index 1f7161212..6dd88c797 100644
--- a/examples/elementtree/adjacency_list.py
+++ b/examples/elementtree/adjacency_list.py
@@ -1,4 +1,6 @@
-"""Illustrates an explicit way to persist an XML document expressed using ElementTree.
+"""
+Illustrates an explicit way to persist an XML document expressed using
+ElementTree.
Each DOM node is stored in an individual
table row, with attributes represented in a separate table. The
@@ -8,34 +10,37 @@ 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.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.
"""
-################################# PART I - Imports/Coniguration ####################################
-from sqlalchemy import (
- MetaData,
- Table,
- Column,
- Integer,
- String,
- ForeignKey,
- Unicode,
- and_,
- create_engine,
-)
-from sqlalchemy.orm import mapper, relationship, Session, lazyload
+# PART I - Imports/Configuration
+import io
+import os
+import re
+from xml.etree import ElementTree
-import sys, os, io, re
+from sqlalchemy import and_
+from sqlalchemy import Column
+from sqlalchemy import create_engine
+from sqlalchemy import ForeignKey
+from sqlalchemy import Integer
+from sqlalchemy import MetaData
+from sqlalchemy import String
+from sqlalchemy import Table
+from sqlalchemy import Unicode
+from sqlalchemy.orm import lazyload
+from sqlalchemy.orm import mapper
+from sqlalchemy.orm import relationship
+from sqlalchemy.orm import Session
-from xml.etree import ElementTree
e = create_engine("sqlite://")
meta = MetaData()
-################################# PART II - Table Metadata #########################################
+# PART II - Table Metadata
# stores a top level record of an XML document.
documents = Table(
@@ -75,10 +80,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
@@ -90,19 +97,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
@@ -130,9 +140,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 "_root" 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 "_root" mapped
+# collection.
+
+
class ElementTreeMarshal(object):
def __get__(self, document, owner):
if document is None:
@@ -180,7 +193,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--------------------------------------------------------"
@@ -202,7 +215,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"
d = (
@@ -218,6 +231,8 @@ d = (
print(d)
# generalize the above approach into an extremely impoverished xpath function:
+
+
def find_document(path, compareto):
j = documents
prev_elements = None
diff --git a/examples/elementtree/optimized_al.py b/examples/elementtree/optimized_al.py
index 8e9c48b96..b66ada19d 100644
--- a/examples/elementtree/optimized_al.py
+++ b/examples/elementtree/optimized_al.py
@@ -7,28 +7,32 @@
"""
-##################### PART I - Imports/Configuration #########################
-from sqlalchemy import (
- MetaData,
- Table,
- Column,
- Integer,
- String,
- ForeignKey,
- Unicode,
- and_,
- create_engine,
-)
-from sqlalchemy.orm import mapper, relationship, Session, lazyload
-
-import sys, os, io, re
+# PART I - Imports/Configuration
+import io
+import os
+import re
from xml.etree import ElementTree
+from sqlalchemy import and_
+from sqlalchemy import Column
+from sqlalchemy import create_engine
+from sqlalchemy import ForeignKey
+from sqlalchemy import Integer
+from sqlalchemy import MetaData
+from sqlalchemy import String
+from sqlalchemy import Table
+from sqlalchemy import Unicode
+from sqlalchemy.orm import lazyload
+from sqlalchemy.orm import mapper
+from sqlalchemy.orm import relationship
+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(
@@ -68,10 +72,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
@@ -83,19 +89,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
@@ -115,10 +124,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,
@@ -134,9 +144,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:
@@ -188,7 +201,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--------------------------------------------------------"
@@ -210,7 +223,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)
@@ -227,6 +240,8 @@ d = (
print(d)
# generalize the above approach into an extremely impoverished xpath function:
+
+
def find_document(path, compareto):
j = documents
prev_elements = None
diff --git a/examples/elementtree/pickle.py b/examples/elementtree/pickle.py
index a86fe30e5..ca2c65504 100644
--- a/examples/elementtree/pickle.py
+++ b/examples/elementtree/pickle.py
@@ -1,31 +1,37 @@
-"""illustrates a quick and dirty way to persist an XML document expressed using ElementTree and pickle.
+"""
+illustrates a quick and dirty way to persist an XML document expressed using
+ElementTree and pickle.
This is a trivial example using PickleType to marshal/unmarshal the ElementTree
-document into a binary column. Compare to explicit.py which stores the individual components of the ElementTree
-structure in distinct rows using two additional mapped entities. Note that the usage of both
-styles of persistence are identical, as is the structure of the main Document class.
+document into a binary column. Compare to explicit.py which stores the
+individual components of the ElementTree structure in distinct rows using two
+additional mapped entities. Note that the usage of both styles of persistence
+are identical, as is the structure of the main Document class.
+
"""
-from sqlalchemy import (
- create_engine,
- MetaData,
- Table,
- Column,
- Integer,
- String,
- PickleType,
-)
-from sqlalchemy.orm import mapper, Session
+import os
+import sys
+from xml.etree import ElementTree
-import sys, os
+from sqlalchemy import Column
+from sqlalchemy import create_engine
+from sqlalchemy import Integer
+from sqlalchemy import MetaData
+from sqlalchemy import PickleType
+from sqlalchemy import String
+from sqlalchemy import Table
+from sqlalchemy.orm import mapper
+from sqlalchemy.orm import Session
-from xml.etree import ElementTree
e = create_engine("sqlite://")
meta = MetaData()
# setup a comparator for the PickleType since it's a mutable
# element.
+
+
def are_elements_equal(x, y):
return x == y
@@ -44,6 +50,8 @@ meta.create_all(e)
# our document class. contains a string name,
# and the ElementTree root element.
+
+
class Document(object):
def __init__(self, name, element):
self.filename = name
@@ -53,7 +61,7 @@ class Document(object):
# setup mapper.
mapper(Document, documents)
-###### time to test ! #########
+# time to test !
# get ElementTree document
filename = os.path.join(os.path.dirname(__file__), "test.xml")