From a96a0f977611d907197280ef905475bfb15f601b Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 7 Jan 2018 20:25:48 -0600 Subject: [PATCH] Add .__repr__ to match expressions --- loopy/match.py | 49 +++++++++++++++++++++++++----------------------- loopy/version.py | 2 +- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/loopy/match.py b/loopy/match.py index ab0038af8..a417c2799 100644 --- a/loopy/match.py +++ b/loopy/match.py @@ -134,6 +134,12 @@ class All(MatchExpressionBase): def __call__(self, kernel, matchable): return True + def __str__(self): + return "all" + + def __repr__(self): + return "%s()" % (type(self).__name__) + def update_persistent_hash(self, key_hash, key_builder): key_builder.rec(key_hash, "all_match_expr") @@ -144,18 +150,21 @@ class All(MatchExpressionBase): return hash(type(self)) -class And(MatchExpressionBase): +class MultiChildMatchExpressionBase(MatchExpressionBase): def __init__(self, children): self.children = children - def __call__(self, kernel, matchable): - return all(ch(kernel, matchable) for ch in self.children) - def __str__(self): - return "(%s)" % (" and ".join(str(ch) for ch in self.children)) + joiner = " %s " % type(self).__name__.lower() + return "(%s)" % (joiner.join(str(ch) for ch in self.children)) + + def __repr__(self): + return "%s(%s)" % ( + type(self).__name__, + ", ".join(repr(ch) for ch in self.children)) def update_persistent_hash(self, key_hash, key_builder): - key_builder.rec(key_hash, "and_match_expr") + key_builder.rec(key_hash, type(self).__name__) key_builder.rec(key_hash, self.children) def __eq__(self, other): @@ -166,26 +175,14 @@ class And(MatchExpressionBase): return hash((type(self), self.children)) -class Or(MatchExpressionBase): - def __init__(self, children): - self.children = children - +class And(MultiChildMatchExpressionBase): def __call__(self, kernel, matchable): - return any(ch(kernel, matchable) for ch in self.children) - - def __str__(self): - return "(%s)" % (" or ".join(str(ch) for ch in self.children)) - - def update_persistent_hash(self, key_hash, key_builder): - key_builder.rec(key_hash, "or_match_expr") - key_builder.rec(key_hash, self.children) + return all(ch(kernel, matchable) for ch in self.children) - def __eq__(self, other): - return (type(self) == type(other) - and self.children == other.children) - def __hash__(self): - return hash((type(self), self.children)) +class Or(MultiChildMatchExpressionBase): + def __call__(self, kernel, matchable): + return any(ch(kernel, matchable) for ch in self.children) class Not(MatchExpressionBase): @@ -198,6 +195,9 @@ class Not(MatchExpressionBase): def __str__(self): return "(not %s)" % str(self.child) + def __repr__(self): + return "%s(%r)" % (type(self).__name__, self.child) + def update_persistent_hash(self, key_hash, key_builder): key_builder.rec(key_hash, "not_match_expr") key_builder.rec(key_hash, self.child) @@ -222,6 +222,9 @@ class GlobMatchExpressionBase(MatchExpressionBase): descr = type(self).__name__ return descr.lower() + ":" + self.glob + def __repr__(self): + return "%s(%r)" % (type(self).__name__, self. glob) + def update_persistent_hash(self, key_hash, key_builder): key_builder.rec(key_hash, type(self).__name__) key_builder.rec(key_hash, self.glob) diff --git a/loopy/version.py b/loopy/version.py index a0839c2c4..d5d50a0fe 100644 --- a/loopy/version.py +++ b/loopy/version.py @@ -32,4 +32,4 @@ except ImportError: else: _islpy_version = islpy.version.VERSION_TEXT -DATA_MODEL_VERSION = "v73-islpy%s" % _islpy_version +DATA_MODEL_VERSION = "v74-islpy%s" % _islpy_version -- GitLab