Execute your method with combination parameters.
Project description
combu
Execute your method with combination parameters.
Use cases
- Testing
- Test pattern.
- Machine learning
- Model validation.
- Grid search.
- Web Scraping
- Query parameters pattern.
Install
pip install combu
Usage
One time loop
import combu
def func(v1, v2):
return v1 + v2
params = {'v1': ['a', 'b'], 'v2': ['A', 'B']}
for res, param in combu.execute(func, params):
print(res, param)
# Output
'''
aA {'v1': 'a', 'v2': 'A'}
aB {'v1': 'a', 'v2': 'B'}
bA {'v1': 'b', 'v2': 'A'}
bB {'v1': 'b', 'v2': 'B'}
'''
# Set loop order
order = ['v2', 'v1']
for res, param in combu.execute(func, params, order=order):
print(res, param)
# Output
'''
aA {'v2': 'A', 'v1': 'a'}
bA {'v2': 'A', 'v1': 'b'}
aB {'v2': 'B', 'v1': 'a'}
bB {'v2': 'B', 'v1': 'b'}
'''
Reloopable by using class
import combu
def func(v1, v2):
return v1 + v2
comb = combu.Combu(func)
# You can set order on initializer.
# comb = combu.Combu(func, order=['v2', 'v1'])
# If you want to show progress bar.
# comb = combu.Combu(func, progress=True)
params = {'v1': ['a', 'b'], 'v2': ['A', 'B']}
for res, param in comb.execute(params):
print(res, param)
params = {'v1': ['x', 'y'], 'v2': ['X', 'Y']}
for res, param in comb.execute(params):
print(res, param)
# You can set order on Combu.execute().
for res, param in comb.execute(params, order=['v2', 'v1']):
print(res, param)
Hooks
-
Hooks flow
order = [A, B] before_a() for a in A: before_b() before_each_a() for b in B: before_each_b() func() after_each_b() after_each_a() after_b() after_a()
-
Define hooks.
def func(v1, v2): pass def before_v1(v1, v2): pass # Initialize with hooks. # Available: # * before # * after # * before_each # * after_each comb = Comb(func, before={'v1': before_v1}) # Set a hook after initialized. # Available: # * set_before(k, func) # * set_after(k, func) # * set_before_each(k, func) # * set_after_each(k, func) comb.set_before('v1', before_v1)
Parallel
# Use n_jobs parameter.
for res, param in combu.execute(func, params, n_jobs=2):
print(res, param)
# Use combu.CombuParallel and n_jobs.
# n_jobs=-1 mean "use all cores."
comb = combu.CombuParallel(func, n_jobs=-1)
Utility
- Create parameter combination (not execute any functions).
combu.create_values
- Count combinations.
combu.util.count
- Shuffle parameters.
combu.util.shuffle_params
Aliases
- combu.exec -> combu.execute
- combu.values -> combu.create_values
Examples
- Available on
./examples
.
Development
- Requirements: poetry, pyenv
# Setup
poetry install
# Lint & Test
mkdir report
poetry run flake8 --format=html --htmldir=report/flake-report src/ tests/
poetry run mypy src/ tests/combu/
poetry run pytest tests/
poetry run pytest tests/ --cov-report html:report/coverage
# Build and publish
poetry run python create_badges.py
poetry build
poetry publish
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
combu-1.2.1.tar.gz
(8.5 kB
view details)
Built Distribution
combu-1.2.1-py3-none-any.whl
(8.9 kB
view details)
File details
Details for the file combu-1.2.1.tar.gz
.
File metadata
- Download URL: combu-1.2.1.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.6.12 Linux/4.19.128-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f312fbf8ee704f644e8098522e4c1cd938356189c11fd9fd84406bfa987f10d8 |
|
MD5 | 0bfff45f0d49f408d61da5058133ee12 |
|
BLAKE2b-256 | 210f7f526b664f37dbe34f103dce9f3cdaac8886c79e7df447e0ae261d425a0e |
File details
Details for the file combu-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: combu-1.2.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.6.12 Linux/4.19.128-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efb3ce33e48791c1d0622e47eacf25a12273853071b39d54ac39ae8df44cc473 |
|
MD5 | d4da3a18984e649da21ce2ca63138566 |
|
BLAKE2b-256 | ba29456fbb03ef3c2ce994f4464a21577f935800f9e15f981316594bb4c90f55 |