SVN: zope.testing/trunk/ - RENormalizing checkers can be combined via `` + `` now: ``checker1 +

Christian Zagrodnick <[email protected]>
Newsgroups gmane.comp.web.zope.zope3.cvs
Message-ID <20091206112157.C444A9417F__20552.249500952$1260098532$gmane$org@cvs.zope.org>
Log message for revision 106221:
  - RENormalizing checkers can be combined via `` + `` now: ``checker1 +
  checker2`` creates a checker with the transformations of both checkers.
  
  

Changed:
  U   zope.testing/trunk/CHANGES.txt
  U   zope.testing/trunk/src/zope/testing/renormalizing.py

-=-
Modified: zope.testing/trunk/CHANGES.txt
===================================================================
--- zope.testing/trunk/CHANGES.txt	2009-12-06 11:13:14 UTC (rev 106220)
+++ zope.testing/trunk/CHANGES.txt	2009-12-06 11:21:57 UTC (rev 106221)
@@ -9,6 +9,9 @@
 
 - Cleaned up unused imports reported by pyflakes.
 
+- RENormalizing checkers can be combined via `` + `` now: ``checker1 +
+checker2`` creates a checker with the transformations of both checkers.
+
 3.8.3 (2009-09-21)
 ==================
 

Modified: zope.testing/trunk/src/zope/testing/renormalizing.py
===================================================================
--- zope.testing/trunk/src/zope/testing/renormalizing.py	2009-12-06 11:13:14 UTC (rev 106220)
+++ zope.testing/trunk/src/zope/testing/renormalizing.py	2009-12-06 11:21:57 UTC (rev 106221)
@@ -220,6 +220,28 @@
         + <BLANKLINE>
           options:
               -h    display this help message
+
+
+It is possible to combine RENormalizing checkers for easy reuse:
+
+    >>> address_and_time_checker = RENormalizing([
+    ...    (re.compile('[0-9]*[.][0-9]* seconds'), '<SOME NUMBER OF> seconds'),
+    ...    (re.compile('at 0x[0-9a-f]+'), 'at <SOME ADDRESS>'),
+    ...    ])
+    >>> lowercase_checker = RENormalizing([
+    ...    lambda s: s.lower(),
+    ...    ])
+    >>> combined_checker = address_and_time_checker + lowercase_checker
+    >>> len(combined_checker.transformers)
+    3
+
+Combining a checker with something else does not work:
+
+    >>> lowercase_checker + 5
+    Traceback (most recent call last):
+        ...
+    TypeError: unsupported operand type(s) for +: 'instance' and 'int'
+
 """
 
 import doctest
@@ -231,6 +253,11 @@
     def __init__(self, patterns):
         self.transformers = map(self._cook, patterns)
 
+    def __add__(self, other):
+        if not isinstance(other, RENormalizing):
+            return NotImplemented
+        return RENormalizing(self.transformers + other.transformers)
+
     def _cook(self, pattern):
         if callable(pattern):
             return pattern
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.