|
75 | 75 | > beq = c*f(a,b) |
76 | 76 | > eq = beq.makeEquation() |
77 | 77 | """ |
| 78 | +import inspect |
| 79 | +import numbers |
| 80 | +import token |
| 81 | +import tokenize |
| 82 | + |
| 83 | +import numpy |
| 84 | +import six |
| 85 | + |
| 86 | +import diffpy.srfit.equation.literals as literals |
| 87 | +from diffpy.srfit.equation.equationmod import Equation |
| 88 | +from diffpy.srfit.equation.literals.literal import Literal |
78 | 89 |
|
79 | 90 | __all__ = [ |
80 | 91 | "EquationFactory", |
|
95 | 106 | _builders = {} |
96 | 107 |
|
97 | 108 |
|
98 | | -import inspect |
99 | | -import numbers |
100 | | - |
101 | | -import numpy |
102 | | -import six |
103 | | - |
104 | | -import diffpy.srfit.equation.literals as literals |
105 | | -from diffpy.srfit.equation.equationmod import Equation |
106 | | -from diffpy.srfit.equation.literals.literal import Literal |
107 | | - |
108 | | - |
109 | 109 | class EquationFactory(object): |
110 | 110 | """A Factory for equations. |
111 | 111 |
|
112 | 112 | builders -- A dictionary of BaseBuilders registered with the |
113 | 113 | factory, indexed by name. |
114 | 114 | newargs -- A set of new arguments created by makeEquation. This is |
115 | 115 | redefined whenever makeEquation is called. |
116 | | - equations -- Set of equations that have been built by the EquationFactory. |
| 116 | + equations -- Set of equations that have been built by the |
| 117 | + EquationFactory. |
117 | 118 | """ |
118 | 119 |
|
119 | 120 | symbols = ("+", "-", "*", "/", "**", "%", "|") |
@@ -213,10 +214,10 @@ def registerFunction(self, name, func, argnames): |
213 | 214 | if n not in self.builders: |
214 | 215 | self.registerConstant(n, 0) |
215 | 216 | opbuilder = wrapFunction(name, func, len(argnames)) |
216 | | - for n in argnames: |
217 | | - b = self.builders[n] |
218 | | - l = b.literal |
219 | | - opbuilder.literal.addLiteral(l) |
| 217 | + for argname in argnames: |
| 218 | + builder = self.builders[argname] |
| 219 | + argliteral = builder.literal |
| 220 | + opbuilder.literal.addLiteral(argliteral) |
220 | 221 |
|
221 | 222 | return self.registerBuilder(name, opbuilder) |
222 | 223 |
|
@@ -341,9 +342,6 @@ def _getUndefinedArgs(self, eqstr): |
341 | 342 |
|
342 | 343 | Raises SyntaxError if the equation string uses invalid syntax. |
343 | 344 | """ |
344 | | - import token |
345 | | - import tokenize |
346 | | - |
347 | 345 | interface = six.StringIO(eqstr).readline |
348 | 346 | # output is an iterator. Each entry (token) is a 5-tuple |
349 | 347 | # token[0] = token type |
@@ -512,7 +510,7 @@ def __neg__(self): |
512 | 510 | return self.__evalUnary(literals.NegationOperator) |
513 | 511 |
|
514 | 512 |
|
515 | | -## These are used by the class. |
| 513 | +# These are used by the class. |
516 | 514 |
|
517 | 515 |
|
518 | 516 | class ArgumentBuilder(BaseBuilder): |
@@ -681,15 +679,18 @@ def __wrapSrFitOperators(): |
681 | 679 | instances in the module namespace.""" |
682 | 680 | opmod = literals.operators |
683 | 681 | excluded_types = set((opmod.CustomOperator, opmod.UFuncOperator)) |
| 682 | + |
684 | 683 | # check if opmod member should be wrapped as OperatorBuilder |
685 | | - is_exported_type = lambda cls: ( |
686 | | - inspect.isclass(cls) |
687 | | - and issubclass(cls, opmod.Operator) |
688 | | - and not inspect.isabstract(cls) |
689 | | - and not cls in excluded_types |
690 | | - ) |
| 684 | + def _is_exported_type(cls): |
| 685 | + return ( |
| 686 | + inspect.isclass(cls) |
| 687 | + and issubclass(cls, opmod.Operator) |
| 688 | + and not inspect.isabstract(cls) |
| 689 | + and cls not in excluded_types |
| 690 | + ) |
| 691 | + |
691 | 692 | # create OperatorBuilder objects |
692 | | - for nm, opclass in inspect.getmembers(opmod, is_exported_type): |
| 693 | + for nm, opclass in inspect.getmembers(opmod, _is_exported_type): |
693 | 694 | op = opclass() |
694 | 695 | assert op.name, "Unnamed Operator should never appear here." |
695 | 696 | _builders[op.name] = OperatorBuilder(op.name, op) |
|
0 commit comments