Closed arihantparsoya closed 7 years ago
Unfortunately, currently all subexpressions must be subclasses of Expression
as well. As a workaround,
you could use a custom Symbol
subclass for constants:
class ConstantSymbol(Symbol):
def __init__(self. value):
super().__init__(str(value))
self.value = value
I am planing on making the expressions more flexible, so that the matching works with any types and not just Expression
subclasses.
Thanks @wheerd .
New release of matchpy does not resolve this issue:
>>> from matchpy import *
>>> Plus = Operation.new('+', Arity.variadic, 'Plus', associative=True, one_identity=True, infix=True)
>>> Plus(1, -1).symbols
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/utils.py", line 587, in __get__
File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/expressions/expressions.py", line 113, in symbols
File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.3-py3.6.egg/matchpy/expressions/expressions.py", line 568, in collect_symbols
AttributeError: 'int' object has no attribute 'collect_symbols'
Oh, all the properties of expressions do not work when mixing Expression-subclasses and other types.
This is because something like int does not implement the needed methods.
However, there is a workaround that would solve that problem. For example, you can use the following code to find out if an expression contains a 1
:
from matchpy.expressions.functions import preorder_iter
if any(subexpr == 1 for subexpr in preorder_iter(expr)):
print('Contains 1')
I need to release a new version so that the __contains__
method works with mixed types, too.
The __contains__
method is fixed in version 0.3.1. I will look into the remaining attributes/methods of Expression
s as well, but you should probably just use the functions in matchpy.expressions.functions
if you are using mixed types. x.symbols
will still fail if x
happens to be matched with an int
instead of an Expression
.
I am unable to use
int
objects inReplacementRule
:Is there any alternative way to use
int
orfloat
in expressions?