-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswizzle.py
81 lines (63 loc) · 2.21 KB
/
swizzle.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class CommaFreeError(Exception):
pass
class SwizzleMeta(type):
def __new__(self, name, bases, attrs):
old = attrs['__init__']
slots = set(attrs['__slots__'])
def init_wrapper(self, *args, **kwargs):
old(self, *args, **kwargs)
for x in slots:
for z in slots - {x}:
for y in slots - {x, z}:
if y in x+z:
raise CommaFreeError(y+" is in "+x+z)
attrs['__init__'] = init_wrapper
return super().__new__(self, name, bases, attrs)
class Swizzler(object, metaclass=SwizzleMeta):
__slots__ = []
def __getattr__(self, attr):
attrs = (a for a in set(type(self).__slots__) if a in attr)
attrs = sorted(attrs, key=lambda a: attr.index(a))
return tuple(self.__getattribute__(a) for a in attrs)
def __setattr__(self, key, value):
attrs = [a for a in set(type(self).__slots__) if a in key]
if len(attrs) in (1, 0):
super().__setattr__(key, value)
else:
pairs = zip(sorted(attrs, key=lambda a: key.index(a)), value)
for k, v in pairs:
super().__setattr__(k, v)
def __init__(self):
pass
if __name__ == "__main__":
class Vec4(Swizzler):
__slots__ = ["x", "y", "z", "w"]
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
a = Vec4(1,2,3,4)
b = Vec4(5,6,7,8)
print(b.xyzw)
print("should be 5, 6, 7, 8")
b.xyz = a.yzw
print(b.xyzw)
print("should be 2, 3, 4, 8")
class ComplexSwizz(Swizzler):
__slots__ = ["hello", "world"]
def __init__(self, hello, world):
self.hello = hello
self.world = world
c = ComplexSwizz("hi", "mundo")
d = ComplexSwizz("Aloha", "word")
c.worldhello = d.helloworld
print(c.helloworld)
print("should be word, Aloha")
class ErrSwizz(Swizzler):
__slots__ = ["cat", "attack", "loc"]
def __init__(self, cat, attack, loc):
self.cat = cat
self.attack = attack
self.loc = loc
e = ErrSwizz("bang", "boom", "pow")