Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/pyct/naming.py: 20%
25 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2017 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"""Symbol naming utilities."""
17from tensorflow.python.autograph.pyct import qual_names
20class Namer(object):
21 """Symbol name generator."""
23 def __init__(self, global_namespace):
24 self.global_namespace = global_namespace
25 self.generated_names = set()
27 def new_symbol(self, name_root, reserved_locals):
28 """See control_flow.SymbolNamer.new_symbol."""
29 # reserved_locals may contain QNs.
30 all_reserved_locals = set()
31 for s in reserved_locals:
32 if isinstance(s, qual_names.QN):
33 all_reserved_locals.update(s.qn)
34 elif isinstance(s, str):
35 all_reserved_locals.add(s)
36 else:
37 raise ValueError('Unexpected symbol type "%s"' % type(s))
39 pieces = name_root.split('_')
40 if pieces[-1].isdigit():
41 name_root = '_'.join(pieces[:-1])
42 n = int(pieces[-1])
43 else:
44 n = 0
45 new_name = name_root
47 while (new_name in self.global_namespace or
48 new_name in all_reserved_locals or new_name in self.generated_names):
49 n += 1
50 new_name = '%s_%d' % (name_root, n)
52 self.generated_names.add(new_name)
53 return new_name