Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53638 > unrolled thread
| Started by | Azureaus <lo0446@my.bristol.ac.uk> |
|---|---|
| First post | 2013-09-04 09:32 -0700 |
| Last post | 2013-09-05 22:53 +1000 |
| Articles | 10 — 9 participants |
Back to article view | Back to comp.lang.python
Find out where a class is used throughout a program. Azureaus <lo0446@my.bristol.ac.uk> - 2013-09-04 09:32 -0700
Re: Find out where a class is used throughout a program. dieter <dieter@handshake.de> - 2013-09-04 22:08 +0200
Re: Find out where a class is used throughout a program. Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-09-04 22:40 +0200
Re: Find out where a class is used throughout a program. Terry Reedy <tjreedy@udel.edu> - 2013-09-04 17:36 -0400
Re: Find out where a class is used throughout a program. alex23 <wuwei23@gmail.com> - 2013-09-05 10:11 +1000
Re: Find out where a class is used throughout a program. Roy Smith <roy@panix.com> - 2013-09-04 21:48 -0400
Re: Find out where a class is used throughout a program. Steven D'Aprano <steve@pearwood.info> - 2013-09-05 02:16 +0000
Re: Find out where a class is used throughout a program. Dave Angel <davea@davea.name> - 2013-09-05 09:08 +0000
Re: Find out where a class is used throughout a program. Azureaus <lo0446@my.bristol.ac.uk> - 2013-09-05 05:42 -0700
Re: Find out where a class is used throughout a program. Chris Angelico <rosuav@gmail.com> - 2013-09-05 22:53 +1000
| From | Azureaus <lo0446@my.bristol.ac.uk> |
|---|---|
| Date | 2013-09-04 09:32 -0700 |
| Subject | Find out where a class is used throughout a program. |
| Message-ID | <9d290db6-b9cb-41af-8107-e7f27d2da6d0@googlegroups.com> |
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 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.
[toc] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2013-09-04 22:08 +0200 |
| Message-ID | <mailman.58.1378325308.5461.python-list@python.org> |
| In reply to | #53638 |
Azureaus <lo0446@my.bristol.ac.uk> writes: > ... > 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*.
[toc] | [prev] | [next] | [standalone]
| From | Irmen de Jong <irmen.NOSPAM@xs4all.nl> |
|---|---|
| Date | 2013-09-04 22:40 +0200 |
| Message-ID | <52279acf$0$16007$e4fe514c@news.xs4all.nl> |
| In reply to | #53649 |
On 4-9-2013 22:08, dieter wrote: > Azureaus <lo0446@my.bristol.ac.uk> writes: >> ... >> 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. 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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-09-04 17:36 -0400 |
| Message-ID | <mailman.64.1378330621.5461.python-list@python.org> |
| In reply to | #53638 |
On 9/4/2013 4:08 PM, dieter wrote: > Azureaus <lo0446@my.bristol.ac.uk> writes: >> ... >> 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. 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.) > 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*. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-09-05 10:11 +1000 |
| Message-ID | <l08i7m$qno$1@dont-email.me> |
| In reply to | #53638 |
On 5/09/2013 2:32 AM, Azureaus wrote: > 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? One option is to produce call graphs of the running code: http://pycallgraph.slowchop.com/
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-09-04 21:48 -0400 |
| Message-ID | <roy-951FCF.21480004092013@news.panix.com> |
| In reply to | #53638 |
In article <9d290db6-b9cb-41af-8107-e7f27d2da6d0@googlegroups.com>, Azureaus <lo0446@my.bristol.ac.uk> wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2013-09-05 02:16 +0000 |
| Message-ID | <5227e966$0$2743$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #53638 |
On Wed, 04 Sep 2013 09:32:28 -0700, Azureaus wrote: > 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. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-05 09:08 +0000 |
| Message-ID | <mailman.83.1378372134.5461.python-list@python.org> |
| In reply to | #53638 |
On 4/9/2013 12:32, Azureaus wrote: > 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. > > 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. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Azureaus <lo0446@my.bristol.ac.uk> |
|---|---|
| Date | 2013-09-05 05:42 -0700 |
| Message-ID | <6974f400-49b0-4067-9bd3-03b642ab5a82@googlegroups.com> |
| In reply to | #53638 |
On Wednesday, 4 September 2013 17:32:28 UTC+1, Azureaus wrote: > 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 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. Thanks you all for your time and responses they have been a great help. The practical 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 really the whole point. I think I'm going to be a regular here :)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-09-05 22:53 +1000 |
| Message-ID | <mailman.89.1378385643.5461.python-list@python.org> |
| In reply to | #53699 |
On Thu, Sep 5, 2013 at 10:42 PM, Azureaus <lo0446@my.bristol.ac.uk> wrote: > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web