MCPcopy Create free account
hub / github.com/datapane/datapane / conv_attrib

Function conv_attrib

python-client/src/datapane/common/viewxml_utils.py:52–74  ·  view source on GitHub ↗

Convert a value to a str for use as an ElementBuilder attribute - also handles None to a string for optional field values

(v: t.Any)

Source from the content-addressed store, hash-verified

50
51
52def conv_attrib(v: t.Any) -> t.Optional[str]:
53 """
54 Convert a value to a str for use as an ElementBuilder attribute
55 - also handles None to a string for optional field values
56 """
57 # TODO - use a proper serialisation framework here / lxml features
58 if v is None:
59 return v
60 elif isinstance(v, Sized) and len(v) == 0:
61 return None
62 elif isinstance(v, str):
63 return v
64 elif isinstance(v, Number) and type(v) != bool:
65 if math.isinf(v) and v > 0:
66 return "INF"
67 elif math.isinf(v) and v < 0:
68 return "-INF"
69 elif math.isnan(v):
70 return "NaN"
71 else:
72 return str(v)
73 else:
74 return json.dumps(v)
75
76
77def mk_attribs(**attribs: t.Any) -> SSDict:

Callers 1

mk_attribsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected