async def test_zip_shortest(self): short = ["a", "b", "c"] long = [0, 1, 2, 3, 5] result = await ait.list(ait.zip(short, long)) expected = [["a", 0], ["b", 1], ["c", 2]] self.assertListEqual(expected, result)
def filter_history(channel: discord.TextChannel, **kwargs) -> typing.Coroutine[typing.Any, None, list[discord.Message]]: check = kwargs.pop('check', lambda m: True) limit = kwargs.pop('limit', sys.maxsize) return aioitertools.list(aioitertools.map( operator.itemgetter(1), aioitertools.zip(range(limit), channel.history(limit=None, **kwargs).filter(check)) ))
async def _visit_edge(it, key, query): async for i, res in aioitertools.zip(it, query.iter()): i[str(key)] = res if VISITED_EDGES_KEY in i: i[VISITED_EDGES_KEY].append(key) elif hasattr(i, VISITED_EDGES_KEY): i.__visited_edges__.add(key) yield i
async def fetchmany(self, n=1): if not self._more or n < 1: return self._cache += [ entry async for self._n_last_fetched, entry in aioitertools.zip( range(n), self.iterator) ] if self._n_last_fetched < n: self._more = False self._ncache += self._n_last_fetched
async def test_location_a_lot_queries(): ips = ['8.8.8.8', '1.1.1.1', '1.0.0.1'] * 700 check_fields = ['org', 'lat', 'lon', 'country', 'as'] async with IpApiClient() as client: async for ip, res in aioitertools.zip(ips, client.location_stream(ips)): assert 'status' in res and res['status'] == 'success' assert 'query' in res and res['query'] == ip for field in check_fields: assert field in res
async def test_zip_equal(self): idx = 0 async for a, b in ait.zip(slist, srange): self.assertEqual(a, slist[idx]) self.assertEqual(b, srange[idx]) idx += 1
async def _visit_edge(it, key, query): async for i, res in aioitertools.zip(it, query.iter()): i[str(key)] = res if VISITED_EDGES_KEY in i: i[VISITED_EDGES_KEY].append(key) yield i
async def interactive_poll_maker(self, ctx: MyContext, timeout: PollTime = TIMEOUT): """Create a poll interactively""" embed = discord.Embed( title='Interactive Poll Maker', description=f'Poll created by {ctx.author.mention}\n\n' f'React with :x: to cancel.', colour=discord.Colour.orange() ) content = 'Hello, you\'ve entered the interactive poll maker. Please enter your question below.' accepted_emojis = {'\N{CROSS MARK}'} async def get_poll_votes() -> typing.Union[str, bool]: deleted = True my_message: typing.Optional[discord.Message] = None def msg_check(msg: discord.Message): return msg.channel == ctx.channel and msg.author == ctx.author def rxn_check(rxn: discord.Reaction, usr: discord.User): return rxn.message == my_message and usr == ctx.author and str(rxn) in accepted_emojis while True: if deleted: my_message = await ctx.send(content, embed=embed) for emo in accepted_emojis: await my_message.add_reaction(emo) deleted = False futs = { asyncio.create_task(self.bot.wait_for('message', check=msg_check)), asyncio.create_task(self.bot.wait_for('reaction_add', check=rxn_check)) } done, pending = await asyncio.wait(futs, timeout=60.0, return_when=asyncio.FIRST_COMPLETED) [fut.cancel() for fut in pending] params: typing.Union[discord.Message, tuple[discord.Reaction, discord.User]] = done.pop().result() if isinstance(params, discord.Message): response = params.content.strip() if not response: await ctx.send('Message has no content', delete_after=10) continue if discord.utils.get(embed.fields, txt=response): await ctx.send('Duplicate options are not allowed') continue else: response = str(params[0]) == '\N{CROSS MARK}' await my_message.delete() yield response deleted = True async for i, resp in aioitertools.zip(range(11), get_poll_votes()): if isinstance(resp, str): content = f'Hello, you\'ve entered the interactive poll maker. ' \ f'Please enter option {i + 1} below.' if i == 2: accepted_emojis.add('\N{WHITE HEAVY CHECK MARK}') embed.description += '\nReact with :white_check_mark: to exit' embed.add_field( name='Question' if i == 0 else f'Option {1}', value=resp ) elif resp: return await ctx.send('Poll creation cancelled by user', delete_after=10) else: break timeout += (datetime.datetime.utcnow() - ctx.message.created_at).total_seconds() options = [field.value for field in embed.fields] mgr = await PollManager.from_command(ctx, timeout, *options) self.polls.append(mgr) mgr.start()