Snippets

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

Updated by Lilian Besson (Naereen)

File drunk-python.py Modified

  • Ignore whitespace
  • Hide word diff
+# See https://nbviewer.jupyter.org/github/Naereen/notebooks/blob/master/Living_in_a_noisy_world_with_James_Powell_rwatch_module.ipynb
+
 # Install it from https://github.com/dutc/rwatch
 import rwatch
 from sys import setrwatch, getrwatch
Created by Lilian Besson (Naereen)

File demo.txt Added

  • Ignore whitespace
  • Hide word diff
+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

File drunk-python.py Added

  • Ignore whitespace
  • Hide word diff
+# 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.