Exemple #1
0
#!python

class ListInherited:
    """
    Use dir() to collect both instance attr and names inherited from 
    its classes;
    """

    def __attrnames(self):
        result = ''
        for attr in dir(self):
            if attr[:2] == '__' and attr[-2:] == '__':
                result += '\t%s\n' % attr
            else:
                result += '\t%s=%s\n' % (attr, getattr(self, attr))
        return result

    def __str__(self):
        return '<Instance of %s, address %s:\n%s>' % (self.__class__.__name__, id(self), self.__attrnames())

if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListInherited)

class ListInherited:
    """
    Use dir() to collect both instance attrs and names inherited
    from its classes; Python 3.X shows more names than 2.X because
    of the implied object superclass in the new style class model;
    getattr() fetches inherited names not in self.__dict__;
    use __str__ and not __repr__ or else it loops when printing bound methods
    """
    def __attrnames(self):
        result = ''
        for attr in dir(self):  # instance dir
            if attr[:2] == '__' and attr[-2:] == '__':  # skip internals
                result += '\t%s\n' % attr
            else:
                result += '\t%s=%s\n' % (attr, getattr(self, attr))
        return result

    def __str__(self):
        return 'Instance of %s, address %s\n%s' % (
            self.__class__.__name__,  # class name
            id(self),  # address
            self.__attrnames()  # name = value list
        )


if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListInherited)
                result += spaces + '{0}\n'.format(attr)
            else:
                result += spaces + '{0}={1}\n'.format(attr, getattr(obj, attr))
        return result

    def __listclass(self, aClass, indent):
        dots = '.' * indent
        if aClass in self.__visited:
            return '\n{0}<Class {1}:, address {2}: (see above)>\n'.format(
                dots, aClass.__name__, id(aClass))
        else:
            self.__visited[aClass] = True
            here = self.__attrnames(aClass, indent)
            above = ''
            for super in aClass.__bases__:
                above += self.__listclass(super, indent + 4)
            return '\n{0}<Class {1}, address {2}:\n{3}{4}{5}>\n'.format(
                dots, aClass.__name__, id(aClass), here, above, dots)

    def __str__(self):
        self.__visited = {}
        here = self.__attrnames(self, 0)
        above = self.__listclass(self.__class__, 4)
        return '<Instance of {0}, address {1}:\n{2}{3}>'.format(
            self.__class__.__name__, id(self), here, above)


if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListTree)
Exemple #4
0
#!python
# File listinstance.py (2.X + 3.X)


class ListInstance:
    """
    Mix-in class that provides a formatted print() or str() of instances via 
    inheritance of __str__ coded here;  displays instance attrs only;  self is
    instance of lowest class; __X names avoid clashing with client's attrs
    """
    def __attrnames(self):
        result = ''
        for attr in sorted(self.__dict__):
            result += '\t%s=%s\n' % (attr, self.__dict__[attr])
        return result

    def __str__(self):
        return '<Instance of %s, address %s:\n%s>' % (
            self.__class__.__name__,  # My class's name
            id(self),  # My address
            self.__attrnames())  # name=value list


if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListInstance)
    def __listclass(self, aClass, indent):
        dots = '.' * indent
        if aClass in self.__visited:
            return '\n{0}<Class {1}:, address {2}: (see above)>\n'.format(
                           dots,
                           aClass.__name__,
                           id(aClass))
        else:
            self.__visited[aClass] = True
            genabove = (self.__listclass(c, indent+4) for c in aClass.__bases__)
            return '\n{0}<Class {1}, address {2}:\n{3}{4}{5}>\n'.format(
                           dots,
                           aClass.__name__,
                           id(aClass),
                           self.__attrnames(aClass, indent),
                           ''.join(genabove),
                           dots)

    def __str__(self):
        self.__visited = {}
        return '<Instance of {0}, address {1}:\n{2}{3}>'.format(
                           self.__class__.__name__,
                           id(self),
                           self.__attrnames(self, 0),
                           self.__listclass(self.__class__, 4))

if __name__ == '__main__': 
    import testmixin
    testmixin.tester(ListTree)
#!python
# file listinstance.py

class ListInstance:
    '''
    Mix-in class that provides a formatted print() or str() of instances via inheritance
    of __str__ coded here; displays instance attrs only; self is instance of lowest class;
    __X names avoid clashing wiht client's attrs
    '''
    def __attrnames(self):
        result = ''
        for attr in sorted(self.__dict__):
            result += '\t%s=%s\n' % (attr, self.__dict__[attr])
        return result

    def __str__(self):
        return  '<Instance of %s, address %s"\n%s>' % (
                self.__class__.__name__,
                id(self),
                self.__attrnames())


if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListInstance)
class ListInstance:
    """
	document string
	"""
    def __attrnames(self):
        '''
		result = ''
		for attr in sorted(self.__dict__):
			result += '\t%s=%s\n' % (attr,self.__dict__[attr])
		return result
		'''
        return ''.join('\t%s=%s\n' % (attr, self.__dict__[attr])
                       for attr in sorted(self.__dict__))

    def __str__(self):
        return '<Instance of %s, address %s:\n%s>' % (
            self.__class__.__name__, id(self), self.__attrnames())


if __name__ == '__main__':
    import testmixin
    testmixin.tester(ListInsurance)
Exemple #8
0
class SuperLister:
    def __attrnames(self): print('To Be implemented')
    def __str__(self):
        return '<Instance of %s(%s), address %s:\n%s>' % (
                           self.__class__.__name__,         # My class's name
                           self.__class__.__bases__,        # Names of superclasses
                           id(self),                        # My address
                           self.attrnames())              # name=value list

class SLInstance(SuperLister,ListInstance):
    def attrnames(self):
        return ListInstance.__attrnames(self)
    def __str__(self):
        return SuperLister.__str__(self)

class SLInherited(ListInherited, SuperLister):
    def attrnames(self):
        return ListInherited.__attrnames(self)
    def __str__(self):
        return SuperLister.__str__(self)

class SLTree(ListTree, SuperLister):
    def __attrnames(self):
        return ListTree.__attrnames(self)
    def __str__(self):
        return SuperLister.__str__(self)

tester(SLInstance)
tester(SLInherited)
tester(SLTree)