-
Notifications
You must be signed in to change notification settings - Fork 0
/
pychoice.py
64 lines (58 loc) · 2.1 KB
/
pychoice.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
class Error(ValueError):
def __init__(self, item, container_name):
self.item = item
self.container_name = container_name
def __str__(self):
return '{item} does not all exist in {container_name}'.format(item=self.item,
container_name=self.container_name)
class Choice:
def __init__(self, *pairs):
self._choices = pairs
def __getitem__(self, index):
if index is Ellipsis:
return [n for i, n in self._choices]
elif isinstance(index, tuple):
rv = [n for i, n in self._choices if i in index]
if len(rv) != len(index):
raise Error(index, self.__class__.__name__)
return rv
else:
for i, n in self._choices:
if i == index:
return n
raise Error(index, self.__class__.__name__)
def __call__(self, *names):
if len(names) == 0:
return [i for i, n in self._choices]
elif len(names) == 1:
name = names[0]
for i, n in self._choices:
if n == name:
return i
raise Error(name, self.__class__.__name__)
else:
rv = [i for i, n in self._choices if n in names]
if len(rv) != len(names):
raise Error(names, self.__class__.__name__)
return rv
def exclude(self, *names):
rv = []
is_in_choice = [False for x in names]
idx = 0
for i, n in self._choices:
if n not in names:
rv.append(i)
else:
is_in_choice[idx] = True
idx += 1
if not all(is_in_choice):
raise Error(names, self.__class__.__name__)
return rv
def pairs(self, *names):
if names:
rv = [(i, n) for i, n in self._choices if n in names]
if len(rv) != len(names):
raise Error(names, self.__class__.__name__)
else:
rv = [(i, n) for i, n in self._choices]
return rv