Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

witertools-1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

witertools-1.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file witertools-1.0.1.tar.gz.

File metadata

  • Download URL: witertools-1.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for witertools-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b8028d28d1ac4534488ba9d3a52404f1c890f4756d8b06eb6371b6f881a8fc05
MD5 aaaa38c6cca9ffbbe9ae7d31bbf70315
BLAKE2b-256 5b639114fd678f661bf304bd73a42bbd1231c38158234613a734d45bb180d3dc

See more details on using hashes here.

File details

Details for the file witertools-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: witertools-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for witertools-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52ec188b7e7f1959cc70020031aeecf8368b05366b06cd952c90793df8e91795
MD5 25706dee3a16b2cd51c082191cd91605
BLAKE2b-256 77b6087cc67da5365ca1ad48988697501ba6d77173cd88ef6bfbfde5798d7482

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page