def to_dict()

in aws_xray_sdk/core/models/entity.py [0:0]


    def to_dict(self):
        """
        Convert Entity(Segment/Subsegment) object to dict
        with required properties that have non-empty values.
        """
        entity_dict = {}

        for key, value in vars(self).items():
            if isinstance(value, bool) or value:
                if key == 'subsegments':
                    # child subsegments are stored as List
                    subsegments = []
                    for subsegment in value:
                        subsegments.append(subsegment.to_dict())
                    entity_dict[key] = subsegments
                elif key == 'cause':
                    if isinstance(self.cause, dict):
                        entity_dict[key] = {}
                        entity_dict[key]['working_directory'] = self.cause['working_directory']
                        # exceptions are stored as List
                        throwables = []
                        for throwable in value['exceptions']:
                            throwables.append(throwable.to_dict())
                        entity_dict[key]['exceptions'] = throwables
                    else:
                        entity_dict[key] = self.cause
                elif key == 'metadata':
                    entity_dict[key] = metadata_to_dict(value)
                elif key != 'sampled' and key != ORIGIN_TRACE_HEADER_ATTR_KEY:
                    entity_dict[key] = value

        return entity_dict