in the RunningAnimation how to show only running animation and avoid showing any other…
- User
in the RunningAnimation how to show only running animation and avoid showing any other print statemnts
class RunningAnimation: def __init__(self, message): self.message = message def __enter__(self): self.stop_event = threading.Event() self.animation_thread = threading.Thread(target=self.click_animation, args=(self.stop_event,)) self.animation_thread.start() # Redirect stdout to suppress print statements self.old_stdout = sys.stdout sys.stdout = io.StringIO() # Redirect to an in-memory stream return self def __exit__(self, exc_type, exc_value, traceback): self.stop_event.set() self.animation_thread.join() # Restore stdout to the original value sys.stdout = self.old_stdout # Clear the last line printed by the animation sys.stdout.write('\r' + ' ' * 20 + '\r') def click_animation(self, stop_event): while not stop_event.is_set(): for animation in [f'{self.message}...', f'{self.message}..', f'{self.message}.']: sys.stdout.write('\r' + animation) sys.stdout.flush() time.sleep(0.5) def generate_results(): # Simulate some operations that would print statements print("Generating results...") # This will be suppressed time.sleep(3) # Simulate time-consuming work print("Results generated.") # This will also be suppressed # Usage with RunningAnimation("Data Processing"): generate_results()so generate_results should't show any print statemnts.
- Assistant