/
structClass.py
66 lines (52 loc) · 1.97 KB
/
structClass.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
######################################################
# Structs (save in structClass.py (do not save in struct.py!))
######################################################
# Here we use 'struct' in the classic sense:
# http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29
# Note that this is not the same as Python's struct module:
# https://docs.python.org/2/library/struct.html
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
d = self.__dict__
results = [self.__class__.__name__ + "("]
for key in sorted(d.keys()):
if (len(results) > 1): results.append(", ")
results.append(key + "=" + repr(d[key]))
results.append(")")
return "".join(results)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __hash__(self):
return hash(repr(self)) # inefficient but simple
def testStructClass():
print "Testing Struct class...",
obj = Struct()
obj.x = 42
assert(obj.x == 42)
obj = Struct(x=2, b=True, s="abc")
assert((obj.x == 2) and (obj.b == True) and (obj.s == 'abc'))
obj.x = 3
assert(obj.x == 3)
assert(str(obj) == "Struct(b=True, s='abc', x=3)") # alphabetical keys
assert(str([obj]) == "[Struct(b=True, s='abc', x=3)]")
obj2 = eval(repr(obj))
assert(obj == obj2)
assert(obj is not obj2) # they are equal, but not aliases
s = set()
assert(obj not in s)
s.add(obj)
assert(obj in s)
assert(obj2 in s) # since (obj == obj2)
obj.x += 1 # obj is mutable, which is bad, because...
assert(obj not in s) # sigh...
class Person(Struct): pass
joe = Person(name="Joe", age=42)
assert(str(joe) == "Person(age=42, name='Joe')")
assert(type(joe) == Person)
assert(isinstance(joe, Person) == True)
assert(isinstance(joe, Struct) == True)
print "Passed!"
if __name__ == "__main__":
testStructClass()