Where your functions may happen... or not. Embrace the uncertainty in your code execution!
Project description
Mayhap
Mayhap: Where your functions may happen... or not.
Embrace the uncertainty in your code execution!
Overview
Mayhap is a whimsical Python package that allows you to introduce controlled randomness into your function executions. By decorating your functions with @maybe, you can specify various probability distributions to determine whether a function should execute or not. It's perfect for simulations, testing, or just adding a bit of unpredictability to your code.
Features
- Multiple Distributions: Choose from uniform, weighted, normal, exponential, Bernoulli distributions, or define your own custom logic.
- Easy Integration: Simply decorate your functions with
@maybeand specify the desired distribution and parameters. - Flexible Probability Control: Tailor the execution probability to fit your specific needs.
- Optional Verbosity: Control whether skipped executions print a message with the
verboseflag.
Installation
Install Mayhap using pip:
pip install mayhap
Usage
Here's how you can use Mayhap in your projects:
Uniform Distribution
Execute a function with a fixed probability.
from mayhap import maybe
@maybe(distribution='uniform', probability=0.7)
def greet(name):
print(f"Hello, {name}!")
greet('Alice') # Has a 70% chance to print the greeting.
Weighted Distribution
Assign different weights to execution outcomes.
@maybe(distribution='weighted', weights=[3, 1])
def farewell(name):
print(f"Goodbye, {name}!")
farewell('Bob') # 'Goodbye, Bob!' is three times more likely to print than not.
Normal Distribution
Execution probability follows a normal (Gaussian) distribution.
@maybe(distribution='normal', mean=0.5, stddev=0.1)
def announce(event):
print(f"Announcing {event}!")
announce('the event') # Execution probability is centered around 50%.
Exponential Distribution
Execution probability decreases exponentially over time.
@maybe(distribution='exponential', lambd=1.0)
def notify(user):
print(f"Notification sent to {user}.")
notify('Charlie') # Execution probability decreases over time.
Bernoulli Distribution
Execute based on a Bernoulli trial with probability p.
@maybe(distribution='bernoulli', p=0.3)
def alert():
print("Alert triggered!")
alert() # Has a 30% chance to trigger the alert.
Custom Probability Function
Define your own logic for execution probability.
def custom_logic():
# Custom conditions for execution
return some_external_condition_check()
@maybe(distribution='custom', custom_func=custom_logic)
def process():
print("Processing data.")
process() # Executes based on custom logic.
Controlling Verbosity
You can control whether skipped function calls print a message with the verbose flag.
@maybe(distribution='uniform', probability=0.5, verbose=False)
def silent():
print("Might run... but won't explain if not.")
Contributing
Contributions are welcome! If you'd like to improve Mayhap or add new features, please fork the repository and submit a pull request. For major changes, open an issue first to discuss your ideas.
License
This project is licensed under the GPL-3.0-only License.
See the LICENSE file for more details.
Mayhap: Embrace the uncertainty in your code execution!
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 mayhap-0.2.0.tar.gz.
File metadata
- Download URL: mayhap-0.2.0.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7889e29ed67acb271e056500be9666f08c3f01b1f35031992c3fc04dcc7174cb
|
|
| MD5 |
a73f3dc8c70894ce03a216607431b891
|
|
| BLAKE2b-256 |
c118312ecbc630e6c8dcd06a883d4efdd0a52d3cb2d9e7a629c7ac6893b58a8d
|
File details
Details for the file mayhap-0.2.0-py3-none-any.whl.
File metadata
- Download URL: mayhap-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0caca76ed27b6ebd650b4c593c8f5f9687b1710ee2237a3754369036ff593a19
|
|
| MD5 |
4d2c8eda97b2a01175cc298b2fb9f551
|
|
| BLAKE2b-256 |
cf2af2384f79767152b20ddcbdc09645a5ec9cf823351ca2fd86c6ce5a99e28b
|