Create a gist now

Instantly share code, notes, and snippets.

@wheerd /matchpy_with_sympy.py Secret
Last active Jul 26, 2017

What would you like to do?
from matchpy import *
from sympy import Wild, Symbol, cacheit, Add, Pow, Basic, symbols
from matchpy.expressions.functions import register_operation_iterator, register_operation_factory
class WC(Wildcard, Symbol):
def __init__(self, min_length, fixed_size, variable_name=None, default=None, **assumptions):
Wildcard.__init__(self, min_length, fixed_size, variable_name, default)
def __new__(cls, min_length, fixed_size, variable_name=None, default=None, **assumptions):
cls._sanitize(assumptions, cls)
return WC.__xnew__(cls, min_length, fixed_size, variable_name, default, **assumptions)
def __getnewargs__(self):
return (self.min_length, self.fixed_size, self.variable_name, self.optional)
@staticmethod
@cacheit
def __xnew__(cls, min_length, fixed_size, variable_name=None, default=None, **assumptions):
obj = Symbol.__xnew__(cls, variable_name, **assumptions)
return obj
def _hashable_content(self):
return super()._hashable_content() + (self.min_count, self.fixed_size, self.variable_name, self.optional)
Operation.register(Add)
Operation.register(Pow)
CommutativeOperation.register(Add)
AssociativeOperation.register(Add)
OneIdentityOperation.register(Add)
register_operation_iterator(Add, lambda a: a._args, lambda a: len(a._args))
register_operation_iterator(Pow, lambda a: a._args, lambda a: len(a._args))
register_operation_factory(Basic, lambda op, args, variable_name: type(op)(*args))
x_ = WC(1, True, 'x', 1)
a, b = symbols('a b')
subject = 2
pattern = Pattern(x_ + 2)
print(list(match(subject, pattern)))

I am getting the following error while running the code(I imported symbols() from sympy):

Traceback (most recent call last):
  File "mpy.py", line 41, in <module>
    print(next(match(subject, pattern)))
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev21+gcccc5b8-py3.6.egg/matchpy/matching/one_to_one.py", line 45, in match
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev21+gcccc5b8-py3.6.egg/matchpy/matching/one_to_one.py", line 129, in _match
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev21+gcccc5b8-py3.6.egg/matchpy/matching/one_to_one.py", line 251, in _match_operation
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev21+gcccc5b8-py3.6.egg/matchpy/matching/one_to_one.py", line 221, in _non_commutative_match
  File "/Users/parsoyaarihant/anaconda/lib/python3.6/site-packages/matchpy-0.5.dev21+gcccc5b8-py3.6.egg/matchpy/matching/one_to_one.py", line 159, in _count_seq_vars
TypeError: object of type 'Pow' has no len()

How to use, Wildcard.optional()?

Owner

wheerd commented Jul 25, 2017

Sorry, I fixed that issue in the latest commit. What do you mean with Wildcard.optional()?

What do you mean with Wildcard.optional()?

I mean to ask how to use Wildcard.optional() in the above code(using class WC )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment