r/pythontips • u/warbird2k • Feb 25 '19
Play a sound when a function has finished running
Some times it can be useful to have a sound play after a function has finished running (e.g. time consuming data science tasks). Here is a short decorator you can use on your functions to achieve this (win only... i think?)
import time
import winsound
def play_warning(func):
def wrapper(*args, **kwargs):
func(*args, **kwargs)
winsound.MessageBeep()
return wrapper
@play_warning
def testfunc(secs):
for _ in range(secs):
time.sleep(1)
print("Slept" + "."*_)
testfunc(5)