Bottom Page

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
Quote
#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:
İmage
Quote
#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'
Ofnuts likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#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.
Quote
#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
Quote
#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
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to simplify square finding program? meknowsnothing 3 265 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  Help with finding correct topic in Python learning yahya01 1 255 Jun-06-2019, 05:01 PM
Last Post: buran
  Finding perfect numbers BillMcEnaney 6 387 Apr-04-2019, 04:46 AM
Last Post: BillMcEnaney
  Finding exact phrase in list graham23s 2 191 Mar-13-2019, 06:47 PM
Last Post: graham23s
  finding yesterday and tomorrrow without using date.time module apexman 10 494 Feb-25-2019, 05:33 AM
Last Post: samsonite
  Finding phrases of one semantic meaning VladislavMz 2 264 Dec-20-2018, 03:29 AM
Last Post: VladislavMz
  finding problems connecting python to sqlite Dennis 1 253 Dec-10-2018, 02:58 PM
Last Post: Larz60+
  Finding files knollfinder 2 295 Dec-09-2018, 08:03 AM
Last Post: Gribouillis
  finding angle between three points on a 2d graph qrani 4 1,963 Nov-20-2018, 06:10 AM
Last Post: Gribouillis
  Finding and indexing maximum value BlueBall 2 357 Oct-25-2018, 09:14 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)