Sunday, 4 February 2024

Intercept tqdm

For example to track the progress in a database or stream it via http

from tqdm import tqdm
import time

class TqdmContextManager:
    """
    A context manager to temporarily override the default tqdm class with a custom one.
    This allows for custom behavior of tqdm progress bars globally within its context.
    """
    def __init__(self):
        # Save a reference to the original tqdm class
        self.original_tqdm = tqdm

    def __enter__(self):
        # Override the global tqdm reference with our custom class upon entering the context
        global tqdm
        tqdm = CustomTqdm
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        # Restore the original tqdm class upon exiting the context
        global tqdm
        tqdm = self.original_tqdm


class CustomTqdm(tqdm):
    """
    A custom tqdm subclass that limits update frequency of progress output.
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.last_print_time = time.time()  # Record the start time
        self.update_every = 0.1  # Limit to printing every 0.1 seconds (10 times per second)

    def update(self, n=1):
        """
        Override the update method to control the frequency of progress output.
        """
        current_time = time.time()
        if current_time - self.last_print_time >= self.update_every:
            # Print progress if enough time has passed since the last print
            print(f"Progress: {self.n}/{self.total}", end='\r', flush=True)
            self.last_print_time = current_time  # Update the last print time


# Example usage of the custom tqdm with a context manager
def some_function():
    """
    A sample function to demonstrate the use of the custom tqdm progress bar.
    """
    for i in tqdm(range(50)):
        time.sleep(0.05)  # Simulate work with a delay

# Use the custom tqdm progress bar within the context manager
with TqdmContextManager():
    some_function()

No comments:

Post a Comment

Parse Wikipedia dump

""" This module processes Wikipedia dump files by extracting individual articles and parsing them into a structured format, ...