You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

deprecated.py 1.9 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import functools
  4. import inspect
  5. import sys
  6. import wrapt
  7. from tensorlayer.decorators.utils import (
  8. add_deprecation_notice_to_docstring, get_qualified_name, validate_deprecation_args
  9. )
  10. __all__ = ['deprecated']
  11. # Allow deprecation warnings to be silenced temporarily with a context manager.
  12. _PRINT_DEPRECATION_WARNINGS = True
  13. # Remember which deprecation warnings have been printed already.
  14. _PRINTED_WARNING = {}
  15. def deprecated(wrapped=None, date='', instructions='', warn_once=True):
  16. if wrapped is None:
  17. return functools.partial(deprecated, date=date, instructions=instructions, warn_once=warn_once)
  18. @wrapt.decorator
  19. def wrapper(wrapped, instance=None, args=None, kwargs=None):
  20. validate_deprecation_args(date, instructions)
  21. if _PRINT_DEPRECATION_WARNINGS:
  22. class_or_func_name = get_qualified_name(wrapped)
  23. if class_or_func_name not in _PRINTED_WARNING:
  24. if warn_once:
  25. _PRINTED_WARNING[class_or_func_name] = True
  26. from tensorlayer import logging
  27. logging.warning(
  28. '%s: `%s.%s` (in file: %s) is deprecated and will be removed %s.\n'
  29. 'Instructions for updating: %s\n' % (
  30. "Class" if inspect.isclass(wrapped) else "Function", wrapped.__module__, class_or_func_name,
  31. wrapped.__code__.co_filename, 'in a future version' if date is None else
  32. ('after %s' % date), instructions
  33. )
  34. )
  35. return wrapped(*args, **kwargs)
  36. decorated = wrapper(wrapped)
  37. if sys.version_info > (3, 0): # docstring can only be edited with Python 3
  38. wrapt.FunctionWrapper.__setattr__(
  39. decorated, "__doc__", add_deprecation_notice_to_docstring(wrapped.__doc__, date, instructions)
  40. )
  41. return decorated

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.