|
import re |
|
from typing import Dict, List, Union |
|
|
|
|
|
def product_data_to_str(product_data: Dict[str, Union[str, List[str]]]) -> 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 = ( |
|
";" |
|
) |
|
|
|
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) |
|
|
|
|
|
def to_snake_case(s): |
|
|
|
s = s.strip().lower() |
|
|
|
s = re.sub(r'[\s\-\.\+]', '_', s) |
|
|
|
s = re.sub(r'[^a-z0-9_]', '', s) |
|
|
|
s = re.sub(r'_+', '_', s) |
|
|
|
s = re.sub(r'^[0-9]+', '', s) |
|
|
|
if not s or not s[0].isalpha(): |
|
s = 'var_' + s |
|
return s |
|
|