[docs]@contextlib.contextmanagerdefconnect(db_path:str)->Generator[sqlite3.Connection]:"""Context manager to connect to a sqlite database. :param db_path: path to the sqlite database :return: A Generator yielding an open sqlite connection """withcontextlib.closing(sqlite3.connect(db_path,detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES,),)asdb:db.row_factory=sqlite3.Rowwithdb:yielddb
P=ParamSpec('P')R=TypeVar('R')
[docs]defretry(retries:int,exceptions:tuple[type[Exception],...]=(Exception,),)->Callable[[Callable[P,R]],Callable[P,R]]:"""Decorator to retry a function ``retries`` times when a specific exceptions is raised (defined in ``exceptions``). If any other exception is raised, it will not retry the function. It can be used like this: A function is decorated and it is retried a maximum of 10 times when a ``ValueError`` or ``KeyError`` is raised. Every other exception will instantly be raised. .. code-block:: python @retry(retries=10, exceptions=(ValueError, KeyError)) def my_func(): ... :param retries: number of times a function is retried :param exceptions: the exceptions to except and retry """defretry_dec(f:Callable[P,R])->Callable[P,R]:@wraps(f)definner(*args:P.args,**kwargs:P.kwargs)->R:curr_tries=0whileTrue:try:returnf(*args,**kwargs)exceptexceptions:ifcurr_tries>=retries:raisecurr_tries+=1returninnerreturnretry_dec
K=TypeVar('K')V=TypeVar('V')
[docs]classFrozenDict(Generic[K,V]):"""Immutable, generic implementation of a frozen dictionary."""def__init__(self,d:Mapping[K,V])->None:self._d=ddef__getitem__(self,k:K)->V:returnself._d[k]def__contains__(self,k:K)->bool:returnkinself._ddef__iter__(self)->Iterator[K]:yield fromself._ddefget(self,k:K)->V|None:returnself._d.get(k)defvalues(self)->Iterable[V]:returnself._d.values()defkeys(self)->Iterable[K]:returnself._d.keys()defitems(self)->ItemsView[K,V]:returnself._d.items()def__repr__(self)->str:returnf'{type(self).__name__}({self._d})'