G
Giampaolo Rodolà
I've written this decorator to deprecate a function and (optionally)
provide a callable as replacement
def deprecated(repfun=None):
"""A decorator which can be used to mark functions as deprecated.
Optional repfun is a callable that will be called with the same args
as the decorated function.
"""
def outer(fun):
def inner(*args, **kwargs):
msg = "%s is deprecated" % fun.__name__
if repfun is not None:
msg += "; use %s instead" % (repfun.__name__)
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
if repfun is not None:
return repfun(*args, **kwargs)
else:
return fun(*args, **kwargs)
return inner
return outer
Now, I can use my decorator as such:
@deprecated()
def foo():
return 0
....or provide an optional argument:
@deprecated(some_function)
def foo():
return 0
....but I don't know how to modify it so that I can omit parentheses:
@deprecated
def foo():
return 0
Any hint?
--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/
provide a callable as replacement
def deprecated(repfun=None):
"""A decorator which can be used to mark functions as deprecated.
Optional repfun is a callable that will be called with the same args
as the decorated function.
"""
def outer(fun):
def inner(*args, **kwargs):
msg = "%s is deprecated" % fun.__name__
if repfun is not None:
msg += "; use %s instead" % (repfun.__name__)
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
if repfun is not None:
return repfun(*args, **kwargs)
else:
return fun(*args, **kwargs)
return inner
return outer
Now, I can use my decorator as such:
@deprecated()
def foo():
return 0
....or provide an optional argument:
@deprecated(some_function)
def foo():
return 0
....but I don't know how to modify it so that I can omit parentheses:
@deprecated
def foo():
return 0
Any hint?
--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/