pyxsd.transforms¶
Transform framework for pyxsd.
Custom transforms subclass Transform (or Displayer
for transforms that write output) from this package:
from pyxsd.transforms import Transform
The shipped transforms are PrintData,
ToDict, and
ToData.
The historical crystallography library (CellSizer and friends) moved to
examples/legacy/ in the repository.
Classes¶
Functions¶
|
Yield every tree node at or below |
Package Contents¶
- class pyxsd.transforms.Displayer(root)¶
The base abstract class for all transforms.
All methods should mix into the usable transform classes. Contains methods to retrieve elements from the tree.
Subclasses must accept the tree root in their
__init__; that makes the class abstract until it does, so framework-only subclasses (likeDisplayer) cannot be instantiated by accident. Subclasses should callsuper().__init__(root), which stores the root asself.root.- Parameters:
root (Any)
- root: Any¶
- class pyxsd.transforms.ToData(root=None, *, format='pyxsd', fidelity='best-effort', typed=True, always_list=False)¶
Exports the bound tree under an explicit JSON data contract.
Usable both as
doc.transform(ToData(**options))and from the CLI as--transform ToData(**options). The CLI constructs a transform with the tree root, whileDocument.transformcalls it with the root;__call__therefore accepts an optional explicit root and otherwise uses the one given at construction. Keyword arguments pass through to the export codec. The CLI supplies document context for schema-directed formats, including RELAX NG.Native
format="pyxsd"also works on a RELAX NG lexical tree and produces the generic grouped export; it never manufactures RELAX NG typed wrappers or tags. A root-only call cannot export a RELAX NG tree in a schema-directed format; use the CLI or the document’sto_datamethod.- Parameters:
- class pyxsd.transforms.ToDict(root=None, **kwargs)¶
Exports the bound tree as a plain dict.
Usable both as
doc.transform(ToDict(**options))and from the CLI as--transform to_dict(**options). The CLI constructs a transform with the tree root, whileDocument.transformcalls it with the root;__call__therefore accepts an optional explicit root and otherwise uses the one given at construction. Keyword arguments pass through topyxsd.dict_export.bound_to_dict().- Parameters:
root (Any)
kwargs (Any)
- kwargs¶
- class pyxsd.transforms.Transform(root)¶
The base abstract class for all transforms.
All methods should mix into the usable transform classes. Contains methods to retrieve elements from the tree.
Subclasses must accept the tree root in their
__init__; that makes the class abstract until it does, so framework-only subclasses (likeDisplayer) cannot be instantiated by accident. Subclasses should callsuper().__init__(root), which stores the root asself.root.- Parameters:
root (Any)
- root¶
- makeElemObj(name)¶
Creates a new element that contains the proper tree structure.
- Parameters:
name (str)
- Return type:
Any
- iter_tree(instance)¶
Yield every tree node at or below
instance, depth-first.Method form of the module-level
iter_tree(); see there for semantics.instance: a tree node, or a list/dict of them.
- Parameters:
instance (Any)
- Return type:
- walk(instance, visitor, *args, **kwargs)¶
Walks through the tree structure and runs a provided visitor function on all elements.
The visitor is called as
visitor(node, attrNames, elemNames, *args, **kwargs)wherenodeis the tree node being visited,attrNamesis the list of its attribute names, andelemNamesis the list of its children’s names. Traversal is driven byiter_tree().- Parameters:
instance (Any)
visitor (collections.abc.Callable[..., Any])
args (Any)
kwargs (Any)
- Return type:
None
- classCollector(instance, attrNames, elemNames, collectorDict)¶
Visitor function to make a dictionary that associates a class with its instances.
The class name is the key, and the value is the list of associated instances. See
getInstancesByClassName.
- tagCollector(instance, attrNames, elemNames, collectorDict)¶
A visitor function that is used to make a dictionary that associates a tag name with its children.
See
getAllSubElements.
- tagFinder(instance, attrNames, elemNames, collection, name)¶
A visitor function to collect all tags with a particular name and put them into a list.
See
getElementsByName.
- getInstancesByClassName(root)¶
Uses the
walkfunction with theclassCollectorvisitor function to associate a class name with the class’s instances.
- getAllSubElements(root)¶
Uses the
walkfunction with thetagCollectorvisitor function to make a dictionary that associates all elements with their sub-elements.
- getElementsByName(root, name)¶
Uses the
walkfunction with thetagFindervisitor function to make a list containing all elements with a particular name.
- pyxsd.transforms.iter_tree(instance)¶
Yield every tree node at or below
instance, depth-first.Lists (and tuples) are descended into item by item and dictionaries by value; anything without both
_children_and_attribs_is skipped. Each yielded node is visited before its children (pre-order). This generator powerswalk()and is the supported way to iterate a tree directly:for node in iter_tree(root): ...
instance: a tree node, or a list/dict of them.
- Parameters:
instance (Any)
- Return type: