File: Lister.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (35 lines) | stat: -rw-r--r-- 930 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding=utf-8
"""
Pretty-printer for SimPy class objects

"""
class Lister(object):

    indent = 0

    def __str__(self):
        Lister.indent += 1
        if Lister.indent > 3:
            # In case of recursion, avoid infinite loop
            result = ' ... '
        else:
            result = '< Instance of %s, id %s:\n%s%s>' % (
                self.__class__.__name__,
                id(self),
                self.attrnames(),
                '\t' * (Lister.indent - 1),
            )
        Lister.indent -= 1
        return result

    def attrnames(self):
        result = ''
        for attr in self.__dict__:
            # Ignore built-in and private attributes
            if not (attr[:2] == '__' or attr[0] == '_'):
                result += '\t' * Lister.indent + '.%s=%s\n' % (attr,
                        self.__dict__[attr])
        return result

    def __repr__(self):
        return self.__str__()