Snippets

Lilian Besson (Naereen) Get your Python interpreter drunk with @dutc rwatch and random noise

You are viewing an old version of this snippet. View the current version.
Revised by Lilian Besson (Naereen) c2d5908
Warning: your Python intepreter is now drunk, every number it sees are noisy...
Use 'setrwatch({})' to disable it.

In [2]: 0
Out[2]: 10.7445342051449549

In [3]: 1
Out[3]: -0.35048804156890245

In [4]: 1 / 0
Out[4]: -2.546899181270205
# Install it from https://github.com/dutc/rwatch
import rwatch
from sys import setrwatch, getrwatch
setrwatch({})  # clean any previously installed rwatch

import numpy as np
from collections import defaultdict
from inspect import getframeinfo
from numbers import Number

def add_white_noise_to_every_numbers(frame, obj):
    if isinstance(obj, Number):
        info = getframeinfo(frame)
        if '<stdin>' in info.filename or '<ipython-' in info.filename:
            return obj + np.random.normal()
    return obj

print("Warning: your Python intepreter is now drunk, every number it sees are noisy...")
print("Use 'setrwatch({})' to disable it.")
setrwatch(defaultdict(lambda: add_white_noise_to_every_numbers))

# That's it, your interpreter is now drunk!
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.