From 1d1d6d2c702cfddc57e1f95c1f9b9870bcb53caa Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Thu, 24 Sep 2020 17:48:04 +0200 Subject: [PATCH 1/3] add possiblibty to skip first runs --- pyprofilers/pyprofilers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyprofilers/pyprofilers.py b/pyprofilers/pyprofilers.py index 982beb0..9125391 100644 --- a/pyprofilers/pyprofilers.py +++ b/pyprofilers/pyprofilers.py @@ -85,7 +85,7 @@ def formatted_number(number): return f"{number}{suffix}" -def profile_by_line(_func=None, *, exit=False): +def profile_by_line(_func=None, *, exit=False, skip=None): def decorator_profile(func): from line_profiler import LineProfiler @@ -96,7 +96,8 @@ def decorator_profile(func): @functools.wraps(func) def wrapper(*args, **kwargs): result = func(*args, **kwargs) - profile.print_stats() + if pass is None or counter > skip: + profile.print_stats() if exit: nonlocal counter if counter == exit: From 163ac4517049c81d4d63906c2952b95079bac58d Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Thu, 24 Sep 2020 17:49:08 +0200 Subject: [PATCH 2/3] fix --- pyprofilers/pyprofilers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyprofilers/pyprofilers.py b/pyprofilers/pyprofilers.py index 9125391..9773e1e 100644 --- a/pyprofilers/pyprofilers.py +++ b/pyprofilers/pyprofilers.py @@ -96,7 +96,7 @@ def decorator_profile(func): @functools.wraps(func) def wrapper(*args, **kwargs): result = func(*args, **kwargs) - if pass is None or counter > skip: + if skip is None or skip >= counter: profile.print_stats() if exit: nonlocal counter From 8dc5f0181c492e4d4e0a6e91fb51b7744b669546 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Thu, 24 Sep 2020 18:54:49 +0200 Subject: [PATCH 3/3] update --- pyprofilers/pyprofilers.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/pyprofilers/pyprofilers.py b/pyprofilers/pyprofilers.py index 9773e1e..3d29680 100644 --- a/pyprofilers/pyprofilers.py +++ b/pyprofilers/pyprofilers.py @@ -79,33 +79,30 @@ def wrapper(*args, **kwargs): return decorator_profile(_func) -def formatted_number(number): - remainder = number % 10 - suffix = ("st", "nd", "rd")[remainder - 1] if remainder < 3 else "th" - return f"{number}{suffix}" +def suffix(number): + return f"{number}{('st', 'nd', 'rd', 'th')[min(number % 10 - 1, 3)]}" def profile_by_line(_func=None, *, exit=False, skip=None): - def decorator_profile(func): + def decorator_profile(o_func): from line_profiler import LineProfiler profile = LineProfiler() - func = simple_timer(profile(func)) - counter = 1 + func = simple_timer(profile(o_func)) + counter = 0 @functools.wraps(func) def wrapper(*args, **kwargs): - result = func(*args, **kwargs) - if skip is None or skip >= counter: + nonlocal counter + if skip is None or counter >= skip: + result = func(*args, **kwargs) profile.print_stats() - if exit: - nonlocal counter - if counter == exit: - print( - f"Exit after {formatted_number(counter)} call of {func.__name__}" - ) + if exit: + print(f"Exited after {suffix(counter + 1)} call of {func.__name__}") sys.exit() - counter += 1 + else: + result = o_func(*args, **kwargs) + counter += 1 return result return wrapper