-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlabel_v6.py
46 lines (34 loc) · 1.39 KB
/
label_v6.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
from functional import range_
import collections, jenums
AX = jenums.Axes
_attrl = [AX.P, AX.CH, AX.SB, AX.FQ, AX.BL, AX.SRC, AX.TIME, AX.TYPE]
_attrf = [AX.P, AX.CH, AX.SB, AX.FQ, AX.BL, AX.SRC, AX.TIME]
_nattrf = range_(len(_attrf))
BaseClass = collections.namedtuple('BaseClass', _attrl)
_nones = [None]*len(_attrl)
_defaults = dict((zip(_attrl, _nones)))
fmtStr = "{1}".format
fmtInt = "{0}{1}".format
# AX.P AX.CH AX.SB AX.FQ AX.BL AX.SRC AX.TIME
_fmt = [fmtStr, fmtInt, fmtInt, fmtStr, fmtStr, fmtStr, fmtStr]
_fmtDict = dict((zip(_attrf, _fmt)))
def getitem(o, a):
return o[a]
def getattr_v(o, a):
return getattr(o, a.value)
class label(BaseClass):
@classmethod
def format(self, kv):
return "/".join((_fmtDict[k](k, v) for k, v in kv))
__slots__ = ()
_attrs = set(_attrl)
def __new__(_cls, kwdict, which):
accf = getitem if type(kwdict) is dict else getattr_v
kwds = dict(((k, accf(kwdict,k)) for k in which))
return tuple.__new__(_cls, map(kwds.pop, _attrl, _nones))
def key(self):
return tuple(((_attrl[n], self[n]) for n in _nattrf if self[n] is not None))
def attrs(self, which):
return tuple((w, getattr(self, w.value)) for w in which)
def __repr__(self):
return "/".join( [_fmt[n](_attrf[n], self[n]) for n in _nattrf if self[n] is not None] )