diff --git a/components/json/Build_list_of_numbers/component.py b/components/json/Build_list_of_floats/component.py similarity index 78% rename from components/json/Build_list_of_numbers/component.py rename to components/json/Build_list_of_floats/component.py index 65674b6..e67d833 100644 --- a/components/json/Build_list_of_numbers/component.py +++ b/components/json/Build_list_of_floats/component.py @@ -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 @@ -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 ", - "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", }, ) diff --git a/components/json/Build_list_of_numbers/component.yaml b/components/json/Build_list_of_floats/component.yaml similarity index 87% rename from components/json/Build_list_of_numbers/component.yaml rename to components/json/Build_list_of_floats/component.yaml index a9eea1f..ce9fbbe 100644 --- a/components/json/Build_list_of_numbers/component.yaml +++ b/components/json/Build_list_of_floats/component.yaml @@ -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 , canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_numbers/component.yaml'} + annotations: {author: Alexey Volkov , 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 @@ -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] diff --git a/components/json/Build_list_of_integers/component.py b/components/json/Build_list_of_integers/component.py new file mode 100644 index 0000000..73e31e5 --- /dev/null +++ b/components/json/Build_list_of_integers/component.py @@ -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 + """ + 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 ", + "canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Build_list_of_integers/component.yaml", + }, + ) diff --git a/components/json/Build_list_of_integers/component.yaml b/components/json/Build_list_of_integers/component.yaml new file mode 100644 index 0000000..0cb2728 --- /dev/null +++ b/components/json/Build_list_of_integers/component.yaml @@ -0,0 +1,108 @@ +name: Build list of integers +description: Creates a JSON array from multiple integer numbers. +metadata: + annotations: {author: Alexey Volkov , 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 + """ + 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}