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

Displayer

The base abstract class for all transforms.

ToData

Exports the bound tree under an explicit JSON data contract.

ToDict

Exports the bound tree as a plain dict.

Transform

The base abstract class for all transforms.

Functions

iter_tree(instance)

Yield every tree node at or below instance, depth-first.

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 (like Displayer) cannot be instantiated by accident. Subclasses should call super().__init__(root), which stores the root as self.root.

Parameters:

root (Any)

root: Any
openFile(fileName=None)
Parameters:

fileName (str | None)

Return type:

IO[str]

writeTree(file)
Parameters:

file (IO[str])

Return type:

None

makeTempFileOfTree()
Return type:

IO[str]

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, while Document.transform calls 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’s to_data method.

Parameters:
  • root (Any)

  • format (pyxsd.data_conversion.contracts.FormatName)

  • fidelity (pyxsd.data_conversion.contracts.Fidelity)

  • typed (bool)

  • always_list (bool)

kwargs: dict[str, Any]
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, while Document.transform calls it with the root; __call__ therefore accepts an optional explicit root and otherwise uses the one given at construction. Keyword arguments pass through to pyxsd.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 (like Displayer) cannot be instantiated by accident. Subclasses should call super().__init__(root), which stores the root as self.root.

Parameters:

root (Any)

root
makeElemObj(name)

Creates a new element that contains the proper tree structure.

Parameters:

name (str)

Return type:

Any

makeCommentElem(comment)

Makes a comment element.

Parameters:

comment (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:

collections.abc.Iterator[Any]

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) where node is the tree node being visited, attrNames is the list of its attribute names, and elemNames is the list of its children’s names. Traversal is driven by iter_tree().

Parameters:
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.

Parameters:
Return type:

None

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.

Parameters:
Return type:

None

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.

Parameters:
Return type:

None

getInstancesByClassName(root)

Uses the walk function with the classCollector visitor function to associate a class name with the class’s instances.

Parameters:

root (Any)

Return type:

dict[str, list[Any]]

getAllSubElements(root)

Uses the walk function with the tagCollector visitor function to make a dictionary that associates all elements with their sub-elements.

Parameters:

root (Any)

Return type:

dict[str, list[Any]]

getElementsByName(root, name)

Uses the walk function with the tagFinder visitor function to make a list containing all elements with a particular name.

Parameters:
  • root (Any)

  • name (str)

Return type:

list[Any]

find(tagName, baseElem)

Finds an element from a given tagName.

Returns the first one found, or returns None. This function is an alternative to the walk/visitor functions. See getElementsByName.

Parameters:
  • tagName (str)

  • baseElem (Any)

Return type:

Any | None

findAll(tagName, baseElem)

Finds all elements with a given tagName.

Returns a list of elements or None. This function is an alternative to the walk/visitor functions. See getElementsByName.

Parameters:
  • tagName (str)

  • baseElem (Any)

Return type:

list[Any] | None

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 powers walk() 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:

collections.abc.Iterator[Any]