File: do_later.py

package info (click to toggle)
python-pyface 6.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,756 kB
  • sloc: python: 39,728; makefile: 79
file content (66 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) 2005-18, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought util package component>

from pyface.timer.timer import CallbackTimer, Timer


class DoLaterTimer(Timer):
    """ Performs a callback once at a later time.

    This is not used by the `do_later` functions and is only provided for
    backwards compatibility of the API.
    """

    #: The perform the callback once.
    repeat = 1

    def __init__(self, interval, callable, args, kw_args):
        # Adapt the old DoLaterTimer initializer to the Timer initializer.
        super(DoLaterTimer, self).__init__(interval, callable, *args, **kw_args)


def do_later(callable, *args, **kwargs):
    """ Does something 50 milliseconds from now.

    Parameters
    ----------
    callable : callable
        The callable to call in 50ms time.
    *args, **kwargs :
        Arguments to be passed through to the callable.
    """
    return CallbackTimer.single_shot(
        interval=0.05,
        callback=callable,
        args=args,
        kwargs=kwargs,
    )


def do_after(interval, callable, *args, **kwargs):
    """ Does something after some specified time interval.

    Parameters
    ----------
    interval : float
        The time interval in milliseconds to wait before calling.
    callable : callable
        The callable to call.
    *args, **kwargs :
        Arguments to be passed through to the callable.
    """
    return CallbackTimer.single_shot(
        interval=interval / 1000.0,
        callback=callable,
        args=args,
        kwargs=kwargs,
    )