Skip to main content

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 @maybe and specify the desired distribution and parameters.
  • Flexible Probability Control: Tailor the execution probability to fit your specific needs.

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.

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 MIT License. See the LICENSE file for more details.


Mayhap: Embrace the uncertainty in your code execution!

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

mayhap-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mayhap-0.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file mayhap-0.1.0.tar.gz.

File metadata

  • Download URL: mayhap-0.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for mayhap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0d7c9c8a919a96efe463e58fa1a1b1e3718537ffb1f2a51881a620adef315742
MD5 666c977e024f1954408a7e3ff69ca4b3
BLAKE2b-256 d9d5d7b1daf4c9280f25fbf669a5b88663e5628b301bc0b5c034b8023ddbcdf0

See more details on using hashes here.

File details

Details for the file mayhap-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mayhap-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for mayhap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4da35efa6e5c97a330dd270079c640cc7aa946837eeef2a9af916d5690a6f6b4
MD5 61245c56465815a25f0b4e4f05ea4334
BLAKE2b-256 a0545711e547752937c6ab6b905d42de87dda3be39c57786f7918bfb854322ec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page