Spaces:
Sleeping
Sleeping
| def product_data_to_str(product_data: dict[str, any]) -> str: | |
| """ | |
| Convert product data to a string. | |
| Args: | |
| - product_data: a dictionary of product data | |
| Returns: | |
| - a string representation of the product data | |
| """ | |
| if product_data is None: | |
| return "" | |
| data_list = [] | |
| for k, v in product_data.items(): | |
| data_line = None | |
| if isinstance(v, list): | |
| delimiter = "," | |
| for sub_v in v: | |
| if "," in sub_v: | |
| delimiter = ( | |
| ";" # Use semicolon as delimiter if comma is found in the value | |
| ) | |
| for i, sub_v in enumerate(v): | |
| if ";" in sub_v: | |
| v[i] = f'"{sub_v}"' | |
| data_line = f"{k}: {f'{delimiter} '.join(v)}" | |
| elif isinstance(v, str): | |
| data_line = f"{k}: {v}" | |
| else: | |
| raise ValueError( | |
| f"Unsupported data type for value of product data: {type(v)}. Only list and str are supported." | |
| ) | |
| data_list.append(data_line) | |
| return "\n".join(data_list) | |