def try_create_attribute_instance(self, parrent_object, data, label):
        data_as_type = data_type_transform.transform_data_type(data)
        data_type = type(data_as_type)  # if data_as_type is not None else None
        # create the attribute with the parrent
        # label since its a list of values
        # we dont know what it is called

        if parrent_object is not None:
            label = label or parrent_object.base.label

            att = SchemaAttribute(
                label=label,
                data_type=SchemaAttribute.data_type_map[data_type],
                object=self.do_meta_item_exists(parrent_object.base))

            att = self.do_meta_item_exists(att)

            # Create the instance
            if att is not None and data_as_type is not None:
                AttributeInstance = self.att_to_att_inst(att)
                att_inst = AttributeInstance(base=att,
                                             value=data_as_type,
                                             object=parrent_object)
                try:
                    att_inst.save()
                    self.added_instance_items.append(att_inst)
                    return att_inst
                except Exception as e:
                    logger.error("object_create_" + str(e))
            else:
                self.unmapped_objects.append(
                    UnmappedObject(label=label,
                                   parrent_label=parrent_object.base.label,
                                   childrens=data_as_type))
Exemple #2
0
 def get_or_create_fieldnames(self, rows):
     for n, row in enumerate(rows):
         transformed_row = [transform_data_type(cell) for cell in row]
         is_header = all(isinstance(cell, str) for cell in transformed_row)
         if is_header:
             return row, n + 1
         elif n > 10:
             return self.build_fieldnames(transformed_row), 0
    def handle_attributes(self, parrent_object: Node,
                          data, label: str):
        if parrent_object is None:
            return None

        # TODO handle list structures better fix for when list with values
        label = label or parrent_object.base.label

        att = SchemaAttribute.exists_by_label(label, parrent_object.base.label)

        # TODO handle if the data of an attribute is a dict
        if isinstance(data, dict):
            unit = data.get('unit')
            data = data.get('value')

        # identify datatype
        data_as_type = data_type_transform.transform_data_type(data)
        data_type = type(data_as_type)

        if att is None:
            att = SchemaAttribute(
                label=label,
                data_type=SchemaAttribute.data_type_map[data_type],
                object=parrent_object.base
            )
            if not self.save_obj(att):
                return None

        # get the corresponding attribute type class
        AttributeInstance = DictUtils.inverse_dict(
            BaseAttribute.att_inst_to_type_map,
            data_type
        )

        # check if an attribute of such type exists
        att_inst = AttributeInstance.exists_by_label(
            label, parrent_object.pk, data_as_type)

        if att_inst is not None:
            return att_inst
        else:
            # Create the instance
            att_inst = AttributeInstance(
                base=att,
                value=data_as_type,
                object=parrent_object,
                owner=self.owner
            )

            return self.save_obj(att_inst)
    def try_create_attribute(self, parrent_object, data, label):

        data_as_type = data_type_transform.transform_data_type(data)
        data_type = type(data_as_type)  # if data_as_type is not None else None

        if parrent_object is not None:
            label = label or parrent_object.label

            att = SchemaAttribute(
                label=label,
                data_type=SchemaAttribute.data_type_map[data_type],
                object=BaseMetaDataService.do_meta_item_exists(parrent_object))
            try:
                return self._try_create_meta_item(att)
            except Exception as e:
                logger.error("object_create_" + str(e))
    def test_identify_data_type(self):
        input_vs_expected = [
            ("20.40", float),
            (2.4, float),
            ("2012-01-19 17:21:00", datetime),
            ('2018-10-02T16: 16: 49Z', datetime),
            # ("2019-20-04:20:30:59", datetime),
            (2, int),
            ("1", int),
            ("test", str),
            ("False", bool),
            (True, bool),
            (None, type(None))
        ]

        inputd, expected = zip(*input_vs_expected)

        resp = [
            type(data_type_transform.transform_data_type(elm))
            for elm in inputd
        ]

        self.assertListEqual(list(resp), list(expected))
 def get_data_type(self, value):
     value = data_type_transform.transform_data_type(value)
     return type(value)
 def handle_attribute(self, object_ref: Model, attribute_label: str, data):
     attribute_label, data = dm_utils.pre_handle_atts_if_list_and_specific_labels(
         attribute_label, data)
     data = data_type_transform.transform_data_type(data)
     self.try_set_attribute(attribute_label, data, object_ref)