| Total Complexity | 11 |
| Total Lines | 63 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 | "time_raw": self.__time, |
||
| 47 | "name": self.name, |
||
| 48 | "func": self.func, |
||
| 49 | "args": self.args, |
||
| 50 | "fired": self.fired |
||
| 51 | } |
||
| 52 | |||
| 53 | def __str__(self) -> str: |
||
| 54 | return "Event {} (\"{}\") {}".format(self.__time, self.name, self.func) |
||
| 55 | |||
| 56 | def run(self, start_time) -> str: |
||
| 57 | self.__cb(*self.__args) |
||
| 58 | self.__fired = (time() * 1000.0) - start_time |
||
| 59 | return "{} fired at {}".format(str(self), self.fired) |
||
| 60 | |||
| 61 | def reset_fired(self): |
||
| 62 | self.__fired = None |
||
| 63 |