in tools/tensorflow_docs/api_generator/doc_generator_visitor.py [0:0]
def _score_name(self, name):
"""Return a tuple of scores indicating how to sort for the best name.
This function is meant to be used as the `key` to the `sorted` function.
This returns a score tuple composed of the following scores:
defining_class: Prefers method names pointing into the defining class,
over a subclass (`ParentClass.method` over `Subclass.method`, if it
referrs to the same method implementation).
experimental: Prefers names that are not in "contrib" or "experimental".
keras: Prefers keras names to non-keras names.
module_length: Prefers submodules (tf.sub.thing) over the root namespace
(tf.thing) over deeply nested paths (tf.a.b.c.thing)
name: Fallback, sorts lexicographically on the full_name.
Args:
name: the full name to score, for example `tf.estimator.Estimator`
Returns:
A tuple of scores. When sorted the preferred name will have the lowest
value.
"""
parts = name.split('.')
short_name = parts[-1]
if len(parts) == 1:
return (-99, -99, -99, -99, short_name)
container = self._index.get('.'.join(parts[:-1]), name)
defining_class_score = 1
if inspect.isclass(container):
if short_name in container.__dict__:
# prefer the defining class
defining_class_score = -1
experimental_score = -1
if 'contrib' in parts or any('experimental' in part for part in parts):
experimental_score = 1
keras_score = 1
if 'keras' in parts:
keras_score = -1
while parts:
container = self._index['.'.join(parts)]
if inspect.ismodule(container):
break
parts.pop()
module_length = len(parts)
if len(parts) == 2:
# `tf.submodule.thing` is better than `tf.thing`
module_length_score = -1
else:
# shorter is better
module_length_score = module_length
return (defining_class_score, experimental_score, keras_score,
module_length_score, name)