MCPcopy
hub / github.com/apple/ml-4m / string_to_list

Function string_to_list

run_generation.py:309–334  ·  view source on GitHub ↗

Convert a string separated by hyphens into a list of a given data type, replacing invalid values with None. Args: input_string (str): The input string to convert. dtype (type): The target data type for conversion. Default is float. delim (str): The delimite

(input_string, dtype=float, delim='-')

Source from the content-addressed store, hash-verified

307 return caption[:len(cap_trunc)]
308
309def string_to_list(input_string, dtype=float, delim='-'):
310 """
311 Convert a string separated by hyphens into a list of a given data type,
312 replacing invalid values with None.
313
314 Args:
315 input_string (str): The input string to convert.
316 dtype (type): The target data type for conversion. Default is float.
317 delim (str): The delimiter used to separate values in the string. Default is '-'.
318
319 Returns:
320 list: A list of values in the specified data type or None for invalid values.
321 """
322 if input_string is None:
323 return [None]
324
325 if isinstance(input_string, float) or isinstance(input_string, int):
326 return [input_string]
327
328 def try_cast(item, dtype):
329 try:
330 return dtype(item)
331 except ValueError:
332 return None
333
334 return [try_cast(item, dtype) for item in input_string.split(delim)]
335
336def repeat_if_necessary(lst, n):
337 return lst * n if len(lst) == 1 else lst

Callers 1

mainFunction · 0.85

Calls 1

try_castFunction · 0.85

Tested by

no test coverage detected