Untitled

 avatar
unknown
python
a year ago
865 B
8
Indexable
class ProfileManager:
    profiles = {}
    
    @classmethod
    def profile(cls, func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            error_occurred = False
            result = None

            try:
                result = func(*args, **kwargs)
            except Exception as e:
                error_occurred = True
                result = e

            # Save the stats
            if func not in cls.profiles:
                cls.profiles[func.__qualname__] = []
            cls.profiles[func.__qualname__].append(Stats(args, kwargs, result, error_occurred))

            if error_occurred:
                raise result  # Re-raise the exception if the function call failed

            return result

        return wrapper

    @classmethod
    def get_profile(cls, func):
        return cls.profiles.get(func.__qualname__, [])
Editor is loading...
Leave a Comment