cic-cli/cic/utils.py

25 lines
845 B
Python
Raw Permalink Normal View History

def object_to_str(obj, keys):
"""Return a string representation of an object."""
s = ""
for key in keys:
value = eval("obj." + key)
key = key.replace("()", "")
2022-04-26 13:12:43 +02:00
if isinstance(value, str):
s += f"{key} = {value}\n"
2022-04-26 13:12:43 +02:00
elif isinstance(value, list):
for idx, vv in enumerate(value):
if not vv:
s += f"{key}[{idx}] = \n"
continue
s += f"{key}[{idx}] = {vv}\n"
2022-04-26 13:12:43 +02:00
elif isinstance(value, dict):
for vv_key in value.keys():
vv_value = value[vv_key]
if not vv_value:
s += f"{key}.{vv_key} = \n"
continue
s += f"{key}.{vv_key} = {vv_value}\n"
else:
s += f"{key} = {str(value)}\n"
return s