Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. from time import time
  2. from IPython import get_ipython
  3. from IPython.display import Audio, display
  4.  
  5. class Beeper:
  6.  
  7. def __init__(self, threshold, **audio_kwargs):
  8. self.threshold = threshold
  9. self.start_time = None # time in sec, or None
  10. self.audio = audio_kwargs
  11.  
  12. def pre_execute(self):
  13. if not self.start_time:
  14. self.start_time = time()
  15.  
  16. def post_execute(self):
  17. end_time = time()
  18. if self.start_time and end_time - self.start_time > self.threshold:
  19. audio = Audio(**self.audio, autoplay=True)
  20. display(audio)
  21. self.start_time = None
  22.  
  23. beeper = Beeper(5, filename='beep-07.wav')
  24.  
  25. ipython = get_ipython()
  26. ipython.events.register('pre_execute', beeper.pre_execute)
  27. ipython.events.register('post_execute', beeper.post_execute)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement