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
Metadata
Release files for flexibleloop 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flexibleloop-0.1.0.tar.gz | 6.3 kB | Without provenance | Details for flexibleloop-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flexibleloop-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for flexibleloop-0.1.0-py3-none-any.whl |
Total release size: 12.4 kB
Release files / flexibleloop-0.1.0.tar.gz
| Download URL | flexibleloop-0.1.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8effda5d426c0482890be1c8248e783bde96d40f206f7c3c57e60a5304c84776
|
|
BLAKE2b-256 checksum How to use checksums |
ee867f49ad0046575f8a8ac6c93c7b43f2f6e9fda67f8e75e7f88c82f413c9b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.1
|
Release files / flexibleloop-0.1.0-py3-none-any.whl
| Download URL | flexibleloop-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c1eaa0a237a5a656e028cda18e2bee0ae60d680977efa1d65f831700a43b4bfc
|
|
BLAKE2b-256 checksum How to use checksums |
515d226413277ad64f089fcfd44537060eeb1e840bea0fd077ee214520e6bec9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.1
|
Security for flexibleloop 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.