Skip to content

File: Broken/Core/BrokenTrackers.py

Broken.Core.BrokenTrackers

SameTracker

Doumo same desu. If a value is the same, returns True, else updates it and returns False • Useful on ignoring expensive calls where parameters doesn't changes

Source code in Broken/Core/BrokenTrackers.py
11
12
13
14
15
16
17
18
19
20
21
@define
class SameTracker:
    """Doumo same desu. If a value is the same, returns True, else updates it and returns False
    • Useful on ignoring expensive calls where parameters doesn't changes"""
    value: Any = None

    def __call__(self, value: Any=True) -> bool:
        if self.value != value:
            self.value = value
            return False
        return True

value

value: Any = None

__call__

__call__(value: Any = True) -> bool
Source code in Broken/Core/BrokenTrackers.py
17
18
19
20
21
def __call__(self, value: Any=True) -> bool:
    if self.value != value:
        self.value = value
        return False
    return True

OnceTracker

Returns False the first time it's called, never nest style: if once/already(): return

Source code in Broken/Core/BrokenTrackers.py
23
24
25
26
27
28
29
30
31
32
@define
class OnceTracker:
    """Returns False the first time it's called, never nest style: `if once/already(): return`"""
    _first: bool = False

    def __call__(self) -> bool:
        if (not self._first):
            self._first = True
            return False
        return True

__call__

__call__() -> bool
Source code in Broken/Core/BrokenTrackers.py
28
29
30
31
32
def __call__(self) -> bool:
    if (not self._first):
        self._first = True
        return False
    return True

PlainTracker

Source code in Broken/Core/BrokenTrackers.py
34
35
36
37
38
39
40
41
42
@define
class PlainTracker:
    value: Any = None

    def __call__(self, set: bool=None) -> bool:
        """Returns value if None else sets it"""
        if (set is not None):
            self.value = set
        return self.value

value

value: Any = None

__call__

__call__(set: bool = None) -> bool

Returns value if None else sets it

Source code in Broken/Core/BrokenTrackers.py
38
39
40
41
42
def __call__(self, set: bool=None) -> bool:
    """Returns value if None else sets it"""
    if (set is not None):
        self.value = set
    return self.value

FileTracker

Source code in Broken/Core/BrokenTrackers.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@define
class FileTracker:
    file: Path = field(converter=Path)
    retention: DotMap = Factory(lambda: DotMap(days=1, hours=0))

    def __attrs_post_init__(self):
        self.file.touch()

        # Initialize new or empty trackers
        if (not self.file.read_text("utf-8")):
            self._first = True
            self.update()

    _first: bool = False

    @property
    def first(self) -> bool:
        """True if initializing the tracker for the first time"""
        return self._first

    def __enter__(self) -> Self:
        return self

    def __exit__(self, *args) -> None:
        return None

    @property
    def last(self) -> "arrow.Arrow":
        """How long it's been since the last run"""
        import arrow
        return arrow.get(self.file.read_text("utf-8"))

    @property
    def sleeping(self, granularity: tuple[str]=("day")) -> str:
        """How long it's been since the last run, for printing purposes"""
        return self.last.humanize(only_distance=True, granularity=granularity)

    def trigger(self, update: bool=False) -> bool:
        """True if it's been more than 'self.retention' since the last run"""
        import arrow
        trigger = (self.last.shift(**self.retention) < arrow.utcnow())
        if (trigger and update):
            self.update()
        return trigger

    def update(self, **shift: dict) -> None:
        import arrow
        time = arrow.utcnow().shift(**(shift or {}))
        self.file.write_text(str(time), "utf-8")

file

file: Path = field(converter=Path)

retention

retention: DotMap = Factory(lambda: DotMap(days=1, hours=0))

__attrs_post_init__

__attrs_post_init__()
Source code in Broken/Core/BrokenTrackers.py
49
50
51
52
53
54
55
def __attrs_post_init__(self):
    self.file.touch()

    # Initialize new or empty trackers
    if (not self.file.read_text("utf-8")):
        self._first = True
        self.update()

first

first: bool

True if initializing the tracker for the first time

__enter__

__enter__() -> Self
Source code in Broken/Core/BrokenTrackers.py
64
65
def __enter__(self) -> Self:
    return self

__exit__

__exit__(*args) -> None
Source code in Broken/Core/BrokenTrackers.py
67
68
def __exit__(self, *args) -> None:
    return None

last

last: arrow.Arrow

How long it's been since the last run

sleeping

sleeping: str

How long it's been since the last run, for printing purposes

trigger

trigger(update: bool = False) -> bool

True if it's been more than 'self.retention' since the last run

Source code in Broken/Core/BrokenTrackers.py
81
82
83
84
85
86
87
def trigger(self, update: bool=False) -> bool:
    """True if it's been more than 'self.retention' since the last run"""
    import arrow
    trigger = (self.last.shift(**self.retention) < arrow.utcnow())
    if (trigger and update):
        self.update()
    return trigger

update

update(**shift: dict) -> None
Source code in Broken/Core/BrokenTrackers.py
89
90
91
92
def update(self, **shift: dict) -> None:
    import arrow
    time = arrow.utcnow().shift(**(shift or {}))
    self.file.write_text(str(time), "utf-8")