Exemple #1
0
    def ii_push_record(self, in_record: Sdk.RecordRef) -> bool:
        self.Record = in_record
        group_by_value = ''
        if self.GroupByField is not None:
            group_by_value = self.GroupByField.get_as_string(in_record)

        if group_by_value != self.PriorGroupByValue:
            self.CurrentValue = self.InitialValue

        self.PriorGroupByValue = group_by_value
        self.Creator.reset()
        self.Copier.copy(self.Creator, in_record)
        try:
            formula = self.FormulaField.get_as_string(in_record)
            if self.Formula != formula:
                self.Visitor = FormulaVisitor(expression=formula,
                                              fields=self.Fields)
                self.Formula = formula
            self.CurrentValue = self.Visitor.calculate()

            success = set_field(self.CalcField, self.Creator,
                                self.CurrentValue)
            if not success:
                self.parent.display_error_msg(
                    "Invalid data type: {datatype}".format(
                        datatype=self.parent.DataType))
                return False

            data = self.Creator.finalize_record()
            self.parent.Output.push_record(data)
            return True
        except Exception as ex:
            self.parent.display_error_msg(str(ex))
            return False
Exemple #2
0
 def test_missing_field(self):
     fields = {
         'field1': lambda: 10
     }
     expression = '[field2]'
     visitor = FormulaVisitor(expression=expression, fields=fields)
     with self.assertRaises(MissingFieldException) as ex:
         visitor.calculate()
         print(ex)
Exemple #3
0
    def ii_init(self, record_info_in: Sdk.RecordInfo) -> bool:
        self.IncomingInfo = record_info_in
        if self.parent.GroupByField is not None and self.parent.GroupByField != '':
            self.GroupByField = self.IncomingInfo.get_field_by_name(
                self.parent.GroupByField)
        self.Info = self.IncomingInfo.clone()
        if self.parent.DataType == 'String':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.v_wstring,
                                                 1073741823, 0)
        elif self.parent.DataType == 'Integer':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.int64, 8, 0)
        elif self.parent.DataType == 'Decimal':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.double, 8, 0)
        elif self.parent.DataType == 'Date':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.date, 10, 0)
        elif self.parent.DataType == 'Datetime':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.datetime, 19, 0)
        else:
            self.parent.display_error_msg(
                "Invalid data type: {datatype}".format(
                    datatype=self.parent.DataType))
            return False

        self.Creator = self.Info.construct_record_creator()
        self.Copier = Sdk.RecordCopier(self.Info, self.IncomingInfo)
        index = 0
        while index < self.IncomingInfo.num_fields:
            self.Copier.add(index, index)
            field = self.IncomingInfo[index]
            self.Fields[field.name] = generate_getter(self, field)
            index += 1
        self.Copier.done_adding()
        self.parent.Output.init(self.Info)
        try:
            self.InitialValue = calculate(expression=self.parent.InitialValue,
                                          fields={})
            self.CurrentValue = self.InitialValue
            self.Fields[self.parent.FieldName] = lambda: self.CurrentValue
            self.Visitor = FormulaVisitor(expression=self.parent.Formula,
                                          fields=self.Fields)
            return True
        except Exception as ex:
            self.parent.display_error_msg(str(ex))
            return False
Exemple #4
0
 def test_field_expressions_invalid_syntax(self):
     fields = {
         'Field1': lambda: 10
     }
     expression = '[Field1] * (2 + [Field1]'
     with self.assertRaises(Exception) as ex:
         FormulaVisitor(expression=expression, fields=fields)
         print(ex)
Exemple #5
0
class IncomingInterface:
    def __init__(self, parent: AyxPlugin):
        # Default properties
        self.parent: AyxPlugin = parent

        # Custom properties
        self.Info: Sdk.RecordInfo = None
        self.IncomingInfo: Sdk.RecordInfo = None
        self.Copier: Sdk.RecordCopier = None
        self.Creator: Sdk.RecordCreator = None
        self.Record: Sdk.RecordRef = None
        self.Fields: Dict[str, Callable] = {}
        self.Visitor: FormulaVisitor = None
        self.CurrentValue = None
        self.CalcField: Sdk.Field = None
        self.FormulaField: Sdk.Field = None
        self.GroupByField: Sdk.Field = None
        self.PriorGroupByValue = ''
        self.InitialValue = None
        self.Formula = ''

    def ii_init(self, record_info_in: Sdk.RecordInfo) -> bool:
        self.IncomingInfo = record_info_in
        self.FormulaField = self.IncomingInfo.get_field_by_name(
            self.parent.FormulaField)
        if self.parent.GroupByField is not None and self.parent.GroupByField != '':
            self.GroupByField = self.IncomingInfo.get_field_by_name(
                self.parent.GroupByField)
        self.Info = self.IncomingInfo.clone()
        if self.parent.DataType == 'String':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.v_wstring,
                                                 1073741823, 0)
        elif self.parent.DataType == 'Integer':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.int64, 8, 0)
        elif self.parent.DataType == 'Decimal':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.double, 8, 0)
        elif self.parent.DataType == 'Date':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.date, 10, 0)
        elif self.parent.DataType == 'Datetime':
            self.CalcField = self.Info.add_field(self.parent.FieldName,
                                                 Sdk.FieldType.datetime, 19, 0)
        else:
            self.parent.display_error_msg(
                "Invalid data type: {datatype}".format(
                    datatype=self.parent.DataType))
            return False

        self.Creator = self.Info.construct_record_creator()
        self.Copier = Sdk.RecordCopier(self.Info, self.IncomingInfo)
        index = 0
        while index < self.IncomingInfo.num_fields:
            self.Copier.add(index, index)
            field = self.IncomingInfo[index]
            self.Fields[field.name] = generate_getter(self, field)
            index += 1
        self.Copier.done_adding()
        self.parent.Output.init(self.Info)
        try:
            self.InitialValue = calculate(expression=self.parent.InitialValue,
                                          fields={})
            self.CurrentValue = self.InitialValue
            self.Fields[self.parent.FieldName] = lambda: self.CurrentValue
            return True
        except Exception as ex:
            self.parent.display_error_msg(str(ex))
            return False

    def ii_push_record(self, in_record: Sdk.RecordRef) -> bool:
        self.Record = in_record
        group_by_value = ''
        if self.GroupByField is not None:
            group_by_value = self.GroupByField.get_as_string(in_record)

        if group_by_value != self.PriorGroupByValue:
            self.CurrentValue = self.InitialValue

        self.PriorGroupByValue = group_by_value
        self.Creator.reset()
        self.Copier.copy(self.Creator, in_record)
        try:
            formula = self.FormulaField.get_as_string(in_record)
            if self.Formula != formula:
                self.Visitor = FormulaVisitor(expression=formula,
                                              fields=self.Fields)
                self.Formula = formula
            self.CurrentValue = self.Visitor.calculate()

            success = set_field(self.CalcField, self.Creator,
                                self.CurrentValue)
            if not success:
                self.parent.display_error_msg(
                    "Invalid data type: {datatype}".format(
                        datatype=self.parent.DataType))
                return False

            data = self.Creator.finalize_record()
            self.parent.Output.push_record(data)
            return True
        except Exception as ex:
            self.parent.display_error_msg(str(ex))
            return False

    def ii_update_progress(self, d_percent: float):
        # Inform the Alteryx engine of the tool's progress.
        self.parent.alteryx_engine.output_tool_progress(
            self.parent.n_tool_id, d_percent)

    def ii_close(self):
        self.parent.Output.assert_close()
        return