Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
865 B
6
Indexable
Never
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__, [])
Leave a Comment