Find out where a class is used throughout a program.

A

Azureaus

Hi All,
I'm fairly new to Python so please forgive me If I sound confused or include anything a bit irrelevant. I've had some great responses from this group already though so thanks.

I have a source file that is laid out roughly like

class:
class methods
methods
init statement
class:
method

It doesn't seem to have a run method unlike other similar source files I have so it seems to be that this is being referenced from other files and is almost a 'utility file'.

To try and make this question as general as possible - is there a way of finding out / visualising where a particular class is called/used throughout a program? I need to find out the way in which these classes are being usedand their typical input (and where the output from these are going) so I can have a play around and really figure out how it works. Without a run method to call, or an idea of expected input/output it's difficult. Also without some sort of trace it's difficult.

I spoke to colleague and was told to look into dir() method in a Python shell which I will do this evening but if anyone has any suggestions that would be great. Even better if you think this is what I'm after a quick example/use case would be even better. Or maybe I'm looking at this the wrong way and you can point me towards some docs?
Thanks for your help.
 
D

dieter

Azureaus said:
...
is there a way of finding out / visualising where a particular class is called/used throughout a program?

I do not know a simple and reliable way.

When I face such a situation, I use standard operating system
utilities (e.g. "grep -r" under *nix) to search for occurrences of
the class name in the source tree. This often gives good
results when the class name has been well chosen.

Recently (within the last 2 months), I have seen the announcement
(on "...python.announce") of a tracing tool (I forgot the package's
name; maybe, it has been "CodeInspector"). When I have understood
the announcement correctly, then it traces concrete runs
and allows you to explore where objects (e.g. classes) have
been used *in these runs*.
 
I

Irmen de Jong

I do not know a simple and reliable way.

Not 100% reliable, but arguably easier than reverting to simple text search tools, is
using a Python IDE such as PyCharm. It does a remarkable job most of the time to show
you the usages and dependencies of various items in your program. It does this by
actually parsing the source and not simply performing a text based search.

Irmen
 
T

Terry Reedy

I do not know a simple and reliable way.

When I face such a situation, I use standard operating system
utilities (e.g. "grep -r" under *nix) to search for occurrences of
the class name in the source tree. This often gives good
results when the class name has been well chosen.

Idle has a built-in 'grep' called 'Find in Files' on the Edit menu. I
use it routinely. By default, it searches for the current text
selection, if there is one, in all files in the directory containing the
current file (and subdirectories). Idle's grep uses Python's re module,
so one does not have to learn another re dialect. So it works the same,
with Unicode text, on all systems, including Windows, which does not
come with grep. The (undocumented) limitation is that it searches each
line separately, so it cannot search for multiline patterns. (I would
not be surprised if grep does that same, as it also reports line numbers
and multiple hits in a file.)
 
R

Roy Smith

Azureaus said:
To try and make this question as general as possible - is there a way of
finding out / visualising where a particular class is called/used throughout
a program?

Sure.

$ cd <top of your source tree>
$ find . -name "*.py" | xargs grep MyClassName.
 
S

Steven D'Aprano

To try and make this question as general as possible - is there a way of
finding out / visualising where a particular class is called/used
throughout a program? I need to find out the way in which these classes
are being used and their typical input (and where the output from these
are going) so I can have a play around and really figure out how it
works. Without a run method to call, or an idea of expected input/output
it's difficult. Also without some sort of trace it's difficult.


Does the class not come with any documentation?

Start the Python interactive interpreter, and then enter these two lines:

import name_of_module
help(name_of_module.MyClassName)



where, of course, you replace name_of_module with the actual name of the
module, and MyClassName with the actual name of the class.


If that doesn't solve your problem, you can use the searching tools of
your choice to search for uses of the class. Open some source file in
your editor and search for "MyClassName" and see what comes up. Repeat
for any other source files you care about.

On a Linux or Mac system, you can use external tools. At the shell (not
the Python shell, the operating system's shell) enter:

cd directory/where/my/source/code/lives
grep *.py MyClassName


or similar. Windows may have a similar tool, but I don't know what it is.
 
D

Dave Angel

Hi All,
I'm fairly new to Python so please forgive me If I sound confused or include anything a bit irrelevant. I've had some great responses from this group already though so thanks.

I have a source file that is laid out roughly like

class:
class methods
methods
init statement

Perhaps you mean the __init__() method ? This method is invoked when an
object of this class is created, and generally has the job of
initializing the instance data. There may also (or instead) be a
__new__() method, which is the constructor.
class:
method

It doesn't seem to have a run method unlike other similar source files I have so it seems to be that this is being referenced from other files and is almost a 'utility file'.

A method is a function located inside a class. i think by "run method"
you are referring to top-level code. That is code that is executed when
the script/module is first loaded. You are right that if there is no
top-level code, then the file must be intended as a module (or library,
as it is sometimes called). However nearly every module will have some
top-level code, even if it's only an import statement or a class
instance assignment.
To try and make this question as general as possible - is there a way of finding out / visualising where a particular class is called/used throughout a program? I need to find out the way in which these classes are being used and their typical input (and where the output from these are going) so I can have a play around and really figure out how it works. Without a run method to call, or an idea of expected input/output it's difficult. Also without some sort of trace it's difficult.

As others have pointed out, an IDE can help greatly with this. But your
first line of attack should be the documentation included with the
file(s). If there's none, then perhaps it's throwaway code, and not
worth worrying about.
 
A

Azureaus

Hi All,

I'm fairly new to Python so please forgive me If I sound confused or include anything a bit irrelevant. I've had some great responses from this group already though so thanks.



I have a source file that is laid out roughly like



class:

class methods

methods

init statement

class:

method



It doesn't seem to have a run method unlike other similar source files I have so it seems to be that this is being referenced from other files and is almost a 'utility file'.



To try and make this question as general as possible - is there a way of finding out / visualising where a particular class is called/used throughout a program? I need to find out the way in which these classes are being used and their typical input (and where the output from these are going) so Ican have a play around and really figure out how it works. Without a run method to call, or an idea of expected input/output it's difficult. Also without some sort of trace it's difficult.



I spoke to colleague and was told to look into dir() method in a Python shell which I will do this evening but if anyone has any suggestions that would be great. Even better if you think this is what I'm after a quick example/use case would be even better. Or maybe I'm looking at this the wrong way and you can point me towards some docs?

Thanks for your help.

Thanks you all for your time and responses they have been a great help. Thepractical examples were especially helpful. I'm going to go through the suggestions and try them out to find which one is most suited but overall I feel like my understanding/knowledge of Python has increased which is reallythe whole point. I think I'm going to be a regular here :)
 
C

Chris Angelico

I think I'm going to be a regular here :)

Glad to have you around! Interesting and thoughtful questions, as much
as informative answers, are what make this list/group worth being in.
I originally came to ask a question, hung around to answer a few, and
stuck here because there's so much awesome and crazy wit...

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top