-
Notifications
You must be signed in to change notification settings - Fork 1
/
phredstruct.py
72 lines (53 loc) · 2.13 KB
/
phredstruct.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
from caching import cache
@cache(key='positional')
def struct(*attrs):
class Struct:
names = attrs
def __init__(self, *args, **kwargs):
# asserts may be compiled away with the -O flag
assert self.validate(*args, **kwargs) is None
values = []
positionals = iter(args)
for name in self.names:
try:
value = kwargs.pop(name)
except KeyError:
value = next(positionals)
values.append(value)
super().__setattr__('values', tuple(values))
super().__setattr__('items', tuple(zip(self.names, self.values)))
super().__setattr__('_attrs', dict(self.items))
def validate(self, *args, **kwargs):
nargs = len(args)
nkwargs = len(kwargs)
if nargs + nkwargs != len(self.names):
raise AttributeError(
'wrong number of args: got {} positional '
'and {} keyword; expected {} total'
.format(nargs, nkwargs, len(self.names)))
invalid_kwargs = kwargs.keys() - set(self.names)
if invalid_kwargs:
raise AttributeError(
'invalid kwargs: {}'
.format(invalid_kwargs))
def __getattr__(self, name):
try:
return self._attrs[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
raise NotImplementedError
def __delattr__(self, name):
raise NotImplementedError
def __iter__(self):
return iter(self.values)
def __len__(self):
return len(self.values)
def __eq__(self, other):
return self.names == other.names and self.values == other.values
def __hash__(self):
return hash(self.values)
def __repr__(self):
reprs = ['{}={!r}'.format(k, v) for k, v in self.items]
return '{}({})'.format(self.__class__.__name__, ', '.join(reprs))
return Struct