forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheach.py
More file actions
196 lines (168 loc) Β· 5.07 KB
/
each.py
File metadata and controls
196 lines (168 loc) Β· 5.07 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from functools import partial, partialmethod
from operator import attrgetter, itemgetter, methodcaller
class each:
"""Generator expressions? Too verbose.
>>> each('123', int)
each([1, 2, 3])
>>> each(range(5)).real
each([0, 1, 2, 3, 4])
>>> sum(each(range(5)).imag)
0
>>> ''.join(each('abc').upper())
'ABC'
>>> each('123').to(int)
each([1, 2, 3])
>>> each(['123', '456'])[1].to(int)
each([2, 5])
>>> each('abc') == 'a'
each([True, False, False])
>>> each(range(5)) < 3
each([True, True, True, False, False])
>>> 3 > each(range(5))
each([True, True, True, False, False])
>>> each(range(5)) >= 3
each([False, False, False, True, True])
>>> abs(each(range(-3, 3)))
each([3, 2, 1, 0, 1, 2])
>>> each([range(1), range(2)]).contains(0, 1)
each([False, True])
>>> each(range(5)).is_in(range(1, 3))
each([False, True, True, False, False])
>>> e = each([{'a': 1}, {'a': 2}])
>>> e
each([{'a': 1}, {'a': 2}])
>>> e.values().contains(1)
each([True, False])
>>> e['a']
each([1, 2])
>>> e['b'] = 2
>>> del e['a']
>>> e
each([{'b': 2}, {'b': 2}])
>>> e['b'] += 1
Traceback (most recent call last):
...
NotImplementedError: <class 'each.each'> does not support __iadd__
"""
def __init__(self, iterable, effect=None):
self.__effect = effect
self.__it = iterable
def to(self, func, *args, **kwargs):
return self.__class__(self, partial(func, *args, **kwargs))
def contains(self, *values, fold=all):
def effect(self):
return fold(value in self for value in values)
return self.to(effect)
def is_in(self, *containers, fold=all):
def effect(self):
return fold(self in container for container in containers)
return self.to(effect)
def broadcast(self, name):
"""Call this method to access overloaded names.
>>> each('spam').to
<bound method each.to of each(['s', 'p', 'a', 'm'])>
>>> each('spam').broadcast('to')
Traceback (most recent call last):
...
AttributeError: 'str' object has no attribute 'to'
"""
return self.to(attrgetter(name))
__getattr__ = broadcast
def __setattr__(self, name, value):
if name.startswith('_'):
# TODO: move to __init__?
return super().__setattr__(name, value)
for element in self:
setattr(self, name, value)
def __delattr__(self, name):
if name.startswith('_'):
# TODO: remove?
return super().__delattr__(name)
for element in self:
delattr(self, name)
def __getitem__(self, key):
return self.to(itemgetter(key))
def __setitem__(self, name, value):
for element in self:
element[name] = value
def __delitem__(self, name):
for element in self:
del element[name]
def __iter__(self):
if self.__effect is None:
yield from self.__it
else:
for thing in self.__it:
yield self.__effect(thing)
def _apply(self, name, *args, **kwargs):
# TODO: Joe says use operator.X instead of methodcaller
return self.to(methodcaller(name, *args, **kwargs))
def __repr__(self):
return 'each([{}])'.format(', '.join(self.to(repr)))
_broadcast_methods = [
'__lt__',
'__le__',
'__eq__',
'__ne__',
'__ge__',
'__gt__',
'__abs__',
'__add__',
'__and__',
'__call__',
# '__concat__', # Not actually a real special method
# '__contains__', # `in` casts the return value to bool
'__divmod__',
'__floordiv__',
# '__index__', # Must return an int
'__inv__',
'__invert__',
# '__len__', # Must return an int
'__lshift__',
'__mod__',
'__mul__',
'__matmul__',
'__neg__',
'__or__',
'__pos__',
'__pow__',
'__rshift__',
'__sub__',
'__truediv__',
'__xor__',
# TODO: ?
# '__missing__',
# '__reversed__',
# TODO: do I dare?
# '__enter__',
# '__exit__',
# TODO: metaclass with these:
# '__instancecheck__',
# '__subclasscheck__',
]
for name in _broadcast_methods:
locals()[name] = partialmethod(_apply, name)
del name
def _nope(self, name, *args, **kwargs):
raise NotImplementedError(
'{0.__class__} does not support {1}'.format(self, name))
# TODO: make these work
_unsupported_methods = [
'__iadd__',
'__iand__',
'__iconcat__',
'__ifloordiv__',
'__ilshift__',
'__imod__',
'__imul__',
'__imatmul__',
'__ior__',
'__ipow__',
'__irshift__',
'__isub__',
'__itruediv__',
'__ixor__',
]
for name in _unsupported_methods:
locals()[name] = partialmethod(_nope, name)
del name