Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-fill new functions in Eclipse and Pydev with docstring and "Not Implemented" exception

I am editing my Python source code with Eclipse and Pydev.

I want to document all of my functions and raise a "Not Implemented" exception whenever a function have not yet been implemented.

For instance when I type:

def foo(bar1,bar2):

On enter, I would like it to autocomplete to:

def foo(bar1,bar2):
'''
function foo
@param bar1:
@type: 
@param bar2:
@type
'''
raise NotImplementedError("")

Is there already an option in Pydev or Eclipse to do it? If not, is there a separate Python module or script that would do it properly?

like image 858
chiffa Avatar asked Dec 15 '13 20:12

chiffa


1 Answers

Currently, the documentation can be generated already.

I.e.: in a 'def' line, pressing Ctrl+1 will show an option "Generated Docstring" (the format of those docstring may be defined in preferences > pydev > editor > code style > docstrings).

As for the raise NotImplementedError(""), there's currently no way to add that automatically.

Personally, what I use is an 'abstract' decorator such as:

def abstract(func):

def wrapper(self, *args, **kwargs):
    msg = 'Method %r not implemented in class %r.' % (func.__name__, self.__class__)
    raise NotImplementedError(msg)

wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper

And then to use:

@abstract
def my_func(xxx, yyy):
    ...

That way if someone calls your code, the message looks better :)

like image 129
Fabio Zadrozny Avatar answered Oct 18 '22 12:10

Fabio Zadrozny