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.

utils.py 3.9 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. NOTE: DO NOT REMOVE THESE FILES. They are copied from Tensorflow repository and are necessary to build the library without installing TF.
  5. Source: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/util
  6. They replace the following imports:
  7. >>> from tensorflow.python.util import decorator_utils
  8. >>> from tensorflow.python.util.deprecation import _validate_deprecation_args
  9. """
  10. import re
  11. import sys
  12. __all__ = ["add_deprecation_notice_to_docstring", "get_qualified_name", "validate_deprecation_args"]
  13. def add_deprecation_notice_to_docstring(doc, date, instructions):
  14. return _add_deprecated_function_notice_to_docstring(doc, date, instructions)
  15. def get_qualified_name(function):
  16. # Python 3
  17. if hasattr(function, '__qualname__'):
  18. return function.__qualname__
  19. # Python 2
  20. if hasattr(function, 'im_class'):
  21. return function.im_class.__name__ + '.' + function.__name__
  22. return function.__name__
  23. def validate_deprecation_args(date, instructions):
  24. if date is not None and not re.match(r'20\d\d-[01]\d-[0123]\d', date):
  25. raise ValueError('Date must be YYYY-MM-DD.')
  26. if not instructions:
  27. raise ValueError('Don\'t deprecate things without conversion instructions!')
  28. def _add_deprecated_function_notice_to_docstring(doc, date, instructions):
  29. """Adds a deprecation notice to a docstring for deprecated functions."""
  30. if instructions:
  31. deprecation_message = """
  32. .. warning::
  33. **THIS FUNCTION IS DEPRECATED:** It will be removed after %s.
  34. *Instructions for updating:* %s.
  35. """ % (('in a future version' if date is None else ('after %s' % date)), instructions)
  36. else:
  37. deprecation_message = """
  38. .. warning::
  39. **THIS FUNCTION IS DEPRECATED:** It will be removed after %s.
  40. """ % (('in a future version' if date is None else ('after %s' % date)))
  41. main_text = [deprecation_message]
  42. return _add_notice_to_docstring(doc, 'DEPRECATED FUNCTION', main_text)
  43. def _add_notice_to_docstring(doc, no_doc_str, notice):
  44. """Adds a deprecation notice to a docstring."""
  45. if not doc:
  46. lines = [no_doc_str]
  47. else:
  48. lines = _normalize_docstring(doc).splitlines()
  49. notice = [''] + notice
  50. if len(lines) > 1:
  51. # Make sure that we keep our distance from the main body
  52. if lines[1].strip():
  53. notice.append('')
  54. lines[1:1] = notice
  55. else:
  56. lines += notice
  57. return '\n'.join(lines)
  58. def _normalize_docstring(docstring):
  59. """Normalizes the docstring.
  60. Replaces tabs with spaces, removes leading and trailing blanks lines, and
  61. removes any indentation.
  62. Copied from PEP-257:
  63. https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
  64. Args:
  65. docstring: the docstring to normalize
  66. Returns:
  67. The normalized docstring
  68. """
  69. if not docstring:
  70. return ''
  71. # Convert tabs to spaces (following the normal Python rules)
  72. # and split into a list of lines:
  73. lines = docstring.expandtabs().splitlines()
  74. # Determine minimum indentation (first line doesn't count):
  75. # (we use sys.maxsize because sys.maxint doesn't exist in Python 3)
  76. indent = sys.maxsize
  77. for line in lines[1:]:
  78. stripped = line.lstrip()
  79. if stripped:
  80. indent = min(indent, len(line) - len(stripped))
  81. # Remove indentation (first line is special):
  82. trimmed = [lines[0].strip()]
  83. if indent < sys.maxsize:
  84. for line in lines[1:]:
  85. trimmed.append(line[indent:].rstrip())
  86. # Strip off trailing and leading blank lines:
  87. while trimmed and not trimmed[-1]:
  88. trimmed.pop()
  89. while trimmed and not trimmed[0]:
  90. trimmed.pop(0)
  91. # Return a single string:
  92. return '\n'.join(trimmed)

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