Passed
Push — web-timed ( cfeda2...1d9e3e )
by Matt
01:11
created

TimedEvent.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 5
1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX via Python. Featuring fixture profiles and working with uDMX.
3
 *  <https://github.com/MattIPv4/PyDMXControl/>
4
 *  Copyright (C) 2018 Matt Cowley (MattIPv4) ([email protected])
5
"""
6
7
from time import time
8
from typing import Dict
9
10
11
class TimedEvent:
12
13
    def __init__(self, run_time: int, callback: callable, args: tuple = (), name: str = ""):
14
        self.__time = run_time
15
        self.__cb = callback
16
        self.__args = args
17
        self.__name = name
18
        self.__fired = None
19
20
    @property
21
    def time(self) -> str:
22
        return "{}ms".format("{:.4f}".format(self.__time).rstrip("0").rstrip("."))
23
24
    @property
25
    def name(self) -> str:
26
        return "{}".format(self.__name)
27
28
    @property
29
    def func(self) -> str:
30
        return "<func {}>".format(self.__cb.__name__)
31
32
    @property
33
    def args(self) -> str:
34
        return "[{}]".format(", ".join(["{}".format(f) for f in self.__args]))
35
36
    @property
37
    def fired(self) -> str:
38
        if self.__fired is None:
39
            return ""
40
        return "{:.4f}ms ({:.4f}ms late)".format(self.__fired, self.__fired - self.__time)
41
42
    @property
43
    def data(self) -> Dict[str, str]:
44
        return {
45
            "time": self.time,
46
            "name": self.name,
47
            "func": self.func,
48
            "args": self.args,
49
            "fired": self.fired
50
        }
51
52
    def __str__(self) -> str:
53
        return "Event {} (\"{}\") {}".format(self.__time, self.name, self.func)
54
55
    def run(self, start_time) -> str:
56
        self.__cb(*self.__args)
57
        self.__fired = (time() * 1000.0) - start_time
58
        return "{} fired at {}".format(str(self), self.fired)
59