sagemaker-core 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sagemaker-core might be problematic. Click here for more details.

File without changes
@@ -0,0 +1,11 @@
1
+ import os
2
+
3
+ script_dir = os.path.dirname(os.path.abspath(__file__))
4
+
5
+ # Get the root directory of the project
6
+ root_dir = os.path.abspath(os.path.join(script_dir, "..", ".."))
7
+
8
+ version_file_path = os.path.join(root_dir, "VERSION")
9
+
10
+ with open(version_file_path) as version_file:
11
+ __version__ = version_file.read().strip()
File without changes
@@ -0,0 +1,42 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+ import os
14
+ import boto3
15
+ from botocore.config import Config
16
+
17
+
18
+ class Base:
19
+ def __init__(self, session=None, region=None):
20
+ aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
21
+ aws_secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY")
22
+ aws_session_token = os.getenv("AWS_SESSION_TOKEN")
23
+ profile_name = os.getenv("AWS_PROFILE")
24
+
25
+ if session is None:
26
+ if all([aws_access_key_id, aws_secret_access_key, aws_session_token]):
27
+ self.session = boto3.Session(
28
+ aws_access_key_id=aws_access_key_id,
29
+ aws_secret_access_key=aws_secret_access_key,
30
+ aws_session_token=aws_session_token,
31
+ )
32
+ elif profile_name:
33
+ self.session = boto3.Session(profile_name=profile_name)
34
+ else:
35
+ self.session = boto3.Session()
36
+
37
+ self.region = region if region else os.getenv("AWS_REGION")
38
+
39
+ # Create a custom config with the user agent
40
+ custom_config = Config(region_name=self.region, user_agent_extra="SageMakerSDK/3.0")
41
+
42
+ self.client = self.session.client("sagemaker", config=custom_config)
@@ -0,0 +1,241 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+ import logging
14
+
15
+ from dataclasses import asdict
16
+ import re
17
+
18
+ from sagemaker_core.code_injection.shape_dag import SHAPE_DAG
19
+ from sagemaker_core.code_injection.constants import (
20
+ BASIC_TYPES,
21
+ STRUCTURE_TYPE,
22
+ LIST_TYPE,
23
+ MAP_TYPE,
24
+ )
25
+
26
+
27
+ def pascal_to_snake(pascal_str):
28
+ """
29
+ Converts a PascalCase string to snake_case.
30
+
31
+ Args:
32
+ pascal_str (str): The PascalCase string to be converted.
33
+
34
+ Returns:
35
+ str: The converted snake_case string.
36
+ """
37
+ snake_case = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", pascal_str)
38
+ return re.sub("([a-z0-9])([A-Z])", r"\1_\2", snake_case).lower()
39
+
40
+
41
+ def deserialize(data, cls) -> object:
42
+ """
43
+ Deserialize the given data into an instance of the specified class.
44
+
45
+ Args:
46
+ data (dict): The data to be deserialized.
47
+ cls (str or type): The class or class name to deserialize into.
48
+
49
+ Returns:
50
+ object: An instance of the specified class with the deserialized data.
51
+ """
52
+ # Convert the keys to snake_case
53
+ logging.debug(f"Deserialize: pascal cased data: {data}")
54
+ data = {pascal_to_snake(k): v for k, v in data.items()}
55
+ logging.debug(f"Deserialize: snake cased data: {data}")
56
+
57
+ # Get the class from the cls_name string
58
+ if type(cls) == str:
59
+ cls = globals()[cls]
60
+
61
+ # Create a new instance of the class
62
+ instance = cls(**data)
63
+
64
+ return instance
65
+
66
+
67
+ def snake_to_pascal(snake_str):
68
+ """
69
+ Convert a snake_case string to PascalCase.
70
+
71
+ Args:
72
+ snake_str (str): The snake_case string to be converted.
73
+
74
+ Returns:
75
+ str: The PascalCase string.
76
+
77
+ """
78
+ components = snake_str.split("_")
79
+ return "".join(x.title() for x in components[0:])
80
+
81
+
82
+ def serialize(data) -> object:
83
+ """
84
+ Serializes the given data object into a dictionary.
85
+
86
+ Args:
87
+ data: The data object to be serialized.
88
+
89
+ Returns:
90
+ A dictionary containing the serialized data.
91
+
92
+ """
93
+ data_dict = asdict(data)
94
+
95
+ # Convert the keys to pascalCase
96
+ data_dict = {snake_to_pascal(k): v for k, v in data_dict.items() if v is not None}
97
+
98
+ return data_dict
99
+
100
+
101
+ def _evaluate_list_type(raw_list, shape) -> list:
102
+ """
103
+ Evaluates a list type based on the given shape.
104
+
105
+ Args:
106
+ raw_list (list): The raw list to be evaluated.
107
+ shape (dict): The shape of the list.
108
+
109
+ Returns:
110
+ list: The evaluated list based on the shape.
111
+
112
+ Raises:
113
+ ValueError: If an unhandled list member type is encountered.
114
+
115
+ """
116
+ _shape_member_type = shape["member_type"]
117
+ _shape_member_shape = shape["member_shape"]
118
+ if _shape_member_type in BASIC_TYPES:
119
+ # if basic types directly assign list value.
120
+ _evaluated_list = raw_list
121
+ elif _shape_member_type == STRUCTURE_TYPE:
122
+ # if structure type transform each list item and assign value.
123
+ _evaluated_list = []
124
+ # traverse through response list and evaluate item
125
+ for item in raw_list:
126
+ _evaluated_item = transform(item, _shape_member_shape)
127
+ _evaluated_list.append(_evaluated_item)
128
+ else:
129
+ raise ValueError(
130
+ f"Unhandled List member type "
131
+ f"[{_shape_member_type}] encountered. "
132
+ "Needs additional logic for support"
133
+ )
134
+ return _evaluated_list
135
+
136
+
137
+ def _evaluate_map_type(raw_map, shape) -> dict:
138
+ """
139
+ Evaluates a map type based on the given shape.
140
+
141
+ Args:
142
+ raw_map (dict): The raw map to be evaluated.
143
+ shape (dict): The shape of the map.
144
+
145
+ Returns:
146
+ dict: The evaluated map.
147
+
148
+ Raises:
149
+ ValueError: If an unhandled map key type or list member type is encountered.
150
+ """
151
+ _shape_key_type = shape["key_type"]
152
+ _shape_value_type = shape["value_type"]
153
+ _shape_value_shape = shape["value_shape"]
154
+ if _shape_key_type != "string":
155
+ raise ValueError(
156
+ f"Unhandled Map key type "
157
+ f"[{_shape_key_type}] encountered. "
158
+ "Needs additional logic for support"
159
+ )
160
+
161
+ _evaluated_map = {}
162
+ if _shape_value_type in BASIC_TYPES:
163
+ # if basic types directly assign value.
164
+ # Ex. response["map_member"] = {"key":"value"}
165
+ _evaluated_map = raw_map
166
+ elif _shape_value_type == STRUCTURE_TYPE:
167
+ # if structure type loop through and evaluate values
168
+ for k, v in raw_map.items():
169
+ _evaluated_value = transform(v, _shape_value_shape)
170
+ _evaluated_map[k] = _evaluated_value
171
+ elif _shape_value_type == LIST_TYPE:
172
+ for k, v in raw_map.items():
173
+ _list_type_shape = SHAPE_DAG[_shape_value_shape]
174
+ evaluated_values = _evaluate_list_type(v, _list_type_shape)
175
+ _evaluated_map[k] = evaluated_values
176
+ elif _shape_value_type == MAP_TYPE:
177
+ for k, v in raw_map.items():
178
+ _map_type_shape = SHAPE_DAG[_shape_value_shape]
179
+ evaluated_values = _evaluate_map_type(v, _map_type_shape)
180
+ _evaluated_map[k] = evaluated_values
181
+ else:
182
+ raise ValueError(
183
+ f"Unhandled List member type "
184
+ f"[{_shape_value_type}] encountered. "
185
+ "Needs additional logic for support"
186
+ )
187
+
188
+ return _evaluated_map
189
+
190
+
191
+ def transform(data, shape, object_instance=None) -> dict:
192
+ """
193
+ Transforms the given data based on the given shape.
194
+
195
+ Args:
196
+ data (dict): The data to be transformed.
197
+ shape (str): The shape of the data.
198
+ object_instance (object): The object to be transformed. (Optional)
199
+
200
+ Returns:
201
+ dict: The transformed data.
202
+
203
+ Raises:
204
+ ValueError: If an unhandled shape type is encountered.
205
+ """
206
+ result = {}
207
+ _shape = SHAPE_DAG[shape]
208
+
209
+ if _shape["type"] in BASIC_TYPES:
210
+ raise ValueError("Unexpected low-level operation model shape")
211
+
212
+ for member in _shape["members"]:
213
+ _member_name = member["name"]
214
+ _member_shape = member["shape"]
215
+ _member_type = member["type"]
216
+ if data.get(_member_name) is None:
217
+ # skip members that are not in the response
218
+ continue
219
+ # 1. set snake case attribute name
220
+ attribute_name = pascal_to_snake(_member_name)
221
+ # 2. assign response value
222
+ if _member_type in BASIC_TYPES:
223
+ evaluated_value = data[_member_name]
224
+ elif _member_type == STRUCTURE_TYPE:
225
+ evaluated_value = transform(data[_member_name], _member_shape)
226
+ elif _member_type == LIST_TYPE:
227
+ _list_type_shape = SHAPE_DAG[_member_shape]
228
+ # 2. assign response value
229
+ evaluated_value = _evaluate_list_type(data[_member_name], _list_type_shape)
230
+ elif _member_type == MAP_TYPE:
231
+ _map_type_shape = SHAPE_DAG[_member_shape]
232
+ evaluated_value = _evaluate_map_type(data[_member_name], _map_type_shape)
233
+ else:
234
+ raise ValueError(f"Unexpected member type encountered: {_member_type}")
235
+
236
+ result[attribute_name] = evaluated_value
237
+ if object_instance:
238
+ # 3. set attribute value
239
+ setattr(object_instance, attribute_name, evaluated_value)
240
+
241
+ return result
@@ -0,0 +1,18 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+ """Constants used in the code_injection modules."""
14
+
15
+ BASIC_TYPES = ["string", "boolean", "integer", "long", "double", "timestamp", "float"]
16
+ STRUCTURE_TYPE = "structure"
17
+ MAP_TYPE = "map"
18
+ LIST_TYPE = "list"