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.

Parameters:
  • literal (Literal) – The root of the Literal tree.

  • getconsts (bool, optional) – The flag that is True (default) to also get the Arguments that are constant.

Returns:

The Arguments, found depth-first.

Return type:

list of Argument

diffpy.srfit.equation.visitors.get_expression(literal, eqskip=None)[source]

Get the math expression string of a Literal tree.

Parameters:
  • literal (Literal) – The root of the Literal tree.

  • eqskip (str, optional) – The regular expression pattern for Equation objects that should be written under their own name. If None (default), all Equation objects are expanded.

Returns:

The expression of the Literal tree.

Return type:

str

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.

Parameters:
  • literal (Literal) – The root of the Literal tree.

  • oldlit (Literal) – The literal to replace.

  • newlit (Literal) – The literal to put in its place.

Returns:

The Literal tree with oldlit swapped for newlit.

Return type:

Literal

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: Visitor

Validator error for checking validity of an equation tree.

errors

The descriptions of the errors found.

Type:

list of str

_nin

The number of outputs of the last node visited, used to count the inputs of its parent operator.

Type:

int

on_argument(arg)[source]

Check an Argument node.

The Argument must be an instance of ArgumentABC from diffpy.srfit.equation.literals.abcs.

Parameters:

arg (Argument) – The Argument to check.

Returns:

The errors found so far.

Return type:

list of str

on_operator(op)[source]

Check an Operator node and its arguments.

The Operator must be an instance of OperatorABC from diffpy.srfit.equation.literals.abcs, be single-valued, have a name, symbol and operation, and receive as many inputs as it takes.

Parameters:

op (Operator) – The Operator to check.

Returns:

The errors found so far.

Return type:

list of str

reset()[source]

Clear the errors and the input count.

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: Visitor

Swapper 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:
  • oldlit (Literal) – The literal to be replaced.

  • newlit (Literal) – The literal to be placed into the literal tree.

newlit

The literal to be placed into the literal tree.

Type:

Literal

oldlit

The literal to be replaced.

Type:

Literal

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: Visitor

ArgFinder extracts Arguments from a Literal tree.

Parameters:

getconsts (bool, optional) – The flag that is True (default) to also collect constant Arguments.

args

The collected Arguments.

Type:

list

getconsts

The flag that is True to also collect constant Arguments.

Type:

bool

on_argument(arg)[source]

Collect an Argument node.

Parameters:

arg (Argument) – The Argument to collect.

Returns:

The Arguments collected so far.

Return type:

list

on_operator(op)[source]

Collect the Arguments of an Operator node.

Parameters:

op (Operator) – The Operator whose Arguments are collected.

Returns:

The Arguments collected so far.

Return type:

list

reset()[source]

Reset the argument set.

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: object

Abstract class for all visitors to a Literal tree.

Subclasses must override on_argument and on_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.

Parameters:

eq (Equation) – The Equation to process.

Returns:

The result of on_operator(eq).

Return type:

object

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: Visitor

Printer 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

output

The output generated by the printer.

Type:

str

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.

Parameters:

arg (Argument) – The Argument to write.

Returns:

The output so far.

Return type:

str

on_equation(eq)[source]

Write an Equation node.

The Equation is written by name if it matches eqskip, and expanded otherwise.

Parameters:

eq (Equation) – The Equation to write.

Returns:

The output so far.

Return type:

str

on_operator(op)[source]

Write an Operator node.

Binary operators whose symbol differs from their name are written in infix form, such as “(a + b)”. Others are written as a function call, such as “sin(a)”.

Parameters:

op (Operator) – The Operator to write.

Returns:

The output so far.

Return type:

str

reset()[source]

Reset the output string.