• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python decorator_utils.validate_callable函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.python.util.decorator_utils.validate_callable函数的典型用法代码示例。如果您正苦于以下问题:Python validate_callable函数的具体用法?Python validate_callable怎么用?Python validate_callable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了validate_callable函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: deprecated_wrapper

    def deprecated_wrapper(func):
        """Deprecation decorator."""
        decorator_utils.validate_callable(func, "deprecated_arg_values")

        @functools.wraps(func)
        def new_func(*args, **kwargs):
            """Deprecation wrapper."""
            named_args = inspect.getcallargs(func, *args, **kwargs)
            for arg_name, arg_value in deprecated_kwargs.items():
                if arg_name in named_args and named_args[arg_name] == arg_value:
                    logging.warning(
                        "From %s: calling %s (from %s) with %s=%s is deprecated and will "
                        "be removed after %s.\nInstructions for updating:\n%s",
                        _call_location(),
                        decorator_utils.get_qualified_name(func),
                        func.__module__,
                        arg_name,
                        arg_value,
                        date,
                        instructions,
                    )
            return func(*args, **kwargs)

        new_func.__doc__ = _add_deprecated_arg_notice_to_docstring(func.__doc__, date, instructions)
        return new_func
开发者ID:kdavis-mozilla,项目名称:tensorflow,代码行数:25,代码来源:deprecation.py


示例2: experimental

def experimental(func):
  """Decorator for marking functions or methods experimental.

  This decorator logs an experimental warning whenever the decorated function is
  called. It has the following format:

    <function> (from <module>) is experimental and may change or be removed at
    any time, and without warning.

  <function> will include the class name if it is a method.

  It also edits the docstring of the function: ' (experimental)' is appended
  to the first line of the docstring and a notice is prepended to the rest of
  the docstring.

  Args:
    func: A function or method to mark experimental.

  Returns:
    Decorated function or method.
  """
  decorator_utils.validate_callable(func, 'experimental')
  @functools.wraps(func)
  def new_func(*args, **kwargs):
    logging.warning(
        '%s (from %s) is experimental and may change or be removed at '
        'any time, and without warning.',
        decorator_utils.get_qualified_name(func), func.__module__)
    return func(*args, **kwargs)
  new_func.__doc__ = _add_experimental_function_notice_to_docstring(
      func.__doc__)
  return new_func
开发者ID:1000sprites,项目名称:tensorflow,代码行数:32,代码来源:experimental.py


示例3: keyword_args_only

def keyword_args_only(func):
  """Decorator for marking specific function accepting keyword args only.

  This decorator raises a `ValueError` if the input `func` is called with any
  non-keyword args. This prevents the caller from providing the arguments in
  wrong order.

  Args:
    func: The function or method needed to be decorated.

  Returns:
    Decorated function or method.

  Raises:
    ValueError: If `func` is not callable.
  """

  decorator_utils.validate_callable(func, "keyword_args_only")
  @functools.wraps(func)
  def new_func(*args, **kwargs):
    """Keyword args only wrapper."""
    if args:
      raise ValueError(
          "Must use keyword args to call {}.".format(func.__name__))
    return func(**kwargs)
  return new_func
开发者ID:1000sprites,项目名称:tensorflow,代码行数:26,代码来源:keyword_args.py


示例4: deprecated_wrapper

 def deprecated_wrapper(func):
   """Deprecation wrapper."""
   decorator_utils.validate_callable(func, 'deprecated')
   @functools.wraps(func)
   def new_func(*args, **kwargs):
     logging.warning(
         '%s (from %s) is deprecated and will be removed after %s.\n'
         'Instructions for updating:\n%s',
         decorator_utils.get_qualified_name(func), func.__module__, date,
         instructions)
     return func(*args, **kwargs)
   new_func.__doc__ = _add_deprecated_function_notice_to_docstring(
       func.__doc__, date, instructions)
   return new_func
开发者ID:DavidNemeskey,项目名称:tensorflow,代码行数:14,代码来源:deprecation.py


示例5: deprecated_wrapper

 def deprecated_wrapper(func):
   """Deprecation wrapper."""
   decorator_utils.validate_callable(func, 'deprecated')
   @functools.wraps(func)
   def new_func(*args, **kwargs):
     logging.warning(
         'From %s: %s (from %s) is deprecated and will be removed %s.\n'
         'Instructions for updating:\n%s',
         _call_location(), decorator_utils.get_qualified_name(func),
         func.__module__,
         'in a future version' if date is None else ('after %s' % date),
         instructions)
     return func(*args, **kwargs)
   return tf_decorator.make_decorator(
       func, new_func, 'deprecated',
       _add_deprecated_function_notice_to_docstring(func.__doc__, date,
                                                    instructions))
开发者ID:AlbertXiebnu,项目名称:tensorflow,代码行数:17,代码来源:deprecation.py


示例6: deprecated_wrapper

  def deprecated_wrapper(func):
    """Deprecation decorator."""
    decorator_utils.validate_callable(func, 'deprecated_args')

    arg_spec = inspect.getargspec(func)
    deprecated_positions = [
        (i, arg_name) for (i, arg_name) in enumerate(arg_spec.args)
        if arg_name in deprecated_arg_names]
    is_varargs_deprecated = arg_spec.varargs in deprecated_arg_names
    is_kwargs_deprecated = arg_spec.keywords in deprecated_arg_names

    if (len(deprecated_positions) + is_varargs_deprecated + is_kwargs_deprecated
        != len(deprecated_arg_names)):
      known_args = arg_spec.args + [arg_spec.varargs, arg_spec.keywords]
      missing_args = [arg_name for arg_name in deprecated_arg_names
                      if arg_name not in known_args]
      raise ValueError('The following deprecated arguments are not present '
                       'in the function signature: %s. '
                       'Found next arguments: %s.' % (missing_args, known_args))

    @functools.wraps(func)
    def new_func(*args, **kwargs):
      """Deprecation wrapper."""
      invalid_args = []
      for (i, arg_name) in deprecated_positions:
        if i < len(args):
          invalid_args.append(arg_name)
      if is_varargs_deprecated and len(args) > len(arg_spec.args):
        invalid_args.append(arg_spec.varargs)
      if is_kwargs_deprecated and kwargs:
        invalid_args.append(arg_spec.keywords)
      for arg_name in deprecated_arg_names:
        if arg_name in kwargs:
          invalid_args.append(arg_name)
      for arg_name in invalid_args:
        logging.warning(
            'From %s: calling %s (from %s) with %s is deprecated and will '
            'be removed after %s.\nInstructions for updating:\n%s',
            _call_location(), decorator_utils.get_qualified_name(func),
            func.__module__, arg_name, date, instructions)
      return func(*args, **kwargs)
    new_func.__doc__ = _add_deprecated_arg_notice_to_docstring(
        func.__doc__, date, instructions)
    return new_func
开发者ID:ComeOnGetMe,项目名称:tensorflow,代码行数:44,代码来源:deprecation.py


示例7: deprecated_wrapper

 def deprecated_wrapper(func):
   """Deprecation wrapper."""
   decorator_utils.validate_callable(func, 'deprecated')
   @functools.wraps(func)
   def new_func(*args, **kwargs):  # pylint: disable=missing-docstring
     if _PRINT_DEPRECATION_WARNINGS:
       if func not in _PRINTED_WARNING:
         if warn_once:
           _PRINTED_WARNING[func] = True
         logging.warning(
             'From %s: %s (from %s) is deprecated and will be removed %s.\n'
             'Instructions for updating:\n%s',
             _call_location(), decorator_utils.get_qualified_name(func),
             func.__module__,
             'in a future version' if date is None else ('after %s' % date),
             instructions)
     return func(*args, **kwargs)
   return tf_decorator.make_decorator(
       func, new_func, 'deprecated',
       _add_deprecated_function_notice_to_docstring(func.__doc__, date,
                                                    instructions))
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:21,代码来源:deprecation.py


示例8: deprecated_alias

def deprecated_alias(deprecated_name, name, func_or_class, warn_once=True):
  """Deprecate a symbol in favor of a new name with identical semantics.

  This function is meant to be used when defining a backwards-compatibility
  alias for a symbol which has been moved. For example:

  module1.py:
  ```python
  class NewNameForClass: pass
  ```

  module2.py:
  ```python
  import module1

  DeprecatedNameForClass = deprecated_alias(
    deprecated_name='module2.DeprecatedNameForClass',
    name='module1.NewNameForClass',
    module1.NewNameForClass)
  ```

  This function works for classes and functions.

  For classes, it creates a new class which is functionally identical (it
  inherits from the original, and overrides its constructor), but which prints
  a deprecation warning when an instance is created. It also adds a deprecation
  notice to the class' docstring.

  For functions, it returns a function wrapped by `tf_decorator.make_decorator`.
  That function prints a warning when used, and has a deprecation notice in its
  docstring. This is more or less equivalent (the deprecation warning has
  slightly different text) to writing:

  ```python
  @deprecated
  def deprecated_alias(original_args):
    real_function(original_args)
  ```

  Args:
    deprecated_name: The name of the symbol that is being deprecated, to be used
      in the warning message. This should be its fully qualified name to avoid
      confusion.
    name: The name of the symbol that is to be used instead of the deprecated
      name. This should be a fully qualified name to avoid confusion.
    func_or_class: The (non-deprecated) class or function for which a deprecated
      alias should be created.
    warn_once: If True (the default), only print a deprecation warning the first
      time this function is used, or the class is instantiated.

  Returns:
    A wrapped version of `func_or_class` which prints a deprecation warning on
    use and has a modified docstring.
  """
  if tf_inspect.isclass(func_or_class):

    # Make a new class with __init__ wrapped in a warning.
    class _NewClass(func_or_class):  # pylint: disable=missing-docstring
      __doc__ = decorator_utils.add_notice_to_docstring(
          func_or_class.__doc__, 'Please use %s instead.' % name,
          'DEPRECATED CLASS',
          '(deprecated)', ['THIS CLASS IS DEPRECATED. '
                           'It will be removed in a future version. '])
      __name__ = func_or_class.__name__
      __module__ = _call_location(outer=True)

      @_wrap_decorator(func_or_class.__init__)
      def __init__(self, *args, **kwargs):
        if hasattr(_NewClass.__init__, '__func__'):
          # Python 2
          _NewClass.__init__.__func__.__doc__ = func_or_class.__init__.__doc__
        else:
          # Python 3
          _NewClass.__init__.__doc__ = func_or_class.__init__.__doc__

        if _PRINT_DEPRECATION_WARNINGS:
          # We're making the alias as we speak. The original may have other
          # aliases, so we cannot use it to check for whether it's already been
          # warned about.
          if _NewClass.__init__ not in _PRINTED_WARNING:
            if warn_once:
              _PRINTED_WARNING[_NewClass.__init__] = True
            logging.warning(
                'From %s: The name %s is deprecated. Please use %s instead.\n',
                _call_location(), deprecated_name, name)
        super(_NewClass, self).__init__(*args, **kwargs)

    return _NewClass
  else:
    decorator_utils.validate_callable(func_or_class, 'deprecated')

    # Make a wrapper for the original
    @functools.wraps(func_or_class)
    def new_func(*args, **kwargs):  # pylint: disable=missing-docstring
      if _PRINT_DEPRECATION_WARNINGS:
        # We're making the alias as we speak. The original may have other
        # aliases, so we cannot use it to check for whether it's already been
        # warned about.
        if new_func not in _PRINTED_WARNING:
          if warn_once:
#.........这里部分代码省略.........
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:101,代码来源:deprecation.py


示例9: deprecated_wrapper

  def deprecated_wrapper(func):
    """Deprecation decorator."""
    decorator_utils.validate_callable(func, 'deprecated_args')
    deprecated_arg_names = _get_arg_names_to_ok_vals()

    arg_spec = inspect.getargspec(func)
    deprecated_positions = _get_deprecated_positional_arguments(
        deprecated_arg_names, arg_spec)

    is_varargs_deprecated = arg_spec.varargs in deprecated_arg_names
    is_kwargs_deprecated = arg_spec.keywords in deprecated_arg_names

    if (len(deprecated_positions) + is_varargs_deprecated + is_kwargs_deprecated
        != len(deprecated_arg_names_or_tuples)):
      known_args = arg_spec.args + [arg_spec.varargs, arg_spec.keywords]
      missing_args = [arg_name for arg_name in deprecated_arg_names
                      if arg_name not in known_args]
      raise ValueError('The following deprecated arguments are not present '
                       'in the function signature: %s. '
                       'Found next arguments: %s.' % (missing_args, known_args))

    def _same_value(a, b):
      """A comparison operation that works for multiple object types.

      Returns True for two empty lists, two numeric values with the
      same value, etc.

      Returns False for (pd.DataFrame, None), and other pairs which
      should not be considered equivalent.

      Args:
        a: value one of the comparison.
        b: value two of the comparison.

      Returns:
        A boolean indicating whether the two inputs are the same value
        for the purposes of deprecation.
      """
      if a is b:
        return True
      try:
        equality = a == b
        if isinstance(equality, bool):
          return equality
      except TypeError:
        return False
      return False

    @functools.wraps(func)
    def new_func(*args, **kwargs):
      """Deprecation wrapper."""
      invalid_args = []
      named_args = inspect.getcallargs(func, *args, **kwargs)
      for arg_name, spec in iter(deprecated_positions.items()):
        if (spec.position < len(args) and
            not (spec.has_ok_value and
                 _same_value(named_args[arg_name], spec.ok_value))):
          invalid_args.append(arg_name)
      if is_varargs_deprecated and len(args) > len(arg_spec.args):
        invalid_args.append(arg_spec.varargs)
      if is_kwargs_deprecated and kwargs:
        invalid_args.append(arg_spec.keywords)
      for arg_name in deprecated_arg_names:
        if (arg_name in kwargs and
            not (deprecated_positions[arg_name].has_ok_value and
                 _same_value(named_args[arg_name],
                             deprecated_positions[arg_name].ok_value))):
          invalid_args.append(arg_name)
      for arg_name in invalid_args:
        logging.warning(
            'From %s: calling %s (from %s) with %s is deprecated and will '
            'be removed after %s.\nInstructions for updating:\n%s',
            _call_location(), decorator_utils.get_qualified_name(func),
            func.__module__, arg_name, date, instructions)
      return func(*args, **kwargs)
    new_func.__doc__ = _add_deprecated_arg_notice_to_docstring(
        func.__doc__, date, instructions)
    return new_func
开发者ID:AliMiraftab,项目名称:tensorflow,代码行数:78,代码来源:deprecation.py


示例10: test_partial

 def test_partial(self):
   partial = functools.partial(_test_function, unused_arg=7)
   decorator_utils.validate_callable(partial, "test")
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:3,代码来源:decorator_utils_test.py


示例11: test_callable

  def test_callable(self):
    class TestClass(object):

      def __call__(self):
        pass
    decorator_utils.validate_callable(TestClass(), "test")
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:6,代码来源:decorator_utils_test.py


示例12: test_method

 def test_method(self):
   decorator_utils.validate_callable(self.test_method, "test")
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:2,代码来源:decorator_utils_test.py


示例13: test_function

 def test_function(self):
   decorator_utils.validate_callable(_test_function, "test")
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:2,代码来源:decorator_utils_test.py



注:本文中的tensorflow.python.util.decorator_utils.validate_callable函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python function_utils.fn_args函数代码示例发布时间:2022-05-27
下一篇:
Python compat.as_text函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap