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)
| 50 | |
| 51 | |
| 52 | def 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 | |
| 77 | def mk_attribs(**attribs: t.Any) -> SSDict: |