MCPcopy Index your code
hub / github.com/STIXProject/python-stix / to_dict

Function to_dict

stix/utils/__init__.py:289–328  ·  view source on GitHub ↗

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=())

Source from the content-addressed store, hash-verified

287
288@silence_warnings
289def 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
331def xml_bool(value):

Callers

nothing calls this directly

Calls 9

iter_varsFunction · 0.85
key_nameFunction · 0.85
has_valueFunction · 0.85
is_dictableFunction · 0.85
is_timestampFunction · 0.85
is_dateFunction · 0.85
is_sequenceFunction · 0.85
dict_iterFunction · 0.85
to_dictMethod · 0.45

Tested by

no test coverage detected