class OOO (object):
    def __init__ (i, **a): i.__dict__ = a
    def __repr__ (i):
        a = i.__dict__
        return "%s (%s)" % (type (i).__name__, ", ".join (
            "%s=%r" % (o, a [o]) for o in sorted (a.iterkeys ())
        ))

class OO (object):
    def __init__ (i, *o, **a):
        if o:
            p = i.__slots__
            for x in xrange (len (o)):
                setattr (i, p [x], o [x])
        for x in a: setattr (i, x, a [x])
    def __repr__ (i):
        a = list ()
        q = set ()
        try: o = i.__slots__
        except: pass
        else:
            j = 0
            for x in o:
                if x in q: break
                try: a.append (repr (getattr (i, x)))
                except: break
                q.add (x)
                j += 1
            for j in xrange (j, len (o)):
                x = o [j]
                if x not in q:
                    try: a.append ("%s=%r" % (x, getattr (i, x)))
                    except: pass
                    q.add (x)
        try:
            for x, o in sorted (i.__dict__.iteritems ()):
                if x not in q:
                    a.append ("%s=%r" % (x, o))
                    q.add (x)
        except: pass
        return "%s (%s)" % (type (i).__name__, ", ".join (a))

class RGB (OO):
    __slots__ = ["r", "g", "b"]
    def __str__ (i):
        return "#%02X%02X%02X" % (i.r, i.g, i.b)

x = [
    RGB (0, 0, 0, name = "black"),
    RGB (255, 0, 0, name = "red"),
    RGB (0, 255, 0, name = "green"),
    RGB (0, 0, 255, name = "blue"),
    RGB (0, 255, 255, name = "cyan"),
    RGB (255, 0, 255, name = "magenta"),
    RGB (255, 255, 0, name = "yellow"),
    RGB (255, 255, 255, name = "white")
    ]

file ("marshal.txt", "w").write ("\n".join (
    ["[", ",\n".join (repr (o) for o in x), "]", ""]))

y = eval (file ("marshal.txt").read ())
print y
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to