-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON - Replaced the "Build_list_of_numbers" component with "Build_lis…
…t_of_integers" and "Build_list_of_floats" components
- Loading branch information
Showing
4 changed files
with
152 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
from kfp.components import create_component_from_func | ||
|
||
|
||
def build_list_of_numbers( | ||
def build_list_of_floats( | ||
item_1: float = None, | ||
item_2: float = None, | ||
item_3: float = None, | ||
item_4: float = None, | ||
item_5: float = None, | ||
) -> list: | ||
"""Creates a JSON array from multiple numbers. | ||
"""Creates a JSON array from multiple floating-point numbers. | ||
Annotations: | ||
author: Alexey Volkov <[email protected]> | ||
|
@@ -21,12 +21,12 @@ def build_list_of_numbers( | |
|
||
|
||
if __name__ == '__main__': | ||
build_list_of_numbers_op = create_component_from_func( | ||
build_list_of_numbers, | ||
build_list_of_floats_op = create_component_from_func( | ||
build_list_of_floats, | ||
base_image='python:3.8', | ||
output_component_file='component.yaml', | ||
annotations={ | ||
"author": "Alexey Volkov <[email protected]>", | ||
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_numbers/component.yaml", | ||
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_floats/component.yaml", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name: Build list of numbers | ||
description: Creates a JSON array from multiple numbers. | ||
name: Build list of floats | ||
description: Creates a JSON array from multiple floating-point numbers. | ||
metadata: | ||
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_numbers/component.yaml'} | ||
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_floats/component.yaml'} | ||
inputs: | ||
- {name: item_1, type: Float, optional: true} | ||
- {name: item_2, type: Float, optional: true} | ||
|
@@ -21,14 +21,14 @@ implementation: | |
printf "%s" "$0" > "$program_path" | ||
python3 -u "$program_path" "$@" | ||
- | | ||
def build_list_of_numbers( | ||
def build_list_of_floats( | ||
item_1 = None, | ||
item_2 = None, | ||
item_3 = None, | ||
item_4 = None, | ||
item_5 = None, | ||
): | ||
"""Creates a JSON array from multiple numbers. | ||
"""Creates a JSON array from multiple floating-point numbers. | ||
Annotations: | ||
author: Alexey Volkov <[email protected]> | ||
|
@@ -51,7 +51,7 @@ implementation: | |
return json.dumps(obj, default=default_serializer, sort_keys=True) | ||
import argparse | ||
_parser = argparse.ArgumentParser(prog='Build list of numbers', description='Creates a JSON array from multiple numbers.') | ||
_parser = argparse.ArgumentParser(prog='Build list of floats', description='Creates a JSON array from multiple floating-point numbers.') | ||
_parser.add_argument("--item-1", dest="item_1", type=float, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-2", dest="item_2", type=float, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-3", dest="item_3", type=float, required=False, default=argparse.SUPPRESS) | ||
|
@@ -61,7 +61,7 @@ implementation: | |
_parsed_args = vars(_parser.parse_args()) | ||
_output_files = _parsed_args.pop("_output_paths", []) | ||
_outputs = build_list_of_numbers(**_parsed_args) | ||
_outputs = build_list_of_floats(**_parsed_args) | ||
_outputs = [_outputs] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from kfp.components import create_component_from_func | ||
|
||
|
||
def build_list_of_integers( | ||
item_1: int = None, | ||
item_2: int = None, | ||
item_3: int = None, | ||
item_4: int = None, | ||
item_5: int = None, | ||
) -> list: | ||
"""Creates a JSON array from multiple integer numbers. | ||
Annotations: | ||
author: Alexey Volkov <[email protected]> | ||
""" | ||
result = [] | ||
for item in [item_1, item_2, item_3, item_4, item_5]: | ||
if item is not None: | ||
result.append(item) | ||
return result | ||
|
||
|
||
if __name__ == '__main__': | ||
build_list_of_integers_op = create_component_from_func( | ||
build_list_of_integers, | ||
base_image='python:3.8', | ||
output_component_file='component.yaml', | ||
annotations={ | ||
"author": "Alexey Volkov <[email protected]>", | ||
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_integers/component.yaml", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
name: Build list of integers | ||
description: Creates a JSON array from multiple integer numbers. | ||
metadata: | ||
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_integers/component.yaml'} | ||
inputs: | ||
- {name: item_1, type: Integer, optional: true} | ||
- {name: item_2, type: Integer, optional: true} | ||
- {name: item_3, type: Integer, optional: true} | ||
- {name: item_4, type: Integer, optional: true} | ||
- {name: item_5, type: Integer, optional: true} | ||
outputs: | ||
- {name: Output, type: JsonArray} | ||
implementation: | ||
container: | ||
image: python:3.8 | ||
command: | ||
- sh | ||
- -ec | ||
- | | ||
program_path=$(mktemp) | ||
printf "%s" "$0" > "$program_path" | ||
python3 -u "$program_path" "$@" | ||
- | | ||
def build_list_of_integers( | ||
item_1 = None, | ||
item_2 = None, | ||
item_3 = None, | ||
item_4 = None, | ||
item_5 = None, | ||
): | ||
"""Creates a JSON array from multiple integer numbers. | ||
Annotations: | ||
author: Alexey Volkov <[email protected]> | ||
""" | ||
result = [] | ||
for item in [item_1, item_2, item_3, item_4, item_5]: | ||
if item is not None: | ||
result.append(item) | ||
return result | ||
def _serialize_json(obj) -> str: | ||
if isinstance(obj, str): | ||
return obj | ||
import json | ||
def default_serializer(obj): | ||
if hasattr(obj, 'to_struct'): | ||
return obj.to_struct() | ||
else: | ||
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__) | ||
return json.dumps(obj, default=default_serializer, sort_keys=True) | ||
import argparse | ||
_parser = argparse.ArgumentParser(prog='Build list of integers', description='Creates a JSON array from multiple integer numbers.') | ||
_parser.add_argument("--item-1", dest="item_1", type=int, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-2", dest="item_2", type=int, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-3", dest="item_3", type=int, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-4", dest="item_4", type=int, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("--item-5", dest="item_5", type=int, required=False, default=argparse.SUPPRESS) | ||
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1) | ||
_parsed_args = vars(_parser.parse_args()) | ||
_output_files = _parsed_args.pop("_output_paths", []) | ||
_outputs = build_list_of_integers(**_parsed_args) | ||
_outputs = [_outputs] | ||
_output_serializers = [ | ||
_serialize_json, | ||
] | ||
import os | ||
for idx, output_file in enumerate(_output_files): | ||
try: | ||
os.makedirs(os.path.dirname(output_file)) | ||
except OSError: | ||
pass | ||
with open(output_file, 'w') as f: | ||
f.write(_output_serializers[idx](_outputs[idx])) | ||
args: | ||
- if: | ||
cond: {isPresent: item_1} | ||
then: | ||
- --item-1 | ||
- {inputValue: item_1} | ||
- if: | ||
cond: {isPresent: item_2} | ||
then: | ||
- --item-2 | ||
- {inputValue: item_2} | ||
- if: | ||
cond: {isPresent: item_3} | ||
then: | ||
- --item-3 | ||
- {inputValue: item_3} | ||
- if: | ||
cond: {isPresent: item_4} | ||
then: | ||
- --item-4 | ||
- {inputValue: item_4} | ||
- if: | ||
cond: {isPresent: item_5} | ||
then: | ||
- --item-5 | ||
- {inputValue: item_5} | ||
- '----output-paths' | ||
- {outputPath: Output} |