A flexible, customizable while loop for Python with timeout, iteration limits, and pre-iteration hooks.
Project description
flexibleloop
A Python utility that extends the standard while loop with built-in timeout control, iteration limits, pre-iteration hooks, and dynamic condition functions — all through a clean for-loop interface.
Why
Python's while loop is simple but limited. Adding a timeout, counting iterations, or updating variables before checking a condition requires boilerplate every time. FlexibleWhile packages all of that into a reusable, configurable object.
Features
- Timeout — Stop the loop automatically after a set number of seconds
- Max iterations — Cap the number of iterations without a counter variable
- Before function — Run a function before each iteration; its return value becomes the loop variable
- Condition function — Define the loop condition as a callable instead of inline logic
- Callbacks — Execute custom functions when timeout or max iterations are hit
- Clean interface — Used as a standard
forloop; no new syntax to learn
Installation
Clone the repository and import directly:
git clone https://github.com/tolgakanatli/flexiblewhile.git
from FlexibleLoop import FlexibleWhile
Usage
Basic loop with timeout
from FlexibleLoop import FlexibleWhile
loop = FlexibleWhile(timeout=5.0)
for _ in loop:
# Runs until 5 seconds have elapsed
do_something()
Loop with a condition function
def condition(value):
return value < 100
def update(value):
return value + 7
loop = FlexibleWhile(
before_func=update,
cond_func=condition,
start_vals=(0,)
)
for value in loop:
print(value)
# Prints: 7, 14, 21, ... up to 98
Loop with max iterations and a callback
def on_limit():
print("Max iterations reached")
loop = FlexibleWhile(
max_iterations=10,
on_max_iterations=on_limit
)
for _ in loop:
do_something()
Accessing loop metadata
loop = FlexibleWhile(timeout=3.0)
for _ in loop:
pass
print(f"Completed {loop.iterations} iterations in {loop.time_elapsed:.2f}s")
API Reference
FlexibleWhile(before_func, cond_func, start_vals, timeout, max_iterations, on_timeout, on_max_iterations)
| Parameter | Type | Default | Description |
|---|---|---|---|
before_func |
callable | None | Called before each iteration. Return value updates loop variable(s). |
cond_func |
callable | None | Loop continues while this returns True. None is equivalent to while True. |
start_vals |
tuple | (None,) |
Initial values passed to before_func and cond_func on first iteration. |
timeout |
float | None | Maximum loop duration in seconds. |
max_iterations |
int | None | Maximum number of iterations. |
on_timeout |
callable | None | Called when timeout is reached. |
on_max_iterations |
callable | None | Called when max iterations is reached. |
Instance attributes (after iteration)
| Attribute | Description |
|---|---|
loop.iterations |
Number of completed iterations |
loop.time_elapsed |
Total elapsed time in seconds |
loop.values |
Current value(s) of the loop variable |
License
MIT License — free to use, modify, and distribute, including commercially.
Attribution required: please credit Dr. Tolga Kaan Kanatlı in any derivative work or product.
Author
Dr. Tolga Kaan Kanatlı
PhD in Chemical Engineering — PEM Fuel Cells & Hydrogen Production
GitHub · LinkedIn · tolgakanatli@hotmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flexibleloop-0.1.0.tar.gz.
File metadata
- Download URL: flexibleloop-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8effda5d426c0482890be1c8248e783bde96d40f206f7c3c57e60a5304c84776
|
|
| MD5 |
c0e9bd5304a06db621bb97c8ed2c9375
|
|
| BLAKE2b-256 |
ee867f49ad0046575f8a8ac6c93c7b43f2f6e9fda67f8e75e7f88c82f413c9b2
|
File details
Details for the file flexibleloop-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flexibleloop-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1eaa0a237a5a656e028cda18e2bee0ae60d680977efa1d65f831700a43b4bfc
|
|
| MD5 |
5e14092d5e38c7a8a5f51a9b7c0770df
|
|
| BLAKE2b-256 |
515d226413277ad64f089fcfd44537060eeb1e840bea0fd077ee214520e6bec9
|