Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract the pipelineparam deserialize function #841

Merged
merged 5 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions sdk/python/kfp/dsl/_container_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from . import _pipeline
from . import _pipeline_param
from ._pipeline_param import _extract_pipelineparams
import re
from typing import Dict

Expand Down Expand Up @@ -64,13 +65,8 @@ def __init__(self, name: str, image: str, command: str=None, arguments: str=None
self.pod_labels = {}
self.num_retries = 0

matches = []
for arg in (command or []) + (arguments or []):
match = re.findall(r'{{pipelineparam:op=([\w\s_-]*);name=([\w\s_-]+);value=(.*?)}}', str(arg))
matches += match
self.argument_inputs = _extract_pipelineparams([str(arg) for arg in (command or []) + (arguments or [])])

self.argument_inputs = [_pipeline_param.PipelineParam(x[1], x[0], x[2])
for x in list(set(matches))]
self.file_outputs = file_outputs
self.dependent_op_names = []

Expand Down
15 changes: 15 additions & 0 deletions sdk/python/kfp/dsl/_pipeline_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
# For now, this identifies a condition with only "==" operator supported.
ConditionOperator = namedtuple('ConditionOperator', 'operator operand1 operand2')

def _extract_pipelineparams(payloads: str or list[str]):
"""_extract_pipelineparam extract a list of PipelineParam instances from the payload string.
Note: this function removes all duplicate matches.

Args:
payload (str or list[str]): a string/a list of strings that contains serialized pipelineparams
Return:
List[PipelineParam]
"""
if isinstance(payloads, str):
payloads = [payloads]
matches = []
for payload in payloads:
matches += re.findall(r'{{pipelineparam:op=([\w\s_-]*);name=([\w\s_-]+);value=(.*?)}}', payload)
return [PipelineParam(x[1], x[0], x[2]) for x in list(set(matches))]

class PipelineParam(object):
"""Representing a future value that is passed between pipeline components.
Expand Down
15 changes: 15 additions & 0 deletions sdk/python/tests/dsl/pipeline_param_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


from kfp.dsl import PipelineParam
from kfp.dsl._pipeline_param import _extract_pipelineparams
import unittest


Expand All @@ -35,3 +36,17 @@ def test_str_repr(self):

p = PipelineParam(name='param3', value='value3')
self.assertEqual('{{pipelineparam:op=;name=param3;value=value3}}', str(p))

def test_extract_pipelineparam(self):
"""Test _extract_pipeleineparam."""

p1 = PipelineParam(name='param1', op_name='op1')
p2 = PipelineParam(name='param2')
p3 = PipelineParam(name='param3', value='value3')
stuff_chars = ' between '
payload = str(p1) + stuff_chars + str(p2) + stuff_chars + str(p3)
params = _extract_pipelineparams(payload)
self.assertListEqual([p1, p2, p3], params)
payload = [str(p1) + stuff_chars + str(p2), str(p2) + stuff_chars + str(p3)]
params = _extract_pipelineparams(payload)
self.assertListEqual([p1, p2, p3], params)