diff --git a/tools/tools.py b/tools/tools.py index eec7dcf..ec386c9 100644 --- a/tools/tools.py +++ b/tools/tools.py @@ -7,6 +7,25 @@ from functools import wraps from typing import Any +class Cache(dict): + def __init__(self): + super().__init__() + self.hits = 0 + self.misses = 0 + + def str_hits(self) -> str: + return "%d (%1.2f)" % (self.hits, self.hits / (self.misses + self.hits) * 100) + + def __contains__(self, item: Any) -> bool: + r = super().__contains__(item) + if r: + self.hits += 1 + else: + self.misses += 1 + + return r + + def get_script_dir(follow_symlinks: bool = True) -> str: """return path of the executed script""" if getattr(sys, 'frozen', False):