paco icon indicating copy to clipboard operation
paco copied to clipboard

Add missing asynchronous utility functions

Open h2non opened this issue 8 years ago • 3 comments

Ideally, porting the following functions to the asynchronous world:

  • itertools.chain
  • itertools.chain.from_iterable
  • functools.partial_method
  • itertools.takewhile
  • itertools.compress
  • itertools.startmap
  • take
  • itertools.tee

h2non avatar Dec 29 '16 00:12 h2non

See: http://fitzgen.github.io/wu.js/

h2non avatar Dec 29 '16 01:12 h2non

I'd like to suggest functions similar to bluebird.js::props(), and pydash::map_values() for working with dictionaries rather than lists.

This allows computing a series of async functions whilst retaining the original mapping between input and output values, and is also a convenient way to load multiple named async variables before the main synchronous code block.

output = await paco.map_dict({
   "A.txt": async_read_file("A.txt"),
   "B.txt": async_read_file("B.txt")
})
assert output == { "A.txt": "contents A.txt", "A.txt": "contents B.txt" }
output = await paco.map_dict({
   "A.txt": "A.txt",
   "B.txt": "B.txt"
}, async_read_file)
assert output == { "A.txt": "contents A.txt", "B.txt": "contents B.txt" }
output = await paco.map_dict(["A.txt", "B.txt"], async_read_file)
assert output == { "A.txt": "contents A.txt", "B.txt": "contents B.txt" }

JamesMcGuigan avatar Dec 29 '17 13:12 JamesMcGuigan

Oh, yeah, that's relatively common if you came from a JS background, but not sure if Python devs are going to appreciate this. At the end I feel it's pretty much logic sugar for convenience and laziness.

If any, I would only bet supporting interface and behavior of example 1 and 2, but no more. I prefer a concrete, predictable interface instead of a too overloaded interface with too many vectors.

This can be easily implemented in multiple ways, such as using the async reducer. PR are welcome.

h2non avatar Dec 29 '17 15:12 h2non