noniterators.py 9.2 KB

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