Preserve Signature in Decorator python 2

2024/10/14 23:18:45

I am writing a decorator which will catch TypeError for incorrect number of arguments in a function call and will print a customised message. The code is here:

import inspectdef inspect_signature(f):def decorate(*args, **kwargs):try:f(*args, **kwargs)except TypeError as e:print('Failed to call "{}" with signature {}. Provided args={} and kwargs={}.'.format(f.__name__, inspect.getargspec(f).args, args, kwargs))return freturn decorate@inspect_signature
def func(foo, bar):pass
func('a')
func('a', 'b')

I get the following output:

Failed to call "func" with signature ['foo', 'bar']. Provided args=('a',) and kwargs={}.
Called successfully with foo=a, bar=b
ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None)

The function signature is empty. Please suggest me a solution how can I retain it?

PS: I am using python2 and cannot switch to python3.

Answer

You missed this here. func(*foo, **bar)

In your case func('a') was not working as you gave fixed arg for it.

You need to pass a variable number of arguments to your function

@inspect_signature
def func(*foo, **bar):pass
https://en.xdnf.cn/q/117895.html

Related Q&A

Gimp: start script without image

Well, Im trying to write a python plug-in for Gimp, but it wont start without first loading an image... What can I do about that?

Pywinauto: how the `findbestmatch` module works?

Im trying to understand how the findbestmatch module works. Here is an example.from pywinauto.application import Application from pywinauto.findbestmatch import find_best_match ditto=Application().conn…

How to get the surface from a rect/line

I am trying to find the point where a line collides with a brick in the arkanoid that i am making. The most logical way i found is getting the mask from the line and use collidemask as it returns the p…

Python readin .txt and put in an array with numpy

i want to create an array with numpy. The base is a .txt file which is given in the following form:i tried it with loadtxt:data = np.loadtxt("myfile.txt",delimiter=\n,skiprows = 1)The first r…

Running a PyQt4 script without a display

I would like to run a Python script that normally opens a Qt window remotely over a connection with no X11 forwarding. Is there any way to create some kind of virtual display that the window drawing ca…

Scrapy : Program organization when interacting with secondary website

Im working with Scrapy 1.1 and I have a project where I have spider 1 scrape site A (where I aquire 90% of the information to fill my items). However depending on the results of the Site A scrape, I ma…

How do I use openpyxl and still maintain OOP structure?

I am using python to do some simulations and using openpyxl to generate the reports. Now the simulation is results are to be divided into several sheets of an excel file. By the principles of OOP my st…

Leaving rows with a giving value in column

UPDATED: In my dataset I have 3 columns (x,y) and VALUE. Its looking like this(sorted already):df1: x , y ,value 1 , 1 , 12 2 , 2 , 12 4 , 3 , 12 1 , 1 , 11 2 , 2 , 11 4 , 3 , 11 1 , 1 , 33 2 , 2 , 33 …

Python Circular dependencies, unable to link variable to other file

I am working on a program that allows me to directly edit a word document through a tkinter application. I am trying to link the tkinter input from my gui file to my main file so that I can execute my …

how to use xlrd module with python for abaqus

Im working on a script for abaqus where I have to import data from an excel file to put them into my script. I already downloaded the xlrd module and it work well on python interpreter (IDLE), but when…