Witertools - Tools to wrap iterator functions, to consume the whole iterator and return a list. Avoid sprinklist list(...)
Project description
witertools.
witertools provides:
- functions that take a function that returns and iterator, and return a function that returns list.
- wrappers around built in functions that return iterators (filter, map, and zip)
- wrappers around the functions in itertools except count, cycle, and repeat
- wrappers around the functions in more_itertools
The motivation as that itertools and more-itertools in particular have functions that return iterators, but very frequently the desired result is a list. Over and over in code you will see repeated code of calling list on the result of a function that produces an interator. You will even see it in most examples in the more-itertools api.
Wrappers Around Functions that Return Iterators
In witertools: iterator_to_list_function will take a function that returns an iterable, and return a function that returns a list.
>>> from witertools import *
>>> f=iterator_to_list_function(filter)
>>> w.filter(lambda s: True, [3,4,5])
[3, 4, 5]
iterator_name_to_list_function will take a module object and a function name, and return a function that returns a list.
>> g=iterator_name_to_list_function(__builtins__,"filter")
>>> g(lambda s : True , [1,2])
[1, 2]
Wrappers Around Built in Functions
To use a wrapper around a built in function, just use a function from the same name from the witertools package.
>>> import witertools as w
>>> w.filter(lambda s: True, [3,4,5])
[3, 4, 5]
which replaces
[3, 4, 5]
Wrappers Around itertools
Use a function from the witertools.itertools namespace, with the same name as the function in the itertools namespace.
>>> i.chain([1,3],[4,5])
[1, 3, 4, 5]
Wrappers Around more-itertools
Use a function from the witertools.more_itertools namespace, with the same name as the function in the more-itertools namespace. You will need to install more-itertools to use these functions, as more-itertools as not a prequisite of witertools.
>>> from witertools.more_itertools import chunked
>>> chunked([1, 2, 3, 4, 5, 6], 3)
[[1, 2, 3], [4, 5, 6]]
There are a few functions in more_itertools that do not return iterables. For example, nth_or_last. These functions are still wrapped in witertools.more_itertools but will result in a traceback.
>>> from witertools.more_itertools import nth_or_last
>>> nth_or_last([0, 1, 2, 3], 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Python39-32\lib\site-packages\witertools\iterator_to_list.py", line 7, in as_list
return list(iterator_function(*args,**kwargs))
TypeError: 'int' object is not iterable
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for witertools-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52ec188b7e7f1959cc70020031aeecf8368b05366b06cd952c90793df8e91795 |
|
MD5 | 25706dee3a16b2cd51c082191cd91605 |
|
BLAKE2b-256 | 77b6087cc67da5365ca1ad48988697501ba6d77173cd88ef6bfbfde5798d7482 |