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

Print only N lines of result #171

Open
NicolasMICAUX opened this issue Apr 10, 2024 · 1 comment
Open

Print only N lines of result #171

NicolasMICAUX opened this issue Apr 10, 2024 · 1 comment

Comments

@NicolasMICAUX
Copy link

I cannot find an option to not print all the lines, but say only the first 500.

I'm trying:

stats = yappi.get_func_stats().sort("ttot")
# keep only the first 500 lines
stats = stats[:500]  # BUT THIS CONVERTS stats TO A LIST, SO print_all WILL NOT WORK
stats.print_all()

The solution is actually quite easy, something like this:

def print_all(
    self,
    out=sys.stdout,
    columns={0: ("name", 36), 1: ("ncall", 5), 2: ("tsub", 8), 3: ("ttot", 8), 4: ("tavg", 8)},
    N: int = None,
):
    """
    Prints all of the function profiler results to a given file. (stdout by default)
    """
    if self.empty():
        return

    for _, col in columns.items():
        _validate_columns(col[0], COLUMNS_FUNCSTATS)

    out.write(LINESEP)
    out.write(f"Clock type: {self._clock_type.upper()}")
    out.write(LINESEP)
    out.write(f"Ordered by: {self._sort_type}, {self._sort_order}")
    out.write(LINESEP)
    out.write(LINESEP)

    self._print_header(out, columns)
    for i, stat in enumerate(self):
        if N is not None and i >= N:
            break
        stat._print(out, columns)
@sumerc
Copy link
Owner

sumerc commented Jul 19, 2024

You are right! That might be handy. I will look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants