I want to implenent a decorator accepting a probability as an argument with the following syntax:
@trigger_randomly(0.5)
def func():
Using only the numpy
library.
The probability indicates how likely is the function to be executed when any of the decorated functions are being called.
It's should support also initialization without providing a probability. In that case the probability should be the complement of initialized probabilities.
Example:
@trigger_randomly(0.5)
def first():
return "good person"
@trigger_randomly(0.4)
def second():
return "kind person"
@trigger_randomly(0.1)
def third():
return "happy person"
@trigger_randomly()
def fourth():
return "beautiful person"
@trigger_randomly()
def fifth():
return "reliable person"
Running:
personality = {"good person": 0,
"kind person": 0,
"happy person": 0,
"beautiful person": 0,
"reliable person": 0}
for i in range(1000):
personality[fourth()] += 1
print(personality)
I would like to create a function that receives a probability and returns the strings based on percentages.
function declaration: def trigger_randomly(probability=None)
Should print sentences count based on probability:
{'good person': 500, 'kind person': 301, 'happy person': 95,
'beautiful person': 56, 'reliable person': 48}
My code
import numpy as np
from functools import wraps
from random import choices
functions, weights = [], []
def trigger_randomly(probability=None):
if probability == None:
probability = 0.1/2
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
fn ,= choices(functions, weights=weights)
return fn(*args, **kwargs)
functions.append(fn)
weights.append(probability)
return wrapper
return decorator
@trigger_randomly(0.5)
def first():
return "good person"
@trigger_randomly(0.3)
def second():
return "kind person"
@trigger_randomly(0.1)
def third():
return "happy person"
@trigger_randomly()
def fourth():
return "beautiful person"
@trigger_randomly()
def fifth():
return "reliable person"
personality = {"good person": 0,
"kind person": 0,
"happy person": 0,
"beautiful person": 0,
"reliable person": 0}
for i in range(1000):
personality[fourth()] += 1
print(personality)
I need to use only numpy library