Tools to help developers reading class hierarchy f

2019-03-16 03:29发布

问题:

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.

My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTouch.

One of the bottlenecks on my development speed is the pace at which I read existing code, particularly open source libraries which I use.

In Python/Django for example, when I encounter some new features introduced by django developers, I get curious and begin exploring the code base manually. For example, when class-based views were introduced from django 1.3 onwards, reference - https://docs.djangoproject.com/en/dev/topics/class-based-views/ - I will check out the example code shown:

from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = "about.html"

And try it out on one of my projects. More importantly, I am curious about what goes on behind the scenes, so I will dig into the source code -

# django/views/generic/__init__.py file

from django.views.generic.base import View, TemplateView, RedirectView
from django.views.generic.dates import (ArchiveIndexView, YearArchiveView, MonthArchiveView,
                                     WeekArchiveView, DayArchiveView, TodayArchiveView,
                                     DateDetailView)
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView


class GenericViewError(Exception):
    """A problem in a generic view."""
    pass

From here, I will trace it backwards to the django/views/generic/base.py file and find out exactly what TemplateView class does:-

class TemplateView(TemplateResponseMixin, View):
    """
    A view that renders a template.
    """
    def get_context_data(self, **kwargs):
        return {
            'params': kwargs
        }

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

And here's it shows that TemplateView class inherits from TemplateResponseMixin and View classes... and I continue digging further... and so on...

The problem is, this is an extremely inefficient and slow process (to "follow" class hierachies manually and opening up each file along the way).

So the question is - is there an easy way/UI tool (or other visual solution) that parses Python code in a particular project and visualize class hierarchies which I can then inspect easily by "clicking" on a specific class I am interested to read about?

Note that I am aware of IPython shell but that doesn't seem as user-friendly as a visual display tool.

For example, there's F-Script in the world of Objective-C/iOS/Mac programming, which not only provides a shell (much like python or IPython shell), but provides a visual way for developers to introspect class hierachies.

Reference screenshot:-

So is there a class-hierarchy visualization tool (for Python specifically, but even better if it's generic and can be used for different languages)??? What are your methods of getting up to speed efficiently when reading open source source code???

UPDATED

Per advice below, I tried out ctags and vim plugin taglist and I was able to use :TlistOpen to open up a side buffer in vim like this:-

This looks really cool as :TlistOpen now essentially shows me all the classes and functions that are available on my currently open buffer.

My problem now is that when I attempt to do Ctrl] while my cursor is on TemplateView, I get the following error:-

What am I doing wrong? Is it because my django source code is in a virtualenv? Or is there something specific I have to do to make ctags/taglist "aware" of the django source code?

回答1:

Tags are a very good start indeed. (There's too much stuff all over the place on it, so I'll just provide you with one extra keyword to search with: ctags.)

In Vim, it ends up (in the basic case) with Ctrl+] to go to a class/function definition and Ctrl+T to return.



回答2:

The IDLE editor included with Python has an effective class browser that efficiently navigates everything in a given module. I believe in would not be difficult to modify that tool to navigate a full class-hierarchy with some assistance from the inspect module and the pyclbr module.



回答3:

I've been using exuberant ctags with taglist for vim. Use ctrl] to jump to class definition in the current window, ctrlw] to jump to the definition in a split window.

You can install exuberant ctags via homebrew:

brew install ctags

Be sure to use the one installed at /usr/local/bin by homebrew, not the old ctags in /usr/bin.

It is also helpful to put --python-kinds=-vi in ~/.ctags to skip indexing variables and imports for Python files.

Another alternative would be to use a variant of cscope or pyscope though you must have your vim compiled with cscope option enabled.