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

2024/10/1 23:37:29

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?

Answer

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 :)

https://en.xdnf.cn/q/70909.html

Related Q&A

How to serialize hierarchical relationship in Django REST

I have a Django model that is hierarchical using django-mptt, which looks like:class UOMCategory(MPTTModel, BaseModel):"""This represents categories of different unit of measurements.&qu…

Django: Loading another template on click of a button

Ive been working on a django project for a few weeks now, just playing around so that I can get the hang of it. I am a little bit confused. I have a template now called "home.html". I was wo…

Given two python lists of same length. How to return the best matches of similar values?

Given are two python lists with strings in them (names of persons):list_1 = [J. Payne, George Bush, Billy Idol, M Stuart, Luc van den Bergen] list_2 = [John Payne, George W. Bush, Billy Idol, M. Stuart…

Extracting Javascript gettext messages using Babel CLI extractor

It is stated here that Babel can extract gettext messages for Python and Javascript files.Babel comes with a few builtin extractors: python (which extractsmessages from Python source files), javascript…

Getting TTFB (time till first byte) for an HTTP Request

Here is a python script that loads a url and captures response time:import urllib2 import timeopener = urllib2.build_opener() request = urllib2.Request(http://example.com)start = time.time() resp = ope…

accessing kubernetes python api through a pod

so I need to connect to the python kubernetes client through a pod. Ive been trying to use config.load_incluster_config(), basically following the example from here. However its throwing these errors. …

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …

Parsing RSS with Elementtree in Python

How do you search for namespace-specific tags in XML using Elementtree in Python?I have an XML/RSS document like:<?xml version="1.0" encoding="UTF-8"?> <rss version=&quo…

String module object has no attribute join

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function in…

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…