Skip to content

Instantly share code, notes, and snippets.

@vsajip
Forked from walkermatt/debounce.py
Created December 16, 2022 21:26
Show Gist options
  • Save vsajip/a18e3f8f1d1daa8083c9ec707c9fb1a7 to your computer and use it in GitHub Desktop.
Save vsajip/a18e3f8f1d1daa8083c9ec707c9fb1a7 to your computer and use it in GitHub Desktop.

Revisions

  1. @walkermatt walkermatt revised this gist Jun 10, 2012. 2 changed files with 42 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions debounce.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    from threading import Timer


    def debounce(wait):
    """ Decorator that will postpone a functions
    execution until after wait seconds
    have elapsed since the last time it was invoked. """
    def decorator(fn):
    def debounced(*args):
    def debounced(*args, **kwargs):
    def call_it():
    return fn(*args)
    fn(*args, **kwargs)
    try:
    debounced.t.cancel()
    except(AttributeError):
    @@ -16,8 +17,3 @@ def call_it():
    debounced.t.start()
    return debounced
    return decorator


    @debounce(10)
    def do_expensive_thing(foo):
    print 'My, that was expensive: %s' % foo
    39 changes: 39 additions & 0 deletions test_debounce.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import unittest
    import time
    from debounce import debounce


    class TestDebounce(unittest.TestCase):

    @debounce(10)
    def increment(self):
    """ Simple function that
    increments a counter when
    called, used to test the
    debounce function decorator """
    self.count += 1

    def setUp(self):
    self.count = 0

    def test_debounce(self):
    """ Test that the increment
    function is being debounced.
    The counter should only be incremented
    once 10 seconds after the last call
    to the function """
    self.assertTrue(self.count == 0)
    self.increment()
    self.increment()
    time.sleep(9)
    self.assertTrue(self.count == 0)
    self.increment()
    self.increment()
    self.increment()
    self.increment()
    self.assertTrue(self.count == 0)
    time.sleep(10)
    self.assertTrue(self.count == 1)

    if __name__ == '__main__':
    unittest.main()
  2. @walkermatt walkermatt revised this gist Jun 4, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions debounce.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    from threading import Timer

    def debounce(wait):
    """ Decorator that will postpone a functions
    execution until after wait seconds
  3. @walkermatt walkermatt created this gist Jun 4, 2012.
    21 changes: 21 additions & 0 deletions debounce.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    def debounce(wait):
    """ Decorator that will postpone a functions
    execution until after wait seconds
    have elapsed since the last time it was invoked. """
    def decorator(fn):
    def debounced(*args):
    def call_it():
    return fn(*args)
    try:
    debounced.t.cancel()
    except(AttributeError):
    pass
    debounced.t = Timer(wait, call_it)
    debounced.t.start()
    return debounced
    return decorator


    @debounce(10)
    def do_expensive_thing(foo):
    print 'My, that was expensive: %s' % foo