cgarciae / stop_iter

Apache License 2.0
7 stars 0 forks source link

Feature Request: Expand library with more variants of interruptible operators (happy to open PR) #1

Open jaivardhankapoor opened 4 months ago

jaivardhankapoor commented 4 months ago

Description:

The current library provides a stop_iter function, which is a great start for creating interruptible operations. However, to enhance the library considering common ML usecases, I'm proposing expanding the library with more variants of interruptible operators.

Proposed New Features:

Interruptible Context Manager:

A context manager that allows for easy creation of interruptible code blocks. Example usage:

with interruptible() as check_interrupt:
    for item in large_dataset:
        if check_interrupt():
            break
        process_item(item)

Interruptible Decorator:

A decorator to make entire functions interruptible. Example usage:

@interruptible
def long_running_process():
    for step in many_steps:
        check_interrupt()
        do_step(step)

Interruptible Iterator Class:

An iterator class with its own interruptible context. Not sure if the current stop_iter allows nesting. This ensures nesting is handled. Example usage:

for item in InterruptibleIterator(large_dataset):
    process_item(item)

Progress Bar Integration:

Integration with tqdm to provide interruptible progress bars. Example usage:

with InterruptibleTQDM(range(100), desc="Processing") as pbar:
    for item in pbar:
        if pbar.interrupted:
            break
        process_item(item)

I can start a pull request these features if you think it's a good idea. The code will remain clean and in a single-file.

cgarciae commented 4 months ago

Hey! Thanks for the suggestions, I was planning on adding context manager support and enabling a decorator is not far off so I've created #3. I'll merge it soon.

That said, I don't plan to add more fancy stuff, ideally this is very simple single function library, if you want more feature feel free to fork :)