Created
March 16, 2017 11:05
-
-
Save olof/12a0c9713134c2fe595eee170a3cced2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class DontExperiment(Exception): | |
pass | |
def experiment(rate=0.1): | |
""" | |
Generate an experiment decatorator; it will raise an DontExperiment | |
exception with a probability of <rate>. | |
>>> @experiment(rate=0.5) | |
>>> def prototype(foo, bar): | |
>>> return foo*bar | |
>>> | |
>>> try: | |
>>> res = prototype(10, 10) | |
>>> except DontExperiment: | |
>>> res = 10+10 | |
""" | |
def decorator(exp): | |
def ret(*args, **kwargs): | |
if rate > random.random(): | |
raise DontExperiment(exp.__name__) | |
return exp(*args, **kwargs) | |
return ret | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment