Skip to content

Instantly share code, notes, and snippets.

@olof
Created March 16, 2017 11:05
Show Gist options
  • Save olof/12a0c9713134c2fe595eee170a3cced2 to your computer and use it in GitHub Desktop.
Save olof/12a0c9713134c2fe595eee170a3cced2 to your computer and use it in GitHub Desktop.
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