Say I have the following:
def foo(arg1, arg2, arg3 = default, *args, *kwargs*):
"""This function is really cool."""
return
How can I define a new function functionprinter(f) so that functionprinter(f) would print
foo(arg1, arg2, arg3 = default, *args, *kwargs*)
This function is really cool.
or something of that nature? I already know foo.__name__ and foo.__doc__ and have seen the inspect module, specifically here: Getting method parameter names in python but can't seem to string together everything, in particular having the default arguments print properly. I am using Python 3.4.1.
Yup! You can with the inspect module:
import inspect
def foo(arg1, arg2, arg3=None , *args, **kwargs):
"""This function is really cool."""
return
def functionprinter(f):
print("{}{}".format(f.__name__, inspect.signature(f)))
print(inspect.getdoc(f))
functionprinter(foo)
Prints:
foo(arg1, arg2, arg3=None, *args, **kwargs)
This function is really cool.
Note that I changed your default argument to None just for this demonstration because I didn't have the variable default defined.
You can use inspect.signature (to represents the call signature of a callable object and its return annotation.) and inspect.getdoc :
>>> print(inspect.signature(foo))
(arg1, arg2, arg3=3, *args, **kwargs)
>>> inspect.getdoc(foo)
'This function is really cool.'
>>> print ('\n'.join((foo.__name__+str(inspect.signature(foo)),inspect.getdoc(foo))))
foo(arg1, arg2, arg3='', *args, **kwargs)
This function is really cool.
Related
I know I should specify argtypes for my C/C++ functions since some of my calls would otherwise result in stack corruption.
myCfunc.argtypes = [ct.c_void_p, ct.POINTER(ct.c_void_p)]
myCfunc.errcheck = my_error_check
In fact, I would like to verify that I did not forget to specify function prototypes (argtypes/errcheck) for any of my about 100 function calls...
Right now I just grep through my Python files and visually compare against my file containing the prototype definitions.
Is there a better way to verify that I have defined argtypes/errcheck for all my calls?
The mention of namespaces by #eryksun made me wrap the dll in a class that only exposes the explicitly annotated functions. As long as the dll doesn't have the function names "annotate" or "_error_check" (which my didn't), the following approach seems to work for me:
import ctypes as ct
class MyWinDll:
def __init__(self, dll_filename):
self._dll = ct.WinDLL(dll_filename)
# Specify function prototypes using the annotate function
self.annotate(self._dll.myCfunc, [ct.POINTER(ct.c_void_p)], self._error_check)
self.annotate(self._dll.myCfunc2, [ct.c_void_p], self._error_check)
...
def annotate(self, function, argtypes, errcheck):
# note that "annotate" may not be used as a function name in the dll...
function.argtypes = argtypes
function.errcheck = errcheck
setattr(self, function.__name__, function)
def _error_check(self, result, func, arguments):
if result != 0:
raise Exception
if __name__ == '__main__':
dll = MyWinDll('myWinDll.dll')
handle = ct.c_void_p(None)
# Now call the dll functions using the wrapper object
dll.myCfunc(ct.byref(handle))
dll.myCfunc2(handle)
Update: Comments by #eryksun made me try to improve the code by giving the user control of the WinDLL constructor and attempting to reduce repeated code:
import ctypes as ct
DEFAULT = object()
def annotate(dll_object, function_name, argtypes, restype=DEFAULT, errcheck=DEFAULT):
function = getattr(dll_object._dll, function_name)
function.argtypes = argtypes
# restype and errcheck is optional in the function_prototypes list
if restype is DEFAULT:
restype = dll_object.default_restype
function.restype = restype
if errcheck is DEFAULT:
errcheck = dll_object.default_errcheck
function.errcheck = errcheck
setattr(dll_object, function_name, function)
class MyDll:
def __init__(self, ct_dll, **function_prototypes):
self._dll = ct_dll
for name, prototype in function_prototypes.items():
annotate(self, name, *prototype)
class OneDll(MyDll):
def __init__(self, ct_dll):
# set default values for function_prototypes
self.default_restype = ct.c_int
self.default_errcheck = self._error_check
function_prototypes = {
'myCfunc': [[ct.POINTER(ct.c_void_p)]],
'myCfunc2': [[ct.c_void_p]],
# ...
'myCgetErrTxt': [[ct.c_int, ct.c_char_p, ct.c_size_t], DEFAULT, None]
}
super().__init__(ct_dll, **function_prototypes)
# My error check function actually calls the dll, so I keep it here...
def _error_check(self, result, func, arguments):
msg = ct.create_string_buffer(255)
if result != 0:
raise Exception(self.myCgetErrTxt(result, msg, ct.sizeof(msg)))
if __name__ == '__main__':
ct_dll = ct.WinDLL('myWinDll.dll')
dll = OneDll(ct_dll)
handle = ct.c_void_p(None)
dll.myCfunc(ct.byref(handle))
dll.myCfunc2(handle)
(I don't know if original code should be deleted, I kept it for reference.)
Here's a dummy class that can replace the DLL object's function call with a simple check to see the attributes have been defined:
class DummyFuncPtr(object):
restype = False
argtypes = False
errcheck = False
def __call__(self, *args, **kwargs):
assert self.restype
assert self.argtypes
assert self.errcheck
def __init__(self, *args):
pass
def __setattr__(self, key, value):
super(DummyFuncPtr, self).__setattr__(key, True)
To use it replace your DLL object's _FuncPtr class and then call each function to run the check, e.g.:
dll = ctypes.cdll.LoadLibrary(r'path/to/dll')
# replace the DLL's function pointer
# comment out this line to disable the dummy class
dll._FuncPtr = DummyFuncPtr
some_func = dll.someFunc
some_func.restype = None
some_func.argtypes = None
some_func.errcheck = None
another_func = dll.anotherFunc
another_func.restype = None
another_func.argtypes = None
some_func() # no error
another_func() # Assertion error due to errcheck not defined
The dummy class completely prevents the function from ever being called of course, so just comment out the replacement line to switch back to normal operation.
Note that it will only check each function when that function is called, so this would best be in a unit test file somewhere where the function is guaranteed to be called.
In my tornado application initialization I am creating a session:
self.db = scoped_session(sessionmaker(bind=engine, query_cls=CustomQuery))
CustomQuery:
class CustomQuery(Query):
def __new__(cls, *args, **kwargs):
if args and hasattr(cls, "username"):
return Query(*args, **kwargs).filter_by(username="john")
else:
return Query(*args, **kwargs)
How can I pass some sort of parameter to the CustomQuery class, so that if a different username is passed it is filtered on that username?
I have the following function:
def second(first, a):
# i want to return the results of first(a) in this function.
I just can't figure out how i can put values in (first(a)) without creating another function, or using lambda or any other modules? Any help is appreciated.
Maybe you could call the function first passing as parameter a, and then return the value it returned:
def second(first, a):
return first(a)
>>> def first(a, *args, **kwargs):
... print((a, args, kwargs))
...
>>> def second(func, a, *args, **kwargs):
... return func(a, *args, **kwargs)
...
>>> second(first, 'hello', 'world', do='something')
('hello', ('world',), {'do': 'something'})
>>> second(int, 15)
15
>>> second(range, 2, 5)
range(2, 5)
You can omit *args or **kwargs part if you know you don't need additional arguments or keyword arguments for your functions.
update
Since one effect of these functions is to provide a way to use method chaining on methods that would not normally support it *, I'm considering calling them chain and copychain, respectively. This seems less than ideal though, since the would-be copychain is arguably a more fundamental concept, at least in terms of functional programming.
original
I'm calling it a boxer for now. The code is in Python, though the question is general:
def boxer(f):
"""Return a function g(o, *args, **keyargs) -> o
`g` calls `f` on `o` with the remaining arguments
and returns `o`.
>>> l = [2]
>>> def increment_list_element(l, i):
... l[0] += i
>>> adder = boxer(increment_list_element)
>>> adder(l, 2)
[4]
>>> def multiply_list_element(l, i):
... l[0] *= i
>>> multiplier = boxer(multiply_list_element)
>>> sum(multiplier(l, 6))
24
"""
def box(o, *args, **keyargs):
f(o, *args, **keyargs)
return o
return box
A similar concept copies the would-be assignee, and assigns to and returns the copy. This one is a "shadow_boxer":
from copy import deepcopy
def shadow_boxer(f):
"""Return a function g(o, *args, **keyargs) -> p
`g` deepcopies `o` as `p`,
executes `f` on `p` with the remaining arguments,
and returns `p`.
>>> l = [2]
>>> def increment_list_element(l, i):
... l[0] += i
>>> adder = shadow_boxer(increment_list_element)
>>> adder(l, 2)
[4]
>>> def multiply_list_element(l, i):
... l[0] *= i
>>> multiplier = shadow_boxer(multiply_list_element)
>>> sum(multiplier(l, 6))
12
"""
def shadow_box(o, *args, **keyargs):
p = deepcopy(o)
f(p, *args, **keyargs)
return p
return shadow_box
In addition, I'd like to find out about resources for learning more about these sorts of things — though I'm similarly unsure of a name for "these sorts of things". It does seem related to functional programming, although as I understand it, these technique would be unnecessary in a true functional language.
This is pretty much the same thing as Ruby's Object#tap. Don't know how you feel about the name, but that's what they call it.
What the boxer function returns is probably defined closure in some programming languages. If there is not already a function with this name, I would call the function closure.
I would like to write a bit of code that calls a function specified by a given argument. EG:
def caller(func):
return func()
However what I would also like to do is specify optional arguments to the 'caller' function so that 'caller' calls 'func' with the arguments specified (if any).
def caller(func, args):
# calls func with the arguments specified in args
Is there a simple, pythonic way to do this?
You can do this by using arbitrary argument lists and unpacking argument lists.
>>> def caller(func, *args, **kwargs):
... return func(*args, **kwargs)
...
>>> def hello(a, b, c):
... print a, b, c
...
>>> caller(hello, 1, b=5, c=7)
1 5 7
Not sure why you feel the need to do it, though.
This already exists as the apply function, though it is considered obsolete due to the new *args and **kwargs syntax.
>>> def foo(a,b,c): print a,b,c
>>> apply(foo, (1,2,3))
1 2 3
>>> apply(foo, (1,2), {'c':3}) # also accepts keyword args
However, the * and ** syntax is generally a better solution. The above is equivalent to:
>>> foo(*(1,2), **{'c':3})