Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/compat/tensorflow_stub/compat/__init__.py: 45%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Functions for Python 2 vs. 3 compatibility. 

16 

17## Conversion routines 

18In addition to the functions below, `as_str` converts an object to a `str`. 

19 

20 

21## Types 

22The compatibility module also provides the following types: 

23 

24* `bytes_or_text_types` 

25* `complex_types` 

26* `integral_types` 

27* `real_types` 

28""" 

29 

30 

31import numbers as _numbers 

32import numpy as _np 

33 

34from tensorboard.compat.tensorflow_stub.compat.v1 import * # noqa 

35 

36 

37def as_bytes(bytes_or_text, encoding="utf-8"): 

38 """Converts either bytes or unicode to `bytes`, using utf-8 encoding for 

39 text. 

40 

41 Args: 

42 bytes_or_text: A `bytes`, `str`, or `unicode` object. 

43 encoding: A string indicating the charset for encoding unicode. 

44 

45 Returns: 

46 A `bytes` object. 

47 

48 Raises: 

49 TypeError: If `bytes_or_text` is not a binary or unicode string. 

50 """ 

51 if isinstance(bytes_or_text, str): 

52 return bytes_or_text.encode(encoding) 

53 elif isinstance(bytes_or_text, bytes): 

54 return bytes_or_text 

55 else: 

56 raise TypeError( 

57 "Expected binary or unicode string, got %r" % (bytes_or_text,) 

58 ) 

59 

60 

61def as_text(bytes_or_text, encoding="utf-8"): 

62 """Returns the given argument as a unicode string. 

63 

64 Args: 

65 bytes_or_text: A `bytes`, `str`, or `unicode` object. 

66 encoding: A string indicating the charset for decoding unicode. 

67 

68 Returns: 

69 A `unicode` (Python 2) or `str` (Python 3) object. 

70 

71 Raises: 

72 TypeError: If `bytes_or_text` is not a binary or unicode string. 

73 """ 

74 if isinstance(bytes_or_text, str): 

75 return bytes_or_text 

76 elif isinstance(bytes_or_text, bytes): 

77 return bytes_or_text.decode(encoding) 

78 else: 

79 raise TypeError( 

80 "Expected binary or unicode string, got %r" % bytes_or_text 

81 ) 

82 

83 

84# Convert an object to a `str` in both Python 2 and 3. 

85as_str = as_text 

86 

87 

88# @tf_export('compat.as_str_any') 

89def as_str_any(value): 

90 """Converts to `str` as `str(value)`, but use `as_str` for `bytes`. 

91 

92 Args: 

93 value: A object that can be converted to `str`. 

94 

95 Returns: 

96 A `str` object. 

97 """ 

98 if isinstance(value, bytes): 

99 return as_str(value) 

100 else: 

101 return str(value) 

102 

103 

104# @tf_export('compat.path_to_str') 

105def path_to_str(path): 

106 """Returns the file system path representation of a `PathLike` object, else 

107 as it is. 

108 

109 Args: 

110 path: An object that can be converted to path representation. 

111 

112 Returns: 

113 A `str` object. 

114 """ 

115 if hasattr(path, "__fspath__"): 

116 path = as_str_any(path.__fspath__()) 

117 return path 

118 

119 

120# Numpy 1.8 scalars don't inherit from numbers.Integral in Python 3, so we 

121# need to check them specifically. The same goes from Real and Complex. 

122integral_types = (_numbers.Integral, _np.integer) 

123# tf_export('compat.integral_types').export_constant(__name__, 'integral_types') 

124real_types = (_numbers.Real, _np.integer, _np.floating) 

125# tf_export('compat.real_types').export_constant(__name__, 'real_types') 

126complex_types = (_numbers.Complex, _np.number) 

127# tf_export('compat.complex_types').export_constant(__name__, 'complex_types') 

128 

129# Either bytes or text. 

130bytes_or_text_types = (bytes, str) 

131# tf_export('compat.bytes_or_text_types').export_constant(__name__, 

132# 'bytes_or_text_types')