symbol.py 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. from sympy.external import import_module
  2. matchpy = import_module("matchpy")
  3. from sympy.utilities.decorator import doctest_depends_on
  4. from sympy.core.symbol import Symbol
  5. from sympy.utilities.matchpy_connector import Wildcard
  6. @doctest_depends_on(modules=('matchpy',))
  7. class matchpyWC(Wildcard, Symbol):
  8. def __init__(self, min_length, fixed_size, variable_name=None, optional=None, **assumptions):
  9. Wildcard.__init__(self, min_length, fixed_size, str(variable_name), optional)
  10. def __new__(cls, min_length, fixed_size, variable_name=None, optional=None, **assumptions):
  11. cls._sanitize(assumptions, cls)
  12. return matchpyWC.__xnew__(cls, min_length, fixed_size, variable_name, optional, **assumptions)
  13. def __getnewargs__(self):
  14. return (self.min_count, self.fixed_size, self.variable_name, self.optional)
  15. @staticmethod
  16. def __xnew__(cls, min_length, fixed_size, variable_name=None, optional=None, **assumptions):
  17. obj = Symbol.__xnew__(cls, variable_name, **assumptions)
  18. return obj
  19. def _hashable_content(self):
  20. if self.optional:
  21. return super()._hashable_content() + (self.min_count, self.fixed_size, self.variable_name, self.optional)
  22. else:
  23. return super()._hashable_content() + (self.min_count, self.fixed_size, self.variable_name)
  24. @doctest_depends_on(modules=('matchpy',))
  25. def WC(variable_name=None, optional=None, **assumptions):
  26. return matchpyWC(1, True, variable_name, optional)