The Transform class

pyxsd.transforms.Transform is the abstract base every transform inherits from. It provides tree access and node-manufacturing helpers; anything you add to it must operate only on the generic node shape so it applies to all transforms.

class pyxsd.transforms.transform.Transform(root)[source]

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)

makeElemObj(name)[source]

Creates a new element that contains the proper tree structure.

Parameters:

name (str)

Return type:

Any

makeCommentElem(comment)[source]

Makes a comment element.

Parameters:

comment (str)

Return type:

Any

iter_tree(instance)[source]

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:

Iterator[Any]

walk(instance, visitor, *args, **kwargs)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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)[source]

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

Parameters:
Return type:

list[Any]

find(tagName, baseElem)[source]

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)[source]

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

class pyxsd.transforms.displayer.Displayer(root)[source]
Parameters:

root (Any)

root: Any
openFile(fileName=None)[source]
Parameters:

fileName (str | None)

Return type:

IO[str]

writeTree(file)[source]
Parameters:

file (IO[str])

Return type:

None

makeTempFileOfTree()[source]
Return type:

IO[str]

Tree iteration

pyxsd.transforms.transform.iter_tree(instance)[source]

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:

Iterator[Any]

Built-in transforms

class pyxsd.transforms.print_data.PrintData(root)[source]
Parameters:

root (Any)

class pyxsd.transforms.send_tree_to_pyxsd.SendTreeToPyXSD(root)[source]
Category:

Standard Transform Tools

Description:

Sends the generated XML back into pyXSD

Parameters:

root (Any)