1

Say we get a massive and very messy script that contains many lines that aren't in use.

How can we find out which lines are in use?

If the script is very short we can go with debugger step by step and see what is used but this is impractical in most cases.

Is there any tool or trick to get the lines that are not in use?

IDEs mark variables not in use, but what about function definitions that are not executed at all?

Basically how to get the lines that weren't executed in a python script specific run?

Maybe something to plot a list of numbers of lines not in use

user10364768
  • 135
  • 5
  • You add test coverage for the needed functionality, and enable coverage reporting to see what lines are missed/unused – wim Aug 02 '21 at 20:16
  • Check out Vulture to [Find dead code in Python](https://github.com/jendrikseipp/vulture) – DarrylG Aug 02 '21 at 20:17
  • Does this answer your question? [Finding dead code in large python project](https://stackoverflow.com/questions/9524873/finding-dead-code-in-large-python-project) – mkrieger1 Aug 02 '21 at 20:28
  • You can run your program under coverage.py (you don't need tests for this). It will show you which lines were hit in that run (https://coverage.readthedocs.io/en/coverage-5.5/index.html). It's mainly targeted for generating test coverage, but the tool is more general than that. – thebjorn Aug 02 '21 at 20:35
  • @mkrieger1 It is related but if I'm not mistaken seems like it is a much more difficult question, they ask for a static tool, I am looking for a dynamic one. The IDE has the information since it has a debugger, so I was hoping to have some kind of a function in IDE to just plot the lines it executed in a specific execution. Not sure if there is but hoping there is. – user10364768 Aug 02 '21 at 20:35
  • 1
    I suppose the trace module can be used directly as well (https://docs.python.org/3/library/trace.html) but I don't know anyone that has used it that wasn't writing something like coverage.py ;-) – thebjorn Aug 02 '21 at 20:40
  • 1
    @thebjorn It is also used by debuggers e.g. for breakpoints (see [`sys.settrace`](https://docs.python.org/3/library/sys.html#sys.settrace) for the user's hook) – wim Aug 02 '21 at 21:55

0 Answers0