Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding dead code
#1
I'm writing a fairly large a script (currently around a thousand lines and growing(*)) and due to refactoring it could contain some dead code (I already found some...). Short of searching each function by hand, is there a tool somewhere to build the calling tree? I'm not using any IDE at the moment, just an editor (Kate).

(*) no, I can't split it in modules :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#2
See: http://pycallgraph.slowchop.com/en/master/
It might do what you want.
Also, there's Doxygen which is a document generator from source,
this mignt not do the trick if it follows the dead code here's the link: http://www.stack.nl/~dimitri/doxygen/dow...l#gitrepos

Typical output:
[Image: regexp_ungrouped.png]
Reply
#3
Try https://pypi.python.org/pypi/vulture

There are some Python IDE's which will mark unused variables or code which is not used. Sublime Text will do it, I think. May be Atom too.

Edit:
I've wrote this simple code:

#!/usr/bin/env python3
import sys

a = 2
b = 3
c = 4


def print_hw():
    print("hello, world")


def add_nums(x, y):
    return x + y


def main():
    print(add_nums(a, b))
    print(dir(__name__))

if __name__ == '__main__':
    sys.exit(main())
So, Sublime, Atom and Ninja IDE, no one of these has showed the unused code...

I've tried pychecker, pyflakes and vulture. Only the last one has printed out some result.

victor@jerry:~$ vulture -v dead_code_test.py | grep 'Unused'
dead_code_test.py:6: Unused variable 'c'
dead_code_test.py:9: Unused function 'print_hw'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Great suggestions above, but you can also try using a code coverage tool - https://coverage.readthedocs.io/en/coverage-4.2/
From my Googling though, Vulture seems like the best way to go.
Reply
#5
Vulture definitely fits the bill, even if it flagged wrongly an unused attribute (it assumes that the data is used only by my code...). Small price to pay for the rest... and it's fast.

Will look at larz60+'s graph tools later, when I get utterly lost(*) :)

(*) The script is a rat's nest of closures being called by dynamically selected functions.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#6
Is there a python lint?

I just searched and found pylint https://github.com/PyCQA/pylint
Don't know anything abut it
some lints don't do code coverage
Reply
#7
Yes. It's called pylint if I remember. I didn't get it at allĀ  Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 1,159 May-14-2022, 05:34 PM
Last Post: saoko
  code for finding a cables crossed the bondary of the city spider_pytthon 8 3,471 Dec-24-2019, 07:23 AM
Last Post: spider_pytthon
  I need help finding an error in this code I have Tacnoblode 1 2,654 Jul-18-2018, 05:51 AM
Last Post: buran
  code wanted: finding absent characters Skaperen 2 2,892 Mar-26-2018, 03:12 AM
Last Post: Skaperen
  Trouble finding Python code Larz60+ 5 4,291 Jun-02-2017, 01:30 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020