On Fri, Jul 20, 2007 at 07:57:12AM -0400, Jim Fulton wrote: > On Jul 20, 2007, at 5:27 AM, Philipp von Weitershausen wrote: > >I suggesting using the regex-normalizer [1]. There are many > >packages out there that use it if you're looking for examples. > > > >http://svn.zope.org/zope.testing/trunk/src/zope/testing/renormalizing.py?rev=66267&view=auto > > BTW, a small useful easy project that I don't have time for would be > to generalize the renormalizer to allow other objects besides regexes.
It already does, in a way. It doesn't call re.compile by itself, so you can pass it anything that implements a ``sub`` method, not necessarily regexes. class LowercaseTransformer(object): def sub(replacement, text): return text.lower() checker = RENormalizing([(LowercaseTransformer(), None)]) > You now give this a sequences of regex/replacement pairs. > > It would be nice to allow callables in addition to regexs. > Basically, allow items in the sequence to be either: > > - a regex+replacement tuple, or > > - a callable transformer that reads the text and returns new text. So essentially you want nicer syntax for the above. Should be easy to do: Index: renormalizing.py =================================================================== --- renormalizing.py (revision 78151) +++ renormalizing.py (working copy) @@ -181,15 +181,21 @@ class RENormalizing(doctest.OutputChecke """ def __init__(self, patterns): - self.patterns = patterns + self.transformers = map(self._cook, patterns) + + def _cook(self, pattern): + if callable(pattern): + return pattern + regexp, replacement = pattern + return lambda text: regexp.sub(replacement, text) def check_output(self, want, got, optionflags): if got == want: return True - for pattern, repl in self.patterns: - want = pattern.sub(repl, want) - got = pattern.sub(repl, got) + for transformer in self.transformers: + want = transformer(want) + got = transformer(got) return doctest.OutputChecker.check_output(self, want, got, optionflags) @@ -208,9 +214,9 @@ class RENormalizing(doctest.OutputChecke # Dang, this isn't as easy to override as we might wish original = want - for pattern, repl in self.patterns: - want = pattern.sub(repl, want) - got = pattern.sub(repl, got) + for transformer in self.transformers: + want = transformer(want) + got = transformer(got) # temporarily hack example with normalized want: example.want = want Do you want me to write unit tests for this and commit? Marius Gedminas -- C is quirky, flawed, and an enormous success. -- Dennis M. Ritchie
signature.asc
Description: Digital signature
_______________________________________________ Zope3-dev mailing list Zope3-dev@zope.org Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com