Skip to content

Commit

Permalink
SDK/Components - Added naming.generate_unique_name_conversion_table (#…
Browse files Browse the repository at this point in the history
…716)

generate_unique_name_conversion_table replaces _make_name_unique_by_adding_index and simplifies code in several places.
  • Loading branch information
Ark-kun authored and k8s-ci-robot committed Mar 6, 2019
1 parent 0c0c49e commit fa02e75
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
12 changes: 4 additions & 8 deletions sdk/python/kfp/components/_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import sys
from collections import OrderedDict
from ._naming import _sanitize_file_name, _sanitize_python_function_name, _make_name_unique_by_adding_index
from ._naming import _sanitize_file_name, _sanitize_python_function_name, generate_unique_name_conversion_table
from ._yaml_utils import load_yaml
from ._structures import ComponentSpec
from ._structures import *
Expand Down Expand Up @@ -165,15 +165,11 @@ def _create_task_factory_from_component_spec(component_spec:ComponentSpec, compo
description = component_spec.description

inputs_list = component_spec.inputs or [] #List[InputSpec]
input_names = [input.name for input in inputs_list]

#Creating the name translation tables : Original <-> Pythonic
input_name_to_pythonic = {}
pythonic_name_to_input_name = {}
for io_port in inputs_list:
pythonic_name = _sanitize_python_function_name(io_port.name)
pythonic_name = _make_name_unique_by_adding_index(pythonic_name, pythonic_name_to_input_name, '_')
input_name_to_pythonic[io_port.name] = pythonic_name
pythonic_name_to_input_name[pythonic_name] = io_port.name
input_name_to_pythonic = generate_unique_name_conversion_table(input_names, _sanitize_python_function_name)
pythonic_name_to_input_name = {v: k for k, v in input_name_to_pythonic.items()}

if component_ref is None:
component_ref = ComponentReference(name=component_spec.name or component_filename or _default_component_name)
Expand Down
13 changes: 4 additions & 9 deletions sdk/python/kfp/components/_dsl_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,10 @@ def _create_container_op_from_resolved_task(name:str, container_image:str, comma
_dummy_pipeline = dsl.Pipeline('dummy pipeline')
_dummy_pipeline.__enter__()

from ._naming import _sanitize_kubernetes_resource_name, _make_name_unique_by_adding_index
output_name_to_kubernetes = {}
kubernetes_name_to_output_name = {}
for output_name in (output_paths or {}).keys():
kubernetes_name = _sanitize_kubernetes_resource_name(output_name)
kubernetes_name = _make_name_unique_by_adding_index(kubernetes_name, kubernetes_name_to_output_name, '-')
output_name_to_kubernetes[output_name] = kubernetes_name
kubernetes_name_to_output_name[kubernetes_name] = output_name

#Renaming outputs to conform with ContainerOp/Argo
from ._naming import _sanitize_kubernetes_resource_name, generate_unique_name_conversion_table
output_names = (output_paths or {}).keys()
output_name_to_kubernetes = generate_unique_name_conversion_table(output_names, _sanitize_kubernetes_resource_name)
output_paths_for_container_op = {output_name_to_kubernetes[name]: path for name, path in output_paths.items()}

# Construct the ComponentMeta
Expand Down
27 changes: 27 additions & 0 deletions sdk/python/kfp/components/_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
'_convert_to_human_name',
'_generate_unique_suffix',
'_make_name_unique_by_adding_index',
'_convert_name_and_make_it_unique_by_adding_number',
'generate_unique_name_conversion_table',
]


import re
import sys
from typing import Callable, Sequence, Mapping


def _normalize_identifier_name(name):
Expand Down Expand Up @@ -72,3 +75,27 @@ def _make_name_unique_by_adding_index(name:str, collection, delimiter:str):
if unique_name not in collection:
break
return unique_name


def _convert_name_and_make_it_unique_by_adding_number(name: str, used_converted_names, conversion_func: Callable[[str], str]):
converted_name = conversion_func(name)
if converted_name in used_converted_names:
for i in range(2, sys.maxsize ** 10): #Starting indices from 2: "Something", "Something_2", ...
converted_name = conversion_func(name + ' ' + str(i))
if converted_name not in used_converted_names:
break
return converted_name


def generate_unique_name_conversion_table(names: Sequence[str], conversion_func: Callable[[str], str]) -> Mapping[str, str]:
'''Given a list of names and conversion_func, this function generates a map from original names to converted names that are made unique by adding numbers.
'''
forward_map = {}
reverse_map = {}
for name in names:
if name in forward_map:
raise ValueError('Original name {} is not unique.'.format(name))
converted_name = _convert_name_and_make_it_unique_by_adding_number(name, reverse_map, conversion_func)
forward_map[name] = converted_name
reverse_map[converted_name] = name
return forward_map

0 comments on commit fa02e75

Please sign in to comment.