Cache(): a dict that keeps track of "x in Cache()" calls to enable statistics

This commit is contained in:
Stefan Harmuth 2022-12-19 04:48:08 +01:00
parent 2692f4b560
commit f5544299da

View File

@ -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):