views:

182

answers:

2

Hi All,

Warning, this is a sheer-laziness query! As my project develops, so also do the number of functions within a class. I also have a number of classes per .py file. So what I would like to do is re-sort them to that the function names are organised [sorry, UK here, I've already compromised hugely with the 'z' in Alphabetizing ;-)] alphabetically.

Eg currently:

class myClass():
    stuff
    def __init__():
        do stuff
    def b():
        also do stuff
    def c():
        do other stuff
    def a():
        do even more stuff

..and for ease of lookup, I'd like to rearrange to:

class myClass():
    stuff
    def __init__():
        do stuff
    def a():
        do even more stuff         
    def b():
        also do stuff
    def c():
        do other stuff

purely for cosmetic reasons, as it makes searching for the relevant functions more intuitive. I'd obviously want to keep the init() etc at the top of the class.

Now, I can (but haven't yet) do this with a python script that reads in the .py the class(es) reside in as a text file, and do a tonne of string manipulation. But I wonder is there a better way to do this? Perhaps there's a tool I can use somewhere? I've had a good hunt, but I can't really find anything I could use.

I'm using emacs, so perhaps there's a big 5-minute key combination sequence that will do the magic (eg), but I can't see anything in python-mode!

Finally, I would like some way to be able to visualise and publicise the class structure/docstrings.

When I say visualise structure, I mean, for example, where myClass.a() calls myOtherClass.g(), that would be a link, so I'd be able to see what calls what, either on a class-by-class basis (ie, what functions/classes does myClass use?) or overall (here are a bunch of files, how do they 'connect'?)

When I say publicise the class, I guess I mean something like the API documentation you see in eg1,eg2.

Thanks all for your time!

+9  A: 

I don't have a solution to your question, but I have a very strong opinion: if the number of methods in your classes becomes so large you have trouble finding them, you should consider reducing the number, perhaps by splitting the class into smaller ones.

The same principles of cohesion that apply to functions and methods apply to classes as well.

Lars Wirzenius
+3  A: 

I am not sure sorting methods by name is such a great idea :

That's what you see when browsing OLE objects and it is really hard to understand anything as related methods are all over the place because they dont start by the same letter.

I'd rather see "open" just before "close", for example, without all other unrelated methods in between...

Your problem is to list / view easily the functions in your class ?

Here is a simple trick I use in emacs :

M-x occur

regex: ^ *def

Give it a try ... (the same works for some other languages with a different regex (eg : "^sub ")

siukurnin