Represent `obj`, a `dbcore.Model` object, as a dictionary for serialization. Only include the given `fields` if provided; otherwise, include everything. The resulting dictionary's keys are strings and the values are safely YAML-serializable types.
(obj, fields)
| 96 | |
| 97 | |
| 98 | def flatten(obj, fields): |
| 99 | """Represent `obj`, a `dbcore.Model` object, as a dictionary for |
| 100 | serialization. Only include the given `fields` if provided; |
| 101 | otherwise, include everything. |
| 102 | |
| 103 | The resulting dictionary's keys are strings and the values are |
| 104 | safely YAML-serializable types. |
| 105 | """ |
| 106 | # Format each value. |
| 107 | d = {} |
| 108 | for key in obj.keys(): |
| 109 | value = obj[key] |
| 110 | if _safe_value(obj, key, value): |
| 111 | # A safe value that is faithfully representable in YAML. |
| 112 | d[key] = value |
| 113 | else: |
| 114 | # A value that should be edited as a string. |
| 115 | d[key] = obj.formatted()[key] |
| 116 | |
| 117 | # Possibly filter field names. |
| 118 | if fields: |
| 119 | return {k: v for k, v in d.items() if k in fields} |
| 120 | return d |
| 121 | |
| 122 | |
| 123 | def apply_(obj, data): |
no test coverage detected