All pastes #820566 Raw Edit

Putifuto

public python v1 · immutable
·published 2007-12-17 17:26 UTC
rendered paste body
import operatorclass Multicast(list):    """ List with multicast facilities.    Usage:    >>> m=Multicast(["plop", "2"])    >>> m.upper()    ['PLOP', '2']    >>> m.isdigit()    [False, True]    >>> bool(m.isdigit())    False    """    def __call__(self, *args, **kwargs):        "Invoke method attributes and return results through another Multicast"        return self.__class__( [ obj(*args, **kwargs)  for obj in self ] )    def __nonzero__(self):        """ A Multicast is logically true if all delegate attributes are        logically true """        return return operator.truth(reduce(lambda a,b: a and b, self, 1)) #all(self)    def __getattr__(self, name):        "Wrap requested attributes for further processing"        return self.__class__( [ getattr(obj, name) for obj in self ] )if __name__ == "__main__":    import doctest    doctest.testmod()    import unittest    import StringIO    class TestMultiCast(unittest.TestCase):        """ Test Multicast class """        def setUp(self):            self.multicast = Multicast()            self.file1 = StringIO.StringIO()            self.file2 = StringIO.StringIO()            self.multicast += [ self.file1, self.file2 ]        def test_0_prereqires(self):            self.failIf(len(self.multicast)!=2)            self.failUnless(isinstance(self.multicast, list))        def test_1_NotClose(self):            self.failIf(self.multicast.closed != [0,0])        def test_2_Write(self):            self.multicast.write("Testing")            self.failUnlessEqual(self.file1.getvalue(),                                self.file2.getvalue())            self.failUnlessEqual(self.file1.getvalue(), "Testing" )        def test_3_Close(self):            self.file1.close()            self.failIf(self.multicast.closed != [1,0])            self.multicast.close()            self.failIf(self.multicast.closed != [1,1])            self.failUnless(bool(self.multicast.closed)==True)                        unittest.main()