Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to skip first runs #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions pyprofilers/pyprofilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +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):
def decorator_profile(func):
def profile_by_line(_func=None, *, exit=False, skip=None):
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)
profile.print_stats()
if exit:
nonlocal counter
if counter == exit:
print(
f"Exit after {formatted_number(counter)} call of {func.__name__}"
)
nonlocal counter
if skip is None or counter >= skip:
result = func(*args, **kwargs)
profile.print_stats()
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
Expand Down