Jaseci-Labs / jaseci

The Official Jaseci Code Repository
https://jaseci.org
156 stars 214 forks source link

Adding code profiler into our code base #1435

Open mgtm98 opened 2 weeks ago

mgtm98 commented 2 weeks ago

Description

A utils class that can be used to profile the python code.

time_function: A decorator that prints the time taken by a decorated function. start_marker: Starts a timer for a section of code, identified by a unique name. end_marker: Ends the timer for a given section and prints the time taken.

marsninja commented 1 day ago

Hey, here is how you do the type annotation, try chatgpt for more info @mgtm98

from typing import Callable, TypeVar, ParamSpec

P = ParamSpec("P")
R = TypeVar("R")

def decorator(func: Callable[P, R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        print(f"Called with args={args}, kwargs={kwargs}")
        return func(*args, **kwargs)
    return wrapper

@decorator
def example_function(x: int, y: str) -> str:
    return f"{x} and {y}"

result = example_function(1, "hello")  # Logs args and kwargs
print(result)  # Output: "1 and hello"