Can you write the documentation for this python function using the most popular…
- User
Can you write the documentation for this python function using the most popular formatting :
async def paginate(ctx, msg_iter, thumb_url, page): page = int(page) - 1 msg_iter_list = False if isinstance(msg_iter, list): msg_iter_list = msg_iter msg_iter = msg_iter_list[page]
head, desc, foot, field_name, *msg_list = msg_iter max_page = len(msg_list) max_entry = 1 if msg_iter_list: max_entry = len(msg_iter_list) msg_iter_list[page] = [head, desc, foot, field_name, *msg_list] # msg_iter_list = [list(msg_it) for msg_it in msg_iter_list] cycle_thumb = False if isinstance(thumb_url, (tuple,list)): cycle_thumb = thumb_url cycle_field = False if isinstance(field_name, tuple): cycle_field = field_name action_row_entry = ctx.bot.rest.build_message_action_row() val_entry_id = "buttonValEntry" val_entry_button = ( action_row_entry .add_button(hikari.ButtonStyle.PRIMARY, val_entry_id) .set_label("") .set_emoji("\u2705") ) prev_entry_id = "buttonPrevEntry" prev_entry_button = ( action_row_entry .add_button(hikari.ButtonStyle.PRIMARY, prev_entry_id) .set_label("") .set_emoji("\u23EE") ) next_entry_id = "buttonNextEntry" next_entry_button = ( action_row_entry .add_button(hikari.ButtonStyle.PRIMARY, next_entry_id) .set_label("") .set_emoji("\u23ED") ) prev_entry_button.add_to_container() val_entry_button.add_to_container() compo_entry = next_entry_button.add_to_container() action_row = ctx.bot.rest.build_message_action_row() prev_id = "buttonPrev" prev_button = ( action_row .add_button(hikari.ButtonStyle.PRIMARY, prev_id) .set_label("Prev") .set_emoji("\u25C0") ) next_id = "buttonNext" next_button = ( action_row .add_button(hikari.ButtonStyle.PRIMARY, next_id) .set_label("Next") .set_emoji("\u25B6") ) prev_button.add_to_container() compo = next_button.add_to_container() next_button.set_is_disabled(page == max_page - 1) prev_button.set_is_disabled(page == 0) if max_entry > 1: new_foot = foot + f" \u2022 {page+1}/{max_entry}" else: new_foot = foot + f" \u2022 {page+1}/{max_page}" msg = msg_list[0] if cycle_thumb: thumb_url = cycle_thumb[page%len(cycle_thumb)] if not isinstance(thumb_url, str): thumb_url = list(islice(thumb_url, 1))[0] if isinstance(cycle_thumb, list): cycle_thumb[page%len(cycle_thumb)] = thumb_url if cycle_field: field_name = cycle_field[page] logging.debug(f"Thumb: {thumb_url}") embed = embed_generator(head, new_foot, desc, msg, thumb_url, field_name) logging.debug("Embedding initial") if max_entry > 1: valid_resp = False entry_num = page resp = await ctx.respond(embed, components=[compo_entry], user_mentions=False) logging.debug("Entry Primary response") resp_msg = await resp.message() logging.debug("Entry Button response") with ctx.bot.stream(hikari.InteractionCreateEvent, 5).filter( lambda e: isinstance(e.interaction, hikari.ComponentInteraction) and e.interaction.message.id == resp_msg.id and e.interaction.component_type == hikari.ComponentType.BUTTON ) as stream: async for event in stream: button_inter = event.interaction button_id = button_inter.custom_id if button_id == next_entry_id: entry_num += 1 elif button_id == prev_entry_id: entry_num -= 1 elif button_id == val_entry_id: valid_resp = True break head, desc, foot, field_name, *msg_list = msg_iter_list[entry_num%max_entry] msg = msg_list[0] msg_iter_list[entry_num%max_entry] = [head, desc, foot, field_name, *msg_list] max_page = len(msg_list) new_foot = foot + f" \u2022 {entry_num%max_entry+1}/{max_entry}" if cycle_thumb: thumb_url = cycle_thumb[entry_num%len(cycle_thumb)] if not isinstance(thumb_url, str): thumb_url = list(islice(thumb_url, 1))[0] if isinstance(cycle_thumb, list): cycle_thumb[entry_num%len(cycle_thumb)] = thumb_url if cycle_field: field_name = cycle_field[entry_num%max_entry] embed = embed_generator(head, new_foot, desc, msg, thumb_url, field_name) logging.debug("Entry Embedding") try: await button_inter.create_initial_response( hikari.ResponseType.MESSAGE_UPDATE, embed=embed, components=[compo_entry], ) except hikari.NotFoundError: await button_inter.edit_initial_response( embed=embed, components=[compo_entry], ) cycle_thumb = False page = 0 if max_page == 1: compos = [] else: compos = [compo] if valid_resp: try: await button_inter.create_initial_response( hikari.ResponseType.MESSAGE_UPDATE, embed=embed, components=compos, ) except hikari.NotFoundError: await button_inter.edit_initial_response( embed=embed, components=compos, ) else: await resp_msg.edit(components=compos) else: if max_page == 1: resp = await ctx.respond(embed, components=[], user_mentions=False) else: resp = await ctx.respond(embed, components=[compo], user_mentions=False) logging.debug("Primary response") resp_msg = await resp.message() logging.debug("Button response") with ctx.bot.stream(hikari.InteractionCreateEvent, 180).filter( lambda e: isinstance(e.interaction, hikari.ComponentInteraction) and e.interaction.message.id == resp_msg.id and e.interaction.component_type == hikari.ComponentType.BUTTON ) as stream: async for event in stream: button_inter = event.interaction button_id = button_inter.custom_id if button_id == next_id: page += 1 elif button_id == prev_id: page -= 1 next_button.set_is_disabled(page == max_page - 1) prev_button.set_is_disabled(page == 0) try: assert page >= 0 msg = msg_list[page] except (IndexError, AssertionError): if button_id == next_id: page -= 1 elif button_id == prev_id: page += 1 next_button.set_is_disabled(page == max_page - 1) prev_button.set_is_disabled(page == 0) new_foot = foot + f" \u2022 {page+1}/{max_page}" if cycle_thumb: thumb_url = cycle_thumb[page%len(cycle_thumb)] if not isinstance(thumb_url, str): thumb_url = list(islice(thumb_url, 1))[0] if isinstance(cycle_thumb, list): cycle_thumb[page%len(cycle_thumb)] = thumb_url if cycle_field: field_name = cycle_field[page] embed = embed_generator(head, new_foot, desc, msg, thumb_url, field_name) logging.debug("Embedding") try: await button_inter.create_initial_response( hikari.ResponseType.MESSAGE_UPDATE, embed=embed, components=[compo], ) except hikari.NotFoundError: await button_inter.edit_initial_response( embed=embed, components=[compo], ) await resp_msg.edit(components=[]) - Assistant
async def paginate(ctx, msg_iter, thumb_url, page): """ Generate a paginated response from a list of messages. Parameters ---------- ctx : lightbulb.Context The context in which the command was invoked. msg_iter : iterable An iterable of messages (list or tuple) that will be paginated. Each message should be a tuple containing (head, desc, foot, field_name, *msg_list), where: - head: The header of the message embed. - desc: The description of the message embed. - foot: The footer text of the message embed. - field_name: The field name of the message embed. - msg_list: A list of messages for each page in the embed. thumb_url : str or iterable The thumbnail URL to be added to the embed, or a list of thumbnail URLs cycling through each page in the embed. page : int Starting page number (1 indexed). Notes ----- Pagination is done by adding Next and Previous buttons to the message embed that users can interact with. The function will await user interactions for up to 180 seconds, after which it will stop listening for interactions and remove the buttons from the embed. Returns ------- None Raises ------ hikari.NotFoundError If there is an issue while editing the message during interactions. """ - User
- Assistant
- User
Continue.
- Assistant
Here, I further split the code to create a button_builders function to create and handle the button components separately:
async def paginate(ctx, msg_iter, thumb_url, page): def button_builders(): action_row_entry = ctx.bot.rest.build_message_action_row() action_row = ctx.bot.rest.build_message_action_row() button_data = [ (action_row_entry, hikari.ButtonStyle.PRIMARY, "buttonValEntry", "", "\u2705"), (action_row_entry, hikari.ButtonStyle.PRIMARY, "buttonPrevEntry", "", "\u23EE"), (action_row_entry, hikari.ButtonStyle.PRIMARY, "buttonNextEntry", "", "\u23ED"), (action_row, hikari.ButtonStyle.PRIMARY, "buttonPrev", "Prev", "\u25C0"), (action_row, hikari.ButtonStyle.PRIMARY, "buttonNext", "Next", "\u25B6"), ] for row, style, button_id, label, emoji in button_data: button = row.add_button(style, button_id).set_label(label).set_emoji(emoji) button.add_to_container() return action_row_entry, action_row # ... action_row_entry, action_row = button_builders() # ...
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.