-
-
Save wheerd/b57efd0dba2dbdba4bc10ff6e71cf5ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
How to use, Wildcard.optional()
?
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
I am getting the following error while running the code(I imported
symbols()
from sympy):