No project description provided
Project description
foreach
foreach
is a tiny Python package that provides a parallel for loop implementation using multiprocessing.
Installation
You can install the package using pip:
pip install foreach
Usage
from foreach import foreach
def square(x):
return x**2
params = [1, 2, 3, 4, 5]
result = foreach(square, params)
print(result)
# [1, 4, 9, 16, 25]
foreach
Parameters
foreach(func: Callable, params: Iterable, parallel: bool = True, num_processes: int = None, callback: Callable = None) -> List
func
(callable): The function to apply to each element.params
(iterable): The iterable of parameters to apply the function to.parallel
(bool): If True, use multiprocessing for parallel execution. Default is True.num_processes
(int): Number of processes to use in parallel execution (default is None, letting Pool decide).callback
(callable): Optional callback function to execute after each iteration.
Returns:
- List: A list of results from applying the function to each element.
Examples
Sequential Execution
from foreach import foreach
def square(x):
return x**2
params = [1, 2, 3, 4, 5]
result = foreach(square, params, parallel=False)
print(result)
# [1, 4, 9, 16, 25]
Callback Functionality
from foreach import foreach
def square(x):
return x**2
params = [1, 2, 3, 4, 5]
def callback(x):
print(f"Processed: {x}")
result = foreach(square, params, callback=callback)
# Processed: 1
# Processed: 4
# Processed: 9
# Processed: 16
# Processed: 25
Customizing Number of Processes
from foreach import foreach
def square(x):
return x**2
params = [1, 2, 3, 4, 5]
result = foreach(square, params, num_processes=2)
print(result)
# [1, 4, 9, 16, 25]
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
foreach-0.4.0.tar.gz
(2.5 kB
view details)
Built Distribution
File details
Details for the file foreach-0.4.0.tar.gz
.
File metadata
- Download URL: foreach-0.4.0.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/6.5.0-26-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7419a805800ee603f1297928d20355596f677ae58c05f3731304066912fec12 |
|
MD5 | c7848cf643f326109cd56dcc1dfe5eb1 |
|
BLAKE2b-256 | 50b548868df0e4393795ae432d41d277df187ddd9ddeb2187e254a2d07a20506 |
File details
Details for the file foreach-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: foreach-0.4.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/6.5.0-26-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fd68a5fd03842604ed608ae478690b1c894075988d7c97b10d673f490d7d879 |
|
MD5 | f1d4657c613c215cfda0c7f6a333f62c |
|
BLAKE2b-256 | f65a1a643844084e8357bec2bc996026431147524a729ae9b8b71ab7a7e0b27b |