1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Improve some utils

This commit is contained in:
Steffo 2020-03-31 01:10:20 +02:00
parent 6b0d286391
commit 02b6718b2d
2 changed files with 16 additions and 12 deletions

View file

@ -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. loop: The loop to run the function in. If :const:`None`, run it in in the current event loop.
Warning: 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: if not loop:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, functools.partial(function, *args, **kwargs)) return await loop.run_in_executor(None, functools.partial(function, *args, **kwargs))

View file

@ -2,11 +2,12 @@ import typing
import re import re
def andformat(l: typing.Collection[str], middle=", ", final=" and ") -> str: def andformat(coll: 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. """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: Args:
l: the input iterable. coll: the input collection.
middle: the :class:`str` to be added between the middle elements. middle: the :class:`str` to be added between the middle elements.
final: the :class:`str` to be added between the last two 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" "Paltri+Spaggia+Gesù+Mallllco"
""" """
result = "" result = ""
for index, item in enumerate(l): for index, item in enumerate(coll):
result += item result += item
if index == len(l) - 2: if index == len(coll) - 2:
result += final result += final
elif index != len(l) - 1: elif index != len(coll) - 1:
result += middle result += middle
return result return result
@ -50,7 +51,7 @@ def underscorize(string: str) -> str:
:: ::
>>> underscorize("LE EPIC PRANK [GONE WRONG!?!?]") >>> underscorize("LE EPIC PRANK [GONE WRONG!?!?]")
"LE EPIC PRANK _GONE WRONG_____" "LE_EPIC_PRANK__GONE_WRONG_____"
""" """
return re.sub(r"\W", "_", string) 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]}" return f"{string[0:4]}{separator}{string[4:6]}{separator}{string[6:8]}"
def numberemojiformat(l: typing.List[str]) -> str: def numberemojiformat(li: typing.Collection[str]) -> str:
"""Convert a :class:`list` to a Unicode string with one item on every line numbered with emojis. """Convert a collection to a string with one item on every line numbered with emojis.
Parameters: Parameters:
l: the list to convert. li: the list to convert.
Returns: Returns:
The resulting Unicode string. 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", "🔟"] number_emojis = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "🔟"]
extra_emoji = "*️⃣" extra_emoji = "*️⃣"
result = "" result = ""
for index, element in enumerate(l): for index, element in enumerate(li):
try: try:
result += f"{number_emojis[index]} {element}\n" result += f"{number_emojis[index]} {element}\n"
except IndexError: except IndexError: