Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a vague memory of this question being asked before somewhere in cyberspace but I can't find it again.
Say I have a file with a bunch of functions (and or classes) defined, but I know that some of them are not used anywhere in my project. Is there a tool to scan through my project to see which functions aren't being used?
I know I can do this individually for each function, using Pycharm for example, but I don't know of a way to do this for all functions in a file. It feels like there must be a tool for this, but I don't know of one.
EDIT
I know there are edge cases of code, as pointed out by #deceze, that make this sort of usage checking impossible in general. But I'd be happy with a tool that works 99% of the time. The rest can be caught by unit tests for example, and manually handled.
I don't know about any tool that explores your whole project but you could easily make one in few lines of code by using the jedi library.
There is jedi.Script.usages which is exactly what you want here, the tool would do something like:
Create a jedi environment using your python interpreter of choice (this way will have information about sys.path
Walk over the project files you want to analize and extract the functions you want to check usages from (glob, os.walk, custom cli, ...)
On each file, you just need to extract the functions you want to analize (in your parser you store the locations as line/columns pairs)
Create a jedi script with the previous location and call usages and then store the results in a dictionary
Profit
PS: the most "tricky" step would be the one that extract functions from your python files but I guess this could also be done with jedi instead using another builtin python parsers, here's an untested piece of code:
for definition in jedi.names(source, all_scopes=True, definitions=True, references=True):
if definition.parent().type == "function": # The name is located in a function ...
ass = definition.goto_assignments()
if len(ass) > 0 and ass[0].parent().type == "function": # ... and is assigned to in a function
print("Found a local variable:", definition.name)
Extracted from this github issue
Coverage.py - used it recently to show my test coverage. But it can be used in other contexts as well
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I am searching for days to get the class hierarchy such as super and subclasses of any class (along with file path and line number, method over-rides references) something like code analysis of the project. The same functionality as PyCharm editor does. It will be a great help if someone can guide me on how to achieve this
If you want to run a sort of code-analysis on Python, firstly you should remember Python is a dynamically-typed language, which means types are inferred.
In Python, everything is an object. Which means all your class definitions are also objects. You can use the ast module to parse any source file (.py) into an Abstract Syntax Tree. In this AST, you'll be able to see relationships between names. However, bear in mind that the ast module is intraprocedural , which means that if you want to build a complex interprocedural code analysis, you'll need some heavy working to do, specially when it comes to solving aliases problems, among countless other things. Then again, you'll never know the types of each name in the ast.
If you delve into this quest, you should extend the ast module to implement your own NodeVisitor, a class that will execute some routine in every node of your AST. Check more info here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry if the title is confusing.
What I am trying to say is this:
I have worked with Python before, but I'm by no means an expert. So far everything I have done has just been 'somefile.py' with lots of methods and code in it, but it doesn't really have any organizational structure. In Java (which I am more familiar with than Python), there are usually different classes that each have methods and are called from each other. How do you make a file full of code organized and structured when working on a large project? Break them up into files by class?
Hopefully this is clearer. Let me know if this needs clarification.
In Python, the file unit is called module. Modules are organized in packages.
You usually put your classes each in a module and also use modules to group related code that doesn't belong to any class. Related modules are grouped in packages (physically represented by directories) which effectively create namespaces.
Then you use the import command to import the desired pieces of the code into other modules.
You can read about modules, packages and import in the Python documentation here.
Logically, it isn't much different than Java or other languages.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Is there a recommended reference manual for python that's better than the official docs? I'm an experienced programmer (PHP, C#, javascript, and some C most recently), and I find the python manual pretty lacking compared the PHP manual and MSDN. In particular, the official docs never seem to tell me what errors can happen if I pass in something invalid, and there's apparently not a way to navigate within a module.
Take the os module for example. There's no list of constants or methods I can call on that page, so I have to Ctrl+f for "stat(" until I find it. Then, once I do find it, it doesn't tell me what to expect if I call stat with a directory that doesn't exist, so I just have to try it in a terminal and see what happens.
This seems wildly inefficient… how do python programmers deal with this?
In practice, you either make a quick test program to check the behavior, or read the source code. Much of the Python standard library code is fairly clearly written and in fact rather self-documenting, so it's standard practice to refer to it when you need to know the nitty-gritty details of how something works.
One exception: with low-level system functions such as many of those in the os module, the functions map directly on to their C namesakes for the underlying platform. So if you need to know about the behavior of Python's stat, you look up the reference documentation for your platform's native C stat call. In these cases the Python library docs often only explain the basic purpose of the function and how it differs from its C equivalent, if at all.
I don't think I ever had the same feeling towards the python docs as you do but I did some times need to go out of my way some times for a better understand of how a part of python works. Though that is how most language are. Python is a quick and easy language to learn which requires less time on docs and more time programming. Also python's user base isn't as large as PHP. PHP has people constantly giving examples and details on certain functions daily.
Another great thing about python is its interactive shell to test things out. Just my idea of programming you learn more by doing then seeing. So if you're ever interested in testing something you don't need to drag through compiling, just write a quick script in the interpreter. the interpreter also has reference tools. Example dir(<identifier>) and help(<identifier>) for module, clasa or function needs.
Any way enough defending my favorite language, pyDoc is a little useful tool to help you get what you need from python.
pydoc is a tool that comes with Python that can be used for viewing
and generating Python documentation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm writing end-to-end tests for my tool, which is written in Python. The tool reads a file as input. I want to test its exit code, and its output.
This is a fairly common idiom, and I've seen it done in several ways. In the PHP project, each test is a file, and has lines like: INPUT:, EXPECTED:, EXPECTED_REGEX:, etc. In my own phc project, each file is a normal source file, but with a comment added to the top, which includes keywords like EXPECTED. I think I had copied that off gcc which uses a much more complex tool written in tcl.
Are there frameworks, libraries, etc, that do this in Python? It should:
read the source file
parse special keywords (or similar) corresponding to expected output, exit code, words/regexes it expects to find or not find,
check that the output is correct.
While it doesn't seem hard in theory, I recall lots of edge-cases (esp involving escaping) when implementing this before, and would rather not reinvent the wheel.
The robot framework might be helpful. It is a keyword driven functional testing tool implemented in python and can be extended with pythion or java.
see: http://robotframework.googlecode.com/svn/tags/robotframework-2.5.4/doc/userguide/RobotFrameworkUserGuide.html
There are a number of built in libraries that you might be able to apply to solve your problem, including a OperatingSystem library for working with files etc. and a Strings library for working with strings:
http://robotframework.googlecode.com/svn/tags/robotframework-2.5.4/doc/userguide/RobotFrameworkUserGuide.html#standard-libraries
There is also a http://pythonpaste.org/scripttest/ library by Ian Bicking.
Since the implementation of file io is system dependent, why not mock out the file reading and writing using StringIO:
http://docs.python.org/library/stringio.html
and then test the bulk of the logic (reading from a file, doing some stuff, writing to a file) in python?
Then, perhaps you could have one end to end test for basic sanity by having a separate python file call out to the script using the commands module or something similar where you are calling out to it as another process:
http://docs.python.org/library/commands.html
Using that you could get both the output, and the status.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I'm digging into a huge legacy Python class that has a lot of methods. I eventually break complex ones into smaller pieces so the amount of methods increases even more. I wonder if there is a tool that can scan the Python code and build some kind of dependency diagram for its methods.
I define method x() to be a dependency of method y() if x() is called at least once in y().
I could use such a tool to isolate (if possible) subsets of class methods that have no external dependencies (all their dependencies are methods from the same subset).
I'm planning to move some functionality into other classes and I think that such an approach would help me to decide which parts to extract from the initial huge class.
Edit: I would really like a command-line tool.
Have you looked at Snakefood yet? It looks like it's exactly what you're looking for.
Have you tried pydev? It's a python extension for eclipse. I believe it allows you to use the "call hierarchy" feature of Eclipse to view a call graph for a given method. It's not quite what you want but maybe it's enough to get started.
Pycallgraph should do what you are looking for.
Just started using pydeps and so far it works well.
i was confuse in this question too,i have found a search helper help me to find the call hierarchy in another way. not very good but better than donot have. sorry about my english.
ps.IDE eclipse+pydev