29 lines
697 B
Python
29 lines
697 B
Python
# -*- coding-utf-8 -*-
|
|
import sys
|
|
from functools import wraps
|
|
from typing import Callable, Type, TypeVar
|
|
|
|
T = TypeVar('T')
|
|
|
|
|
|
def singleton(cls: Type[T]) -> Type[T]:
|
|
singleton.instances = {}
|
|
|
|
def getinstance():
|
|
if cls not in singleton.instances:
|
|
singleton.instances[cls] = cls()
|
|
return singleton.instances[cls]
|
|
|
|
return getinstance
|
|
|
|
|
|
def use_exceptionLogging(func: Callable):
|
|
""" Redirect any unexpected tracebacks """
|
|
@wraps(func)
|
|
def run(*args, **kwargs):
|
|
try:
|
|
return func(*args, **kwargs)
|
|
except: #pylint: disable=bare-except
|
|
sys.excepthook(*sys.exc_info())
|
|
|
|
return run
|