From 02b6718b2d0a6be136a8b245b03b1b01627aa206 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 31 Mar 2020 01:10:20 +0200 Subject: [PATCH] Improve some utils --- royalnet/utils/asyncify.py | 5 ++++- royalnet/utils/formatters.py | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/royalnet/utils/asyncify.py b/royalnet/utils/asyncify.py index e4a7522f..36d29cd0 100644 --- a/royalnet/utils/asyncify.py +++ b/royalnet/utils/asyncify.py @@ -13,7 +13,10 @@ async def asyncify(function: typing.Callable, *args, loop: typing.Optional[async loop: The loop to run the function in. If :const:`None`, run it in in the current event loop. Warning: - The called function must be thread-safe!""" + The called function must be thread-safe! + + Warning: + Calling a function this way might be significantly slower than calling its blocking counterpart!""" if not loop: loop = asyncio.get_event_loop() return await loop.run_in_executor(None, functools.partial(function, *args, **kwargs)) diff --git a/royalnet/utils/formatters.py b/royalnet/utils/formatters.py index 3c7c72f0..80e4e378 100644 --- a/royalnet/utils/formatters.py +++ b/royalnet/utils/formatters.py @@ -2,11 +2,12 @@ import typing import re -def andformat(l: typing.Collection[str], middle=", ", final=" and ") -> str: - """Convert a iterable (such as a :class:`list`) to a :class:`str` by adding ``final`` between the last two elements and ``middle`` between the others. +def andformat(coll: typing.Collection[str], middle=", ", final=" and ") -> str: + """Convert a collection (such as a :class:`list`) to a :class:`str` by adding ``final`` between the last two + elements and ``middle`` between the others. Args: - l: the input iterable. + coll: the input collection. middle: the :class:`str` to be added between the middle elements. final: the :class:`str` to be added between the last two elements. @@ -26,11 +27,11 @@ def andformat(l: typing.Collection[str], middle=", ", final=" and ") -> str: "Paltri+Spaggia+Gesù+Mallllco" """ result = "" - for index, item in enumerate(l): + for index, item in enumerate(coll): result += item - if index == len(l) - 2: + if index == len(coll) - 2: result += final - elif index != len(l) - 1: + elif index != len(coll) - 1: result += middle return result @@ -50,7 +51,7 @@ def underscorize(string: str) -> str: :: >>> underscorize("LE EPIC PRANK [GONE WRONG!?!?]") - "LE EPIC PRANK _GONE WRONG_____" + "LE_EPIC_PRANK__GONE_WRONG_____" """ return re.sub(r"\W", "_", string) @@ -82,11 +83,11 @@ def ytdldateformat(string: typing.Optional[str], separator: str = "-") -> str: return f"{string[0:4]}{separator}{string[4:6]}{separator}{string[6:8]}" -def numberemojiformat(l: typing.List[str]) -> str: - """Convert a :class:`list` to a Unicode string with one item on every line numbered with emojis. +def numberemojiformat(li: typing.Collection[str]) -> str: + """Convert a collection to a string with one item on every line numbered with emojis. Parameters: - l: the list to convert. + li: the list to convert. Returns: The resulting Unicode string. @@ -97,7 +98,7 @@ def numberemojiformat(l: typing.List[str]) -> str: number_emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"] extra_emoji = "*️⃣" result = "" - for index, element in enumerate(l): + for index, element in enumerate(li): try: result += f"{number_emojis[index]} {element}\n" except IndexError: