1010# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
1111# ANY KIND, either express or implied. See the License for the specific
1212# language governing permissions and limitations under the License.
13+ # pylint: skip-file
1314"""This module contains utilities related to SageMaker JumpStart Hub."""
1415from __future__ import absolute_import
1516
1617import re
17- from typing import Any , Dict
18+ from typing import Any , Dict , List , Optional
1819
1920
2021def camel_to_snake (camel_case_string : str ) -> str :
2122 """Converts camelCaseString or UpperCamelCaseString to snake_case_string."""
2223 snake_case_string = re .sub ("(.)([A-Z][a-z]+)" , r"\1_\2" , camel_case_string )
24+ if "-" in snake_case_string :
25+ # remove any hyphen from the string for accurate conversion.
26+ snake_case_string = snake_case_string .replace ("-" , "" )
2327 return re .sub ("([a-z0-9])([A-Z])" , r"\1_\2" , snake_case_string ).lower ()
2428
2529
@@ -29,20 +33,29 @@ def snake_to_upper_camel(snake_case_string: str) -> str:
2933 return upper_camel_case_string
3034
3135
32- def walk_and_apply_json (json_obj : Dict [Any , Any ], apply ) -> Dict [Any , Any ]:
33- """Recursively walks a json object and applies a given function to the keys."""
36+ def walk_and_apply_json (
37+ json_obj : Dict [Any , Any ], apply , stop_keys : Optional [List [str ]] = ["metrics" ]
38+ ) -> Dict [Any , Any ]:
39+ """Recursively walks a json object and applies a given function to the keys.
40+
41+ stop_keys (Optional[list[str]]): List of field keys that should stop the application function.
42+ Any children of these keys will not have the application function applied to them.
43+ """
3444
3545 def _walk_and_apply_json (json_obj , new ):
3646 if isinstance (json_obj , dict ) and isinstance (new , dict ):
3747 for key , value in json_obj .items ():
3848 new_key = apply (key )
39- if isinstance (value , dict ):
40- new [new_key ] = {}
41- _walk_and_apply_json (value , new = new [new_key ])
42- elif isinstance (value , list ):
43- new [new_key ] = []
44- for item in value :
45- _walk_and_apply_json (item , new = new [new_key ])
49+ if (stop_keys and new_key not in stop_keys ) or stop_keys is None :
50+ if isinstance (value , dict ):
51+ new [new_key ] = {}
52+ _walk_and_apply_json (value , new = new [new_key ])
53+ elif isinstance (value , list ):
54+ new [new_key ] = []
55+ for item in value :
56+ _walk_and_apply_json (item , new = new [new_key ])
57+ else :
58+ new [new_key ] = value
4659 else :
4760 new [new_key ] = value
4861 elif isinstance (json_obj , dict ) and isinstance (new , list ):
@@ -51,6 +64,8 @@ def _walk_and_apply_json(json_obj, new):
5164 new .update (json_obj )
5265 elif isinstance (json_obj , list ) and isinstance (new , list ):
5366 new .append (json_obj )
67+ elif isinstance (json_obj , str ) and isinstance (new , list ):
68+ new .append (json_obj )
5469 return new
5570
5671 return _walk_and_apply_json (json_obj , new = {})
0 commit comments