Пример #1
0
    def __init__(self, func, wait, max_wait=False):
        self.func = func
        self.wait = wait
        self.max_wait = max_wait

        self.last_result = None

        # Initialize last_* times to be prior to the wait periods so that func
        # is primed to be executed on first call.
        self.last_call = pyd.now() - self.wait
        self.last_execution = pyd.now() - max_wait if pyd.is_number(
            max_wait) else None
Пример #2
0
    def __init__(self, func, wait, max_wait=False):
        self.func = func
        self.wait = wait
        self.max_wait = max_wait

        self.last_result = None

        # Initialize last_* times to be prior to the wait periods so that func
        # is primed to be executed on first call.
        self.last_call = pyd.now() - self.wait
        self.last_execution = (pyd.now() - max_wait if pyd.is_number(max_wait)
                               else None)
Пример #3
0
def test_debounce_max_wait():
    def func(): return _.now()

    wait = 250
    max_wait = 300
    debounced = _.debounce(func, wait, max_wait=max_wait)

    start = _.now()
    present = _.now()

    expected = debounced()

    while (present - start) <= (max_wait + 5):
        result = debounced()
        present = _.now()

    assert result > expected
Пример #4
0
def test_debounce_max_wait():
    def func():
        return _.now()

    wait = 250
    max_wait = 300
    debounced = _.debounce(func, wait, max_wait=max_wait)

    start = _.now()
    present = _.now()

    expected = debounced()

    while (present - start) <= (max_wait + 5):
        result = debounced()
        present = _.now()

    assert result > expected
Пример #5
0
def test_throttle():
    def func(): return _.now()

    wait = 250
    throttled = _.throttle(func, wait)

    start = _.now()
    present = _.now()

    expected = throttled()

    while (present - start) < (wait - 50):
        result = throttled()
        present = _.now()

    assert result == expected

    time.sleep(100 / 1000.0)
    assert throttled() > expected
Пример #6
0
def test_throttle():
    def func():
        return _.now()

    wait = 250
    throttled = _.throttle(func, wait)

    start = _.now()
    present = _.now()

    expected = throttled()

    while (present - start) < (wait - 50):
        result = throttled()
        present = _.now()

    assert result == expected

    time.sleep(100 / 1000.0)
    assert throttled() > expected
Пример #7
0
    def __call__(self, *args, **kargs):
        """Execute :attr:`func` if function hasn't been called witinin last
        :attr:`wait` milliseconds. Return results of last successful call.
        """
        present = pyd.now()

        if (present - self.last_execution) >= self.wait:
            self.last_result = self.func(*args, **kargs)
            self.last_execution = present

        return self.last_result
Пример #8
0
def test_debounce():
    def func(): return _.now()

    wait = 250
    debounced = _.debounce(func, wait)

    start = _.now()
    present = _.now()

    expected = debounced()

    while (present - start) <= wait + 100:
        result = debounced()
        present = _.now()

    assert result == expected

    time.sleep(wait / 1000.0)
    result = debounced()

    assert result > expected
Пример #9
0
def test_debounce():
    def func():
        return _.now()

    wait = 250
    debounced = _.debounce(func, wait)

    start = _.now()
    present = _.now()

    expected = debounced()

    while (present - start) <= wait + 100:
        result = debounced()
        present = _.now()

    assert result == expected

    time.sleep(wait / 1000.0)
    result = debounced()

    assert result > expected
Пример #10
0
    def func(): return _.now()

    wait = 250
Пример #11
0
def test_now():
    present = int(time.time() * 1000)
    # Add some leeway when comparing time.
    assert (present - 1) <= _.now() <= (present + 1)
Пример #12
0
    def __init__(self, func, wait):
        self.func = func
        self.wait = wait

        self.last_result = None
        self.last_execution = pyd.now() - self.wait
Пример #13
0
 def func():
     return _.now()
Пример #14
0
def test_now():
    present = int(time.time() * 1000)
    # Add some leeway when comparing time.
    assert (present - 1) <= _.now() <= (present + 1)
Пример #15
0
    def __init__(self, func, wait):
        self.func = func
        self.wait = wait

        self.last_result = None
        self.last_execution = pyd.now() - self.wait
Пример #16
0
 def func():
     return _.now()