pyutils / line_profiler

Line-by-line profiling for Python
Other
2.68k stars 119 forks source link

Fix text alignment for wide columns in show_func #209

Closed Erotemic closed 1 year ago

Erotemic commented 1 year ago

I added some logic in show_func to widen columns if necessary. I also added an associated doctest that demonstrates the issue.

Before when hits or time was large enough it would overflow the column and make the lines have bad indentation:

Total time: 1.32e+11 s
File: /home/joncrall/code/line_profiler/line_profiler/line_profiler.py
Function: show_text at line 306

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   306                                           def show_text(stats, unit, output_unit=None, stream=None, stripzeros=False):
   307                                               """ Show text for the given timings.
   308                                               """
   309 100000000.0 2000000000.0     20.0      1.5      if stream is None:
   310 200000000.0 4000000000.0     20.0      3.0          stream = sys.stdout
   311                                           
   312 300000000.0 6000000000.0     20.0      4.5      if output_unit is not None:
   313 400000000.0 8000000000.0     20.0      6.1          stream.write('Timer unit: %g s\n\n' % output_unit)
   314                                               else:
   315 500000000.0 10000000000.0     20.0      7.6          stream.write('Timer unit: %g s\n\n' % unit)
   316                                           
   317 1100000000.0 22000000000.0     20.0     16.7      for (fn, lineno, name), timings in sorted(stats.items()):
   318 1000000000.0 20000000000.0     20.0     15.2          show_func(fn, lineno, name, stats[fn, lineno, name], unit,
   319 800000000.0 16000000000.0     20.0     12.1                    output_unit=output_unit, stream=stream,
   320 900000000.0 18000000000.0     20.0     13.6                    stripzeros=stripzeros)

With the fix the column width increases to accomidate the largest number in the column:

Total time: 1.32e+11 s
File: /home/joncrall/code/line_profiler/line_profiler/line_profiler.py
Function: show_text at line 327

Line #              Hits          Time  Per Hit   % Time  Line Contents
=======================================================================
   327                                                    def show_text(stats, unit, output_unit=None, stream=None, stripzeros=False):
   328                                                        """ Show text for the given timings.
   329                                                        """
   330  10000000000000.0  2000000000.0      0.0      1.5      if stream is None:
   331  20000000000000.0  4000000000.0      0.0      3.0          stream = sys.stdout
   332                                                    
   333  30000000000000.0  6000000000.0      0.0      4.5      if output_unit is not None:
   334  40000000000000.0  8000000000.0      0.0      6.1          stream.write('Timer unit: %g s\n\n' % output_unit)
   335                                                        else:
   336  50000000000000.0 10000000000.0      0.0      7.6          stream.write('Timer unit: %g s\n\n' % unit)
   337                                                    
   338 110000000000000.0 22000000000.0      0.0     16.7      for (fn, lineno, name), timings in sorted(stats.items()):
   339 100000000000000.0 20000000000.0      0.0     15.2          show_func(fn, lineno, name, stats[fn, lineno, name], unit,
   340  80000000000000.0 16000000000.0      0.0     12.1                    output_unit=output_unit, stream=stream,
   341  90000000000000.0 18000000000.0      0.0     13.6                    stripzeros=stripzeros)
codecov[bot] commented 1 year ago

Codecov Report

Merging #209 (8695883) into main (6ffb10d) will increase coverage by 2.36%. The diff coverage is 100.00%.

Additional details and impacted files [![Impacted file tree graph](https://codecov.io/gh/pyutils/line_profiler/pull/209/graphs/tree.svg?width=650&height=150&src=pr&token=xIK8nFU3K5&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) ```diff @@ Coverage Diff @@ ## main #209 +/- ## ========================================== + Coverage 49.24% 51.61% +2.36% ========================================== Files 4 4 Lines 266 279 +13 Branches 39 40 +1 ========================================== + Hits 131 144 +13 Misses 121 121 Partials 14 14 ``` | [Impacted Files](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [line\_profiler/line\_profiler.py](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bGluZV9wcm9maWxlci9saW5lX3Byb2ZpbGVyLnB5) | `53.43% <100.00%> (+3.16%)` | :arrow_up: | ------ [Continue to review full report at Codecov](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [6ffb10d...8695883](https://codecov.io/gh/pyutils/line_profiler/pull/209?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).