Saturday, January 09, 2010

Templates on Pydev (with Jython scripting)

One really nice feature that has just been added to Pydev (available in the nightly build) is the possibility of extending the variables that are available for templates through Jython scripting.

It opens lots of possibilities, as when it's resolved it has access to the python code being edited (and all the available modules and Pydev APIs).

Some default templates were added already taking advantage of that (see: as a reference -- the docstring explains how to create your own templates).

The image below shows the variables created in the defaults (already using Jython scripting -- note that when multiple superclasses are available, the ${superclass} variable enables the user to choose which one to use):

This enabled the print template to work as expected (giving only 'print ' in python 2.x and 'print()' in python 3.x, as it can 'know' which grammar you are using).

To finish, 2 other templates were creating taking advantage of the context:
super : super(MyClass, self).CurrentMethod()
super_raw : MySuperClass.CurrentMethod(self)

If anyone has other 'must have' template suggestions now that this is available, those are very welcome.

On a side note, the current nightly build is pretty stable (and should be the new released version unless there's some critical error lying there found in the next 2-3 days), so, it should be safe to get it to experiment with those.


Damien Lebrun said...

That's nice.

To add them we need to set in Pydev preferences a folder that holds our extensions files.

Would it be possible to use something like setuptools' entries point to let Pydev disover extension? Can Jython use setuptools?

Fabio Zadrozny said...

I must say that I have no idea if Jython can use setuptools (especially because Pydev is currently bound to version 2.1 of Jython -- the latest got too big to be incorporated).

Also, what would be the advantages over the current system? (which I believe is pretty simple to use)

Ciantic said...

I know this is not help desk, but would this be a way to improve auto-completion (where introspection can't do it) of PyDev?

There are some problems with Django and auto-completion that I would like to fix, even with hardcoded scripts or something ugly if not the neat introspection can handle. Especially when I need to look at the name of the arguments in method, e.g. initializer __init__ of model fields such as:

something = models.CharField([CTRL-Space]...)

I get nothing, nada. I always have to go to look at the the source of CharField.__init__() to know the init parameters, they are neatly listed in there but for some reason PyDev won't show them...

One of my ideas to start fixing auto-completion is to look at the @rtype or :rtype: value in docstring, and assume the output is in that type if possible. This does not help with Django, but with many other well documented modules it would.

Fabio Zadrozny said...

I don't really think that'd be the place for improving that kind of introspection...

Those ideas would be very well suited for the Pydev bugtracker (at sourceforge).




b1r3k said...

It would be great to have docstring template according to PEP 257 spec. like this:

def complex(real=0.0, imag=0.0):
"""Form a complex number.

Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)

if imag == 0.0 and real == 0.0: return complex_zero

Template should build keyword argument list along with default values

Fabio Zadrozny said...

Hi, please post feature requests in the sourceforge tracker (otherwise they end up lost in this blog). See:

Anonymous said...

Please, add ${package} too