问题
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?
回答1:
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 :)
回答2:
I'm not 100% sure how to do it all at once, but it can be accomplished in two steps. You need to customize your def templates. Go to Window-> Pydev->Editor->Templates. Scroll down to def, defc and defp and edit them. When you create a new method start typing def and use control space twice to get to your templates. Select the appropriate template. Change the name from method to what you wish and this will also fill in the NotImplemented exception. Add your arguments. Step two, ebfore leaving the definition line press ctrl 1, and that will fill in your doc string.
I changed my defc to
def ${method}(self):${cursor}
raise NotImplemented('${method}')
来源:https://stackoverflow.com/questions/20599459/pre-fill-new-functions-in-eclipse-and-pydev-with-docstring-and-not-implemented