This is a small python API to be able to display a busy spinner using emojis for a textual bot that run lengthy async jobs without access to a percentage of completion and need to provide feedback so the user knows the bot isn't stuck. I'm looking for any remarks (general, code quality, ease of use, ease of use of the API, code smell, ... ?)
How the code is used:
for progress_bar_emoji in SlackBusySpinner(timeout=3600):
# Do something and react with progress_bar_emoji
# until job.is_finished or it timeout after 3600 s
if job.is_finished:
break
Code:
from __future__ import annotations
class SlackBusySpinner:
def __init__(self, timeout: int | None = None):
self.__progress_bar_index = 0
self.timeout = timeout
def __iter__(self):
return self
def __next__(self) -> str:
if self.timeout is not None and self.__progress_bar_index > self.timeout:
raise StopIteration
minutes = "30" if self.__progress_bar_index % 2 else ""
clock_number = (self.__progress_bar_index % 24 // 2) + 1
emoji = f"clock{clock_number}{minutes}"
self.__progress_bar_index += 1
return emoji
Tests:
from emoji_progress_bar import SlackBusySpinner
def test_clock_emoji() -> None:
expected = [
"clock1", # 🕐
"clock130", # 🕜
"clock2", # 🕑
"clock230", # etc.
"clock3",
"clock330",
"clock4",
"clock430",
"clock5",
"clock530",
"clock6",
"clock630",
"clock7",
"clock730",
"clock8",
"clock830",
"clock9",
"clock930",
"clock10",
"clock1030",
"clock11",
"clock1130",
"clock12",
"clock1230",
]
emojis = []
slack_answer = SlackBusySpinner()
for _ in range(48):
emojis.append(next(slack_answer))
assert emojis == expected * 2
Result:
🕐 then 🕜, then 🕑, etc. ...back to 🕐 then 🕜, then 🕑, etc.