Pending: A Mixin class for testing

Scott David Daniels scott.daniels at acm.org
Mon Nov 27 00:26:16 CET 2006


Here is a Mix-in class I just built for testing.
It is quite simple, but illustrates how Mixins
can be used.

     class Pending(object):
         _pending = iter(())
         def __new__(class_, *args, **kwargs):
             try:
                 return class_._pending.next()
             except StopIteration:
                 return super(Pending, class_).__new__(class_,
                                                *args, **kwargs)

Now for the use:

     class Float(Pending, float): pass

     Float._pending = iter(range(4,7))

     print [Float(x*(x+1)//2) for x in range(6)]

Possibly by using itertools functions such as chain as in:
     Klass._pending = itertools.chain(generate_some(), Klass._pending)
you can inject fixed values to simplify testing.

Or something like this:

      class SomeTrickyClass(...):  # new-style only (derived from object)
          ...

      class Hacked(Pending, SomeTrickyClass):
          _pending = sample_value_generator()

      TempHold, SomeTrickyClass = SomeTrickyClass, Hacked
      try:
          <do the test>
      finally:
          SomeTrickyClass = TempHold


--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list