Skip to content

Commit e7702ec

Browse files
committed
Json - Added 45 new components to handle typical list and dict operations
1 parent dde34a4 commit e7702ec

File tree

90 files changed

+4590
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+4590
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from kfp.components import create_component_from_func
2+
3+
4+
def create_dict_from_boolean_value(
5+
key: str,
6+
value: bool,
7+
) -> list:
8+
"""Creates a JSON object from key and value."""
9+
return {key: value}
10+
11+
12+
if __name__ == "__main__":
13+
create_dict_from_boolean_value_op = create_component_from_func(
14+
create_dict_from_boolean_value,
15+
base_image="python:3.10",
16+
output_component_file="component.yaml",
17+
annotations={
18+
"author": "Alexey Volkov <[email protected]>",
19+
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Boolean/component.yaml",
20+
},
21+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Create dict from boolean value
2+
description: Creates a JSON object from key and value.
3+
metadata:
4+
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Boolean/component.yaml'}
5+
inputs:
6+
- {name: key, type: String}
7+
- {name: value, type: Boolean}
8+
outputs:
9+
- {name: Output, type: JsonArray}
10+
implementation:
11+
container:
12+
image: python:3.10
13+
command:
14+
- sh
15+
- -ec
16+
- |
17+
program_path=$(mktemp)
18+
printf "%s" "$0" > "$program_path"
19+
python3 -u "$program_path" "$@"
20+
- |
21+
def create_dict_from_boolean_value(
22+
key,
23+
value,
24+
):
25+
"""Creates a JSON object from key and value."""
26+
return {key: value}
27+
28+
def _deserialize_bool(s) -> bool:
29+
from distutils.util import strtobool
30+
return strtobool(s) == 1
31+
32+
def _serialize_json(obj) -> str:
33+
if isinstance(obj, str):
34+
return obj
35+
import json
36+
def default_serializer(obj):
37+
if hasattr(obj, 'to_struct'):
38+
return obj.to_struct()
39+
else:
40+
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__)
41+
return json.dumps(obj, default=default_serializer, sort_keys=True)
42+
43+
import argparse
44+
_parser = argparse.ArgumentParser(prog='Create dict from boolean value', description='Creates a JSON object from key and value.')
45+
_parser.add_argument("--key", dest="key", type=str, required=True, default=argparse.SUPPRESS)
46+
_parser.add_argument("--value", dest="value", type=_deserialize_bool, required=True, default=argparse.SUPPRESS)
47+
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1)
48+
_parsed_args = vars(_parser.parse_args())
49+
_output_files = _parsed_args.pop("_output_paths", [])
50+
51+
_outputs = create_dict_from_boolean_value(**_parsed_args)
52+
53+
_outputs = [_outputs]
54+
55+
_output_serializers = [
56+
_serialize_json,
57+
58+
]
59+
60+
import os
61+
for idx, output_file in enumerate(_output_files):
62+
try:
63+
os.makedirs(os.path.dirname(output_file))
64+
except OSError:
65+
pass
66+
with open(output_file, 'w') as f:
67+
f.write(_output_serializers[idx](_outputs[idx]))
68+
args:
69+
- --key
70+
- {inputValue: key}
71+
- --value
72+
- {inputValue: value}
73+
- '----output-paths'
74+
- {outputPath: Output}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from kfp.components import create_component_from_func
2+
3+
4+
def create_dict_from_dict_value(
5+
key: str,
6+
value: dict,
7+
) -> list:
8+
"""Creates a JSON object from key and value."""
9+
return {key: value}
10+
11+
12+
if __name__ == "__main__":
13+
create_dict_from_dict_value_op = create_component_from_func(
14+
create_dict_from_dict_value,
15+
base_image="python:3.10",
16+
output_component_file="component.yaml",
17+
annotations={
18+
"author": "Alexey Volkov <[email protected]>",
19+
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Dict/component.yaml",
20+
},
21+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Create dict from dict value
2+
description: Creates a JSON object from key and value.
3+
metadata:
4+
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Dict/component.yaml'}
5+
inputs:
6+
- {name: key, type: String}
7+
- {name: value, type: JsonObject}
8+
outputs:
9+
- {name: Output, type: JsonArray}
10+
implementation:
11+
container:
12+
image: python:3.10
13+
command:
14+
- sh
15+
- -ec
16+
- |
17+
program_path=$(mktemp)
18+
printf "%s" "$0" > "$program_path"
19+
python3 -u "$program_path" "$@"
20+
- |
21+
def create_dict_from_dict_value(
22+
key,
23+
value,
24+
):
25+
"""Creates a JSON object from key and value."""
26+
return {key: value}
27+
28+
def _serialize_json(obj) -> str:
29+
if isinstance(obj, str):
30+
return obj
31+
import json
32+
def default_serializer(obj):
33+
if hasattr(obj, 'to_struct'):
34+
return obj.to_struct()
35+
else:
36+
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__)
37+
return json.dumps(obj, default=default_serializer, sort_keys=True)
38+
39+
import json
40+
import argparse
41+
_parser = argparse.ArgumentParser(prog='Create dict from dict value', description='Creates a JSON object from key and value.')
42+
_parser.add_argument("--key", dest="key", type=str, required=True, default=argparse.SUPPRESS)
43+
_parser.add_argument("--value", dest="value", type=json.loads, required=True, default=argparse.SUPPRESS)
44+
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1)
45+
_parsed_args = vars(_parser.parse_args())
46+
_output_files = _parsed_args.pop("_output_paths", [])
47+
48+
_outputs = create_dict_from_dict_value(**_parsed_args)
49+
50+
_outputs = [_outputs]
51+
52+
_output_serializers = [
53+
_serialize_json,
54+
55+
]
56+
57+
import os
58+
for idx, output_file in enumerate(_output_files):
59+
try:
60+
os.makedirs(os.path.dirname(output_file))
61+
except OSError:
62+
pass
63+
with open(output_file, 'w') as f:
64+
f.write(_output_serializers[idx](_outputs[idx]))
65+
args:
66+
- --key
67+
- {inputValue: key}
68+
- --value
69+
- {inputValue: value}
70+
- '----output-paths'
71+
- {outputPath: Output}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from kfp.components import create_component_from_func
2+
3+
4+
def create_dict_from_float_value(
5+
key: str,
6+
value: float,
7+
) -> list:
8+
"""Creates a JSON object from key and value."""
9+
return {key: value}
10+
11+
12+
if __name__ == "__main__":
13+
create_dict_from_float_value_op = create_component_from_func(
14+
create_dict_from_float_value,
15+
base_image="python:3.10",
16+
output_component_file="component.yaml",
17+
annotations={
18+
"author": "Alexey Volkov <[email protected]>",
19+
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Float/component.yaml",
20+
},
21+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Create dict from float value
2+
description: Creates a JSON object from key and value.
3+
metadata:
4+
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Float/component.yaml'}
5+
inputs:
6+
- {name: key, type: String}
7+
- {name: value, type: Float}
8+
outputs:
9+
- {name: Output, type: JsonArray}
10+
implementation:
11+
container:
12+
image: python:3.10
13+
command:
14+
- sh
15+
- -ec
16+
- |
17+
program_path=$(mktemp)
18+
printf "%s" "$0" > "$program_path"
19+
python3 -u "$program_path" "$@"
20+
- |
21+
def create_dict_from_float_value(
22+
key,
23+
value,
24+
):
25+
"""Creates a JSON object from key and value."""
26+
return {key: value}
27+
28+
def _serialize_json(obj) -> str:
29+
if isinstance(obj, str):
30+
return obj
31+
import json
32+
def default_serializer(obj):
33+
if hasattr(obj, 'to_struct'):
34+
return obj.to_struct()
35+
else:
36+
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__)
37+
return json.dumps(obj, default=default_serializer, sort_keys=True)
38+
39+
import argparse
40+
_parser = argparse.ArgumentParser(prog='Create dict from float value', description='Creates a JSON object from key and value.')
41+
_parser.add_argument("--key", dest="key", type=str, required=True, default=argparse.SUPPRESS)
42+
_parser.add_argument("--value", dest="value", type=float, required=True, default=argparse.SUPPRESS)
43+
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1)
44+
_parsed_args = vars(_parser.parse_args())
45+
_output_files = _parsed_args.pop("_output_paths", [])
46+
47+
_outputs = create_dict_from_float_value(**_parsed_args)
48+
49+
_outputs = [_outputs]
50+
51+
_output_serializers = [
52+
_serialize_json,
53+
54+
]
55+
56+
import os
57+
for idx, output_file in enumerate(_output_files):
58+
try:
59+
os.makedirs(os.path.dirname(output_file))
60+
except OSError:
61+
pass
62+
with open(output_file, 'w') as f:
63+
f.write(_output_serializers[idx](_outputs[idx]))
64+
args:
65+
- --key
66+
- {inputValue: key}
67+
- --value
68+
- {inputValue: value}
69+
- '----output-paths'
70+
- {outputPath: Output}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from kfp.components import create_component_from_func
2+
3+
4+
def create_dict_from_integer_value(
5+
key: str,
6+
value: int,
7+
) -> list:
8+
"""Creates a JSON object from key and value."""
9+
return {key: value}
10+
11+
12+
if __name__ == "__main__":
13+
create_dict_from_integer_value_op = create_component_from_func(
14+
create_dict_from_integer_value,
15+
base_image="python:3.10",
16+
output_component_file="component.yaml",
17+
annotations={
18+
"author": "Alexey Volkov <[email protected]>",
19+
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Integer/component.yaml",
20+
},
21+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Create dict from integer value
2+
description: Creates a JSON object from key and value.
3+
metadata:
4+
annotations: {author: Alexey Volkov <[email protected]>, canonical_location: 'https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_Integer/component.yaml'}
5+
inputs:
6+
- {name: key, type: String}
7+
- {name: value, type: Integer}
8+
outputs:
9+
- {name: Output, type: JsonArray}
10+
implementation:
11+
container:
12+
image: python:3.10
13+
command:
14+
- sh
15+
- -ec
16+
- |
17+
program_path=$(mktemp)
18+
printf "%s" "$0" > "$program_path"
19+
python3 -u "$program_path" "$@"
20+
- |
21+
def create_dict_from_integer_value(
22+
key,
23+
value,
24+
):
25+
"""Creates a JSON object from key and value."""
26+
return {key: value}
27+
28+
def _serialize_json(obj) -> str:
29+
if isinstance(obj, str):
30+
return obj
31+
import json
32+
def default_serializer(obj):
33+
if hasattr(obj, 'to_struct'):
34+
return obj.to_struct()
35+
else:
36+
raise TypeError("Object of type '%s' is not JSON serializable and does not have .to_struct() method." % obj.__class__.__name__)
37+
return json.dumps(obj, default=default_serializer, sort_keys=True)
38+
39+
import argparse
40+
_parser = argparse.ArgumentParser(prog='Create dict from integer value', description='Creates a JSON object from key and value.')
41+
_parser.add_argument("--key", dest="key", type=str, required=True, default=argparse.SUPPRESS)
42+
_parser.add_argument("--value", dest="value", type=int, required=True, default=argparse.SUPPRESS)
43+
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=1)
44+
_parsed_args = vars(_parser.parse_args())
45+
_output_files = _parsed_args.pop("_output_paths", [])
46+
47+
_outputs = create_dict_from_integer_value(**_parsed_args)
48+
49+
_outputs = [_outputs]
50+
51+
_output_serializers = [
52+
_serialize_json,
53+
54+
]
55+
56+
import os
57+
for idx, output_file in enumerate(_output_files):
58+
try:
59+
os.makedirs(os.path.dirname(output_file))
60+
except OSError:
61+
pass
62+
with open(output_file, 'w') as f:
63+
f.write(_output_serializers[idx](_outputs[idx]))
64+
args:
65+
- --key
66+
- {inputValue: key}
67+
- --value
68+
- {inputValue: value}
69+
- '----output-paths'
70+
- {outputPath: Output}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from kfp.components import create_component_from_func
2+
3+
4+
def create_dict_from_list_value(
5+
key: str,
6+
value: list,
7+
) -> list:
8+
"""Creates a JSON object from key and value."""
9+
return {key: value}
10+
11+
12+
if __name__ == "__main__":
13+
create_dict_from_list_value_op = create_component_from_func(
14+
create_dict_from_list_value,
15+
base_image="python:3.10",
16+
output_component_file="component.yaml",
17+
annotations={
18+
"author": "Alexey Volkov <[email protected]>",
19+
"canonical_location": "https://raw.githubusercontent.com/Ark-kun/pipeline_components/master/components/json/Dict/Create/from_List/component.yaml",
20+
},
21+
)

0 commit comments

Comments
 (0)