diffpy.srfit.equation.visitors package
Visitors that perform on Literal networks.
Visitors are designed to traverse and extract information from Literal networks (diffpy.srfit.equation.literals). Visitors are used to validate, print and extracting Arguments from Literal networks.
The Literal-Visitor relationship is that described by the Visitor pattern (
http://en.wikipedia.org/wiki/Visitor_pattern).
- diffpy.srfit.equation.visitors.getArgs(literal, getconsts=True)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.get_args instead.
- diffpy.srfit.equation.visitors.getExpression(literal, eqskip=None)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.get_expression instead.
- diffpy.srfit.equation.visitors.get_args(literal, getconsts=True)[source]
Get the Arguments of a Literal tree.
- diffpy.srfit.equation.visitors.get_expression(literal, eqskip=None)[source]
Get the math expression string of a Literal tree.
- diffpy.srfit.equation.visitors.prettyPrint(literal)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.pretty_print instead.
- diffpy.srfit.equation.visitors.pretty_print(literal)[source]
Print the math expression of a Literal tree.
- Parameters:
literal (Literal) – The root of the Literal tree.
- diffpy.srfit.equation.visitors.swap(literal, oldlit, newlit)[source]
Swap one literal for another in a Literal tree.
Corrections are done in-place unless literal is oldlit, in which case the return value is newlit.
- diffpy.srfit.equation.visitors.validate(literal)[source]
Validate a Literal tree.
- Parameters:
literal (Literal) – The root of the Literal tree.
- Raises:
ValueError – If the tree contains errors.
Submodules
diffpy.srfit.equation.visitors.validator module
Validator visitor for validating a tree of Literals.
The Validator walks an equation tree composed of Literals and checks the validity of each equation as much as possible without evaluating it. It collects errors in a list.
The Validator checks that the input count of each Operator is equal to the output count of its arguments. It also checks that each object has the proper interface.
- class diffpy.srfit.equation.visitors.validator.Validator[source]
Bases:
VisitorValidator error for checking validity of an equation tree.
- _nin
The number of outputs of the last node visited, used to count the inputs of its parent operator.
- Type:
- on_argument(arg)[source]
Check an Argument node.
The Argument must be an instance of ArgumentABC from diffpy.srfit.equation.literals.abcs.
diffpy.srfit.equation.visitors.swapper module
Swapper for replacing a Literal in an equation with another Literals.
- class diffpy.srfit.equation.visitors.swapper.Swapper(oldlit, newlit)[source]
Bases:
VisitorSwapper for swapping out one literal for another in a literal tree.
Note that this cannot swap out a root node of a literal tree. This case must be tested for explicitly.
- Parameters:
- on_argument(arg)[source]
Process an Argument node.
Tell the parent to swap the old Argument with the replacement Literal.
- Parameters:
arg (Argument) – The Argument to check.
- on_equation(eq)[source]
Process an Equation node.
This looks at the equation itself as well as the root.
- Parameters:
eq (Equation) – The Equation to check and traverse.
- Raises:
ValueError – If the replacement Literal causes a self-reference.
- on_operator(op)[source]
Process an Operator node.
Tell the parent to swap the old Operator with the replacement Literal, and swap any of its arguments in place.
- Parameters:
op (Operator) – The Operator to check and traverse.
- Raises:
ValueError – If the replacement Literal causes a self-reference.
diffpy.srfit.equation.visitors.argfinder module
Visitor for extracting the Argument entries in a Literal tree.
ArgFinder extracts all Arguments from a literal tree.
- class diffpy.srfit.equation.visitors.argfinder.ArgFinder(getconsts=True)[source]
Bases:
VisitorArgFinder extracts Arguments from a Literal tree.
- Parameters:
getconsts (bool, optional) – The flag that is True (default) to also collect constant Arguments.
diffpy.srfit.equation.visitors.visitor module
Base visitor class.
Visitors work with Literal trees to perform a specified action. They are designed according to the visitor pattern ( http://en.wikipedia.org/wiki/Visitor_pattern).
Visitors work with the following Literal classes (diffpy.srfit.equation.literals): Argument Operator
See the Visitor class for the required methods that each Visitor must overload.
- class diffpy.srfit.equation.visitors.visitor.Visitor[source]
Bases:
objectAbstract class for all visitors to a Literal tree.
Subclasses must override
on_argumentandon_operator. See implemented visitors for examples of use.- onArgument(arg)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.visitor.Visitor.on_argument instead.
- onEquation(eq)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.visitor.Visitor.on_equation instead.
- onOperator(op)
This function has been deprecated and will be removed in version 4.0.0.
Please use diffpy.srfit.equation.visitors.visitor.Visitor.on_operator instead.
- on_argument(arg)[source]
Process an Argument node.
- Parameters:
arg (Argument) – The Argument to process.
- Raises:
NotImplementedError – Always, because subclasses must override this method.
- on_equation(eq)[source]
Process an Equation node.
Equations are specialized Operators. They don’t need to be specifically supported by a Visitor, so by default they are passed to
on_operator.
- on_operator(op)[source]
Process an Operator node.
- Parameters:
op (Operator) – The Operator to process.
- Raises:
NotImplementedError – Always, because subclasses must override this method.
diffpy.srfit.equation.visitors.printer module
Printer visitor for printing the equation represented by a Literal tree.
The Printer visitor creates a one-line representation of the Literal tree, which is valid as a string equivalent of the equation.
- class diffpy.srfit.equation.visitors.printer.Printer[source]
Bases:
VisitorPrinter for printing a Literal tree.
- eqskip
The regular expression pattern for Equation objects that should not be expanded, but printed under their own name. Expand all equations when None.
- Type:
str or None
- property eqskip
Pattern for equation objects to be skipped.
Expand all equations when None.
- on_argument(arg)[source]
Write an Argument node.
The name is written, or the value if the Argument has no name. No assumption is made about the argument type.
- on_equation(eq)[source]
Write an Equation node.
The Equation is written by name if it matches
eqskip, and expanded otherwise.