Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/past/builtins/noniterators.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """ | |
2 This module is designed to be used as follows:: | |
3 | |
4 from past.builtins.noniterators import filter, map, range, reduce, zip | |
5 | |
6 And then, for example:: | |
7 | |
8 assert isinstance(range(5), list) | |
9 | |
10 The list-producing functions this brings in are:: | |
11 | |
12 - ``filter`` | |
13 - ``map`` | |
14 - ``range`` | |
15 - ``reduce`` | |
16 - ``zip`` | |
17 | |
18 """ | |
19 | |
20 from __future__ import division, absolute_import, print_function | |
21 | |
22 from itertools import chain, starmap | |
23 import itertools # since zip_longest doesn't exist on Py2 | |
24 from past.types import basestring | |
25 from past.utils import PY3 | |
26 | |
27 | |
28 def flatmap(f, items): | |
29 return chain.from_iterable(map(f, items)) | |
30 | |
31 | |
32 if PY3: | |
33 import builtins | |
34 | |
35 # list-producing versions of the major Python iterating functions | |
36 def oldfilter(*args): | |
37 """ | |
38 filter(function or None, sequence) -> list, tuple, or string | |
39 | |
40 Return those items of sequence for which function(item) is true. | |
41 If function is None, return the items that are true. If sequence | |
42 is a tuple or string, return the same type, else return a list. | |
43 """ | |
44 mytype = type(args[1]) | |
45 if isinstance(args[1], basestring): | |
46 return mytype().join(builtins.filter(*args)) | |
47 elif isinstance(args[1], (tuple, list)): | |
48 return mytype(builtins.filter(*args)) | |
49 else: | |
50 # Fall back to list. Is this the right thing to do? | |
51 return list(builtins.filter(*args)) | |
52 | |
53 # This is surprisingly difficult to get right. For example, the | |
54 # solutions here fail with the test cases in the docstring below: | |
55 # http://stackoverflow.com/questions/8072755/ | |
56 def oldmap(func, *iterables): | |
57 """ | |
58 map(function, sequence[, sequence, ...]) -> list | |
59 | |
60 Return a list of the results of applying the function to the | |
61 items of the argument sequence(s). If more than one sequence is | |
62 given, the function is called with an argument list consisting of | |
63 the corresponding item of each sequence, substituting None for | |
64 missing values when not all sequences have the same length. If | |
65 the function is None, return a list of the items of the sequence | |
66 (or a list of tuples if more than one sequence). | |
67 | |
68 Test cases: | |
69 >>> oldmap(None, 'hello world') | |
70 ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] | |
71 | |
72 >>> oldmap(None, range(4)) | |
73 [0, 1, 2, 3] | |
74 | |
75 More test cases are in test_past.test_builtins. | |
76 """ | |
77 zipped = itertools.zip_longest(*iterables) | |
78 l = list(zipped) | |
79 if len(l) == 0: | |
80 return [] | |
81 if func is None: | |
82 result = l | |
83 else: | |
84 result = list(starmap(func, l)) | |
85 | |
86 # Inspect to see whether it's a simple sequence of tuples | |
87 try: | |
88 if max([len(item) for item in result]) == 1: | |
89 return list(chain.from_iterable(result)) | |
90 # return list(flatmap(func, result)) | |
91 except TypeError as e: | |
92 # Simple objects like ints have no len() | |
93 pass | |
94 return result | |
95 | |
96 ############################ | |
97 ### For reference, the source code for Py2.7 map function: | |
98 # static PyObject * | |
99 # builtin_map(PyObject *self, PyObject *args) | |
100 # { | |
101 # typedef struct { | |
102 # PyObject *it; /* the iterator object */ | |
103 # int saw_StopIteration; /* bool: did the iterator end? */ | |
104 # } sequence; | |
105 # | |
106 # PyObject *func, *result; | |
107 # sequence *seqs = NULL, *sqp; | |
108 # Py_ssize_t n, len; | |
109 # register int i, j; | |
110 # | |
111 # n = PyTuple_Size(args); | |
112 # if (n < 2) { | |
113 # PyErr_SetString(PyExc_TypeError, | |
114 # "map() requires at least two args"); | |
115 # return NULL; | |
116 # } | |
117 # | |
118 # func = PyTuple_GetItem(args, 0); | |
119 # n--; | |
120 # | |
121 # if (func == Py_None) { | |
122 # if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; " | |
123 # "use list(...)", 1) < 0) | |
124 # return NULL; | |
125 # if (n == 1) { | |
126 # /* map(None, S) is the same as list(S). */ | |
127 # return PySequence_List(PyTuple_GetItem(args, 1)); | |
128 # } | |
129 # } | |
130 # | |
131 # /* Get space for sequence descriptors. Must NULL out the iterator | |
132 # * pointers so that jumping to Fail_2 later doesn't see trash. | |
133 # */ | |
134 # if ((seqs = PyMem_NEW(sequence, n)) == NULL) { | |
135 # PyErr_NoMemory(); | |
136 # return NULL; | |
137 # } | |
138 # for (i = 0; i < n; ++i) { | |
139 # seqs[i].it = (PyObject*)NULL; | |
140 # seqs[i].saw_StopIteration = 0; | |
141 # } | |
142 # | |
143 # /* Do a first pass to obtain iterators for the arguments, and set len | |
144 # * to the largest of their lengths. | |
145 # */ | |
146 # len = 0; | |
147 # for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { | |
148 # PyObject *curseq; | |
149 # Py_ssize_t curlen; | |
150 # | |
151 # /* Get iterator. */ | |
152 # curseq = PyTuple_GetItem(args, i+1); | |
153 # sqp->it = PyObject_GetIter(curseq); | |
154 # if (sqp->it == NULL) { | |
155 # static char errmsg[] = | |
156 # "argument %d to map() must support iteration"; | |
157 # char errbuf[sizeof(errmsg) + 25]; | |
158 # PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); | |
159 # PyErr_SetString(PyExc_TypeError, errbuf); | |
160 # goto Fail_2; | |
161 # } | |
162 # | |
163 # /* Update len. */ | |
164 # curlen = _PyObject_LengthHint(curseq, 8); | |
165 # if (curlen > len) | |
166 # len = curlen; | |
167 # } | |
168 # | |
169 # /* Get space for the result list. */ | |
170 # if ((result = (PyObject *) PyList_New(len)) == NULL) | |
171 # goto Fail_2; | |
172 # | |
173 # /* Iterate over the sequences until all have stopped. */ | |
174 # for (i = 0; ; ++i) { | |
175 # PyObject *alist, *item=NULL, *value; | |
176 # int numactive = 0; | |
177 # | |
178 # if (func == Py_None && n == 1) | |
179 # alist = NULL; | |
180 # else if ((alist = PyTuple_New(n)) == NULL) | |
181 # goto Fail_1; | |
182 # | |
183 # for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { | |
184 # if (sqp->saw_StopIteration) { | |
185 # Py_INCREF(Py_None); | |
186 # item = Py_None; | |
187 # } | |
188 # else { | |
189 # item = PyIter_Next(sqp->it); | |
190 # if (item) | |
191 # ++numactive; | |
192 # else { | |
193 # if (PyErr_Occurred()) { | |
194 # Py_XDECREF(alist); | |
195 # goto Fail_1; | |
196 # } | |
197 # Py_INCREF(Py_None); | |
198 # item = Py_None; | |
199 # sqp->saw_StopIteration = 1; | |
200 # } | |
201 # } | |
202 # if (alist) | |
203 # PyTuple_SET_ITEM(alist, j, item); | |
204 # else | |
205 # break; | |
206 # } | |
207 # | |
208 # if (!alist) | |
209 # alist = item; | |
210 # | |
211 # if (numactive == 0) { | |
212 # Py_DECREF(alist); | |
213 # break; | |
214 # } | |
215 # | |
216 # if (func == Py_None) | |
217 # value = alist; | |
218 # else { | |
219 # value = PyEval_CallObject(func, alist); | |
220 # Py_DECREF(alist); | |
221 # if (value == NULL) | |
222 # goto Fail_1; | |
223 # } | |
224 # if (i >= len) { | |
225 # int status = PyList_Append(result, value); | |
226 # Py_DECREF(value); | |
227 # if (status < 0) | |
228 # goto Fail_1; | |
229 # } | |
230 # else if (PyList_SetItem(result, i, value) < 0) | |
231 # goto Fail_1; | |
232 # } | |
233 # | |
234 # if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) | |
235 # goto Fail_1; | |
236 # | |
237 # goto Succeed; | |
238 # | |
239 # Fail_1: | |
240 # Py_DECREF(result); | |
241 # Fail_2: | |
242 # result = NULL; | |
243 # Succeed: | |
244 # assert(seqs); | |
245 # for (i = 0; i < n; ++i) | |
246 # Py_XDECREF(seqs[i].it); | |
247 # PyMem_DEL(seqs); | |
248 # return result; | |
249 # } | |
250 | |
251 def oldrange(*args, **kwargs): | |
252 return list(builtins.range(*args, **kwargs)) | |
253 | |
254 def oldzip(*args, **kwargs): | |
255 return list(builtins.zip(*args, **kwargs)) | |
256 | |
257 filter = oldfilter | |
258 map = oldmap | |
259 range = oldrange | |
260 from functools import reduce | |
261 zip = oldzip | |
262 __all__ = ['filter', 'map', 'range', 'reduce', 'zip'] | |
263 | |
264 else: | |
265 import __builtin__ | |
266 # Python 2-builtin ranges produce lists | |
267 filter = __builtin__.filter | |
268 map = __builtin__.map | |
269 range = __builtin__.range | |
270 reduce = __builtin__.reduce | |
271 zip = __builtin__.zip | |
272 __all__ = [] |