Advertisement
Guest User

Untitled

a guest
Jan 9th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. # http://www-01.ibm.com/support/knowledgecenter/SSLVMB_21.0.0/com.ibm.spss.statistics.help/idh_pref_general.htm
  2.  
  3. import inspect
  4. import functools
  5. import warnings
  6.  
  7. warnings.simplefilter("always")
  8.  
  9. class check_deprecated_args(object):
  10.    
  11.     """
  12.    Class decorator that checks whether deprecated parameters with non-default
  13.    arguments are used
  14.  
  15.  
  16.    Parameters
  17.    ----------
  18.    deprecated_params : tuple
  19.        tuple of strings of parameters that should raise a DeprecationWarning,
  20.        if they are used with non-default arguments
  21.    methods : tuple, optional
  22.        tuple of strings of method names that include deprecated parameters
  23.  
  24.    Raises
  25.    ------
  26.    DeprecationWarning : if deprecated parameters are used with
  27.        non-default arguments in the class's constructor.
  28.    """
  29.  
  30.     def __init__(self, deprecated_params, methods=("__init__",)):
  31.         self.deprecated_params = deprecated_params
  32.         self.methods = methods
  33.  
  34.     def _used_deprecated(self, used_param, used_arg, default_arg):
  35.         is_deprecated = used_param in self.deprecated_params
  36.         if default_arg != used_arg and is_deprecated:
  37.             return {used_param: used_arg}
  38.         return {}
  39.  
  40.     def _check_method(self, method, *used_args, **used_kwargs):
  41.         argspec = inspect.getargspec(method)
  42.         default_params = argspec.args[1:]
  43.         default_args = argspec.defaults
  44.         res = {}
  45.  
  46.         # -- positional args
  47.         for used_param_pos, used_arg in enumerate(used_args):
  48.             used_param = default_params[used_param_pos]
  49.             default_arg = default_args[used_param_pos]
  50.             res.update(self._used_deprecated(used_param, used_arg, default_arg))
  51.  
  52.         # -- keyword args
  53.         for pos, (used_param, used_arg) in enumerate(used_kwargs.items()):
  54.             default_arg = default_args[pos]
  55.             is_deprecated = used_param in self.deprecated_params
  56.             res.update(self._used_deprecated(used_param, used_arg, default_arg))
  57.  
  58.         if res:
  59.             deprec = self.deprecated_params
  60.             old = ["%s:%%(%s)s" % (p, p) for p in deprec if p in res]
  61.             old =  ", ".join(old) % res
  62.             msg = "you're using obsolete args [%s]" % old
  63.             warnings.warn(msg, DeprecationWarning)
  64.  
  65.     def __call__(self, cls):
  66.         @functools.wraps(cls)
  67.         def inner(*used_args, **used_kwargs):
  68.             for method in self.methods:
  69.                 method = getattr(cls, method)  
  70.                 self._check_method(method, *used_args, **used_kwargs)
  71.                 return getattr(cls, "self")  
  72.         return inner
  73.  
  74. if __name__ == "__main__":
  75.     @check_deprecated_args(deprecated_params=("old", "older"))
  76.     class Foo(object):
  77.         def __init__(self, old=True, older=None, new="yaay"):
  78.             pass
  79.         def bar(self, old, new):
  80.             return old, new
  81.  
  82.     f = Foo(new="great", old="obsolete", older="stop using it")
  83.     #print repr(f)
  84.     #f.bar(old="ancient", new="novel")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement