Patch to clear RuleSet
Alberto Valverde <[email protected]>
| Newsgroups | gmane.comp.python.peak |
|---|---|
| Message-ID | <[email protected]> |
Hi Phillip, It has been requested in turbogears-trunk [1] for a mechanism to clear all rules in a generic function like the old RuleDispatch's GenericFunction.clear() method. I've created a patch against the latest trunk that implements this. Maybe you want to consider adding it. Thanks for PEAK-Rules, the more I'm studying it the more I'm liking it :) Alberto [1] http://groups.google.com/group/turbogears-trunk/msg/0b97960dd743547d?hl=en _______________________________________________ PEAK mailing list [email protected] http://www.eby-sarna.com/mailman/listinfo/peak
clear_rules.diff
(text/x-diff, 2.7 KB)
Index: peak/rules/core.py
===================================================================
--- peak/rules/core.py (revision 2565)
+++ peak/rules/core.py (working copy)
@@ -4,7 +4,8 @@
'DispatchError', 'AmbiguousMethods', 'NoApplicableMethods',
'abstract', 'when', 'before', 'after', 'around', 'istype', 'parse_rule',
'implies', 'dominant_signatures', 'combine_actions', 'overrides',
- 'always_overrides', 'merge_by_default', 'intersect', 'disjuncts'
+ 'always_overrides', 'merge_by_default', 'intersect', 'disjuncts',
+ 'clear_all_rules',
]
from peak.util.decorators import decorate_assignment, decorate, struct, synchronized, frameinfo, decorate_class
from peak.util.assembler import Code, Const, Call, Local, Getattr, TryExcept, Suite, with_name
@@ -858,4 +859,37 @@
return best
+def clear_all_rules(func):
+ """
+ Clears all rules that have been registered for a function
+ Example::
+
+ >>> from peak.rules.core import *
+
+ >>> @abstract
+ ... def f(n): pass
+
+ >>> when(f, "n>5")(lambda n: n+1) # doctest: +ELLIPSIS
+ <function <lambda> at ...>
+
+ >>> try:
+ ... f(5)
+ ... assert False, "This should have failed"
+ ... except NoApplicableMethods:
+ ... pass
+
+ >>> assert f(6) == 7
+
+ >>> clear_all_rules(f)
+ >>> try:
+ ... f(6)
+ ... assert False, "This should have failed"
+ ... except NoApplicableMethods:
+ ... pass
+
+ """
+ rs = Dispatching(func).rules
+ for rule in rs.rules:
+ rs.remove(rule)
+ Dispatching(func).request_regeneration()
Index: peak/rules/dispatch.py
===================================================================
--- peak/rules/dispatch.py (revision 2565)
+++ peak/rules/dispatch.py (working copy)
@@ -21,6 +21,7 @@
value.around = core.around.__get__(value)
value.before = core.before.__get__(value)
value.after = core.after.__get__(value)
+ value.clear = core.clear_all_rules.__get__(value)
return core.abstract(value)
return decorate_assignment(callback)
Index: test_rules.py
===================================================================
--- test_rules.py (revision 2565)
+++ test_rules.py (working copy)
@@ -624,7 +624,7 @@
files = [
'README.txt', 'DESIGN.txt', 'Indexing.txt', 'AST-Builder.txt',
'Code-Generation.txt', 'Syntax-Matching.txt', 'Criteria.txt',
- 'Predicates.txt',
+ 'Predicates.txt', 'peak/rules/core.py',
][sys.version<'2.4':] # skip README.txt on 2.3 due to @ syntax
return doctest.DocFileSuite(
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE, *files