Returns a dictionary representation of `entity`. This will iterate over the instance vars of `entity` and construct keys and values from those variable names and values. Args: entity: A ``Entity`` object. skip: An iterable containing keys to exclude from the dictionary.
(entity, skip=())
| 287 | |
| 288 | @silence_warnings |
| 289 | def to_dict(entity, skip=()): |
| 290 | """Returns a dictionary representation of `entity`. This will iterate over |
| 291 | the instance vars of `entity` and construct keys and values from those |
| 292 | variable names and values. |
| 293 | |
| 294 | Args: |
| 295 | entity: A ``Entity`` object. |
| 296 | skip: An iterable containing keys to exclude from the dictionary. These |
| 297 | should be the dictionary key names, and not the instance variable |
| 298 | name (e.g., 'id' and NOT 'id_'). |
| 299 | |
| 300 | Returns: |
| 301 | A dictionary representation of the input `entity`. |
| 302 | |
| 303 | """ |
| 304 | def dict_iter(items): |
| 305 | return [x.to_dict() if is_dictable(x) else x for x in items] |
| 306 | |
| 307 | |
| 308 | d = {} |
| 309 | for name, field in iter_vars(entity): |
| 310 | key = key_name(name) |
| 311 | |
| 312 | if key in skip or not has_value(field): |
| 313 | continue |
| 314 | |
| 315 | if is_dictable(field): |
| 316 | d[key] = field.to_dict() |
| 317 | elif is_timestamp(field): |
| 318 | d[key] = dates.serialize_value(field) |
| 319 | elif is_date(field): |
| 320 | d[key] = dates.serialize_date(field) |
| 321 | elif mixbox.xml.is_element(field) or mixbox.xml.is_etree(field): |
| 322 | d[key] = lxml.etree.tostring(field) |
| 323 | elif is_sequence(field): |
| 324 | d[key] = dict_iter(field) |
| 325 | else: |
| 326 | d[key] = field |
| 327 | |
| 328 | return d |
| 329 | |
| 330 | |
| 331 | def xml_bool(value): |
nothing calls this directly
no test coverage detected