Beispiel #1
0
def rename_df_column(df, workflow, old_name, new_name):
    """
    Function to change the name of a column in the dataframe.

    :param df: dataframe
    :param workflow: workflow object that is handling the data frame
    :param old_name: old column name
    :param new_name: new column name
    :return: Workflow object updated
    """

    # Rename the appearances of the variable in all conditions/filters
    conditions = Condition.objects.filter(action__workflow=workflow)
    for cond in conditions:
        cond.formula = formula_evaluation.rename_variable(
            cond.formula, old_name, new_name)
        cond.save()

    # Rename the appearances of the variable in all actions
    for action_item in Action.objects.filter(workflow=workflow):
        action_item.rename_variable(old_name, new_name)

    # Rename the appearances of the variable in the formulas in the views
    for view in View.objects.filter(workflow=workflow):
        view.formula = formula_evaluation.rename_variable(
            view.formula, old_name, new_name)
        view.save()

    return df.rename(columns={old_name: new_name})
Beispiel #2
0
    def test_rename_variable_true(self):

        self.assertTrue(self.compare(self.formula1, self.formula2))

        f3 = formula_evaluation.rename_variable(self.formula1, 'Course_Code_a',
                                                'Course_Code_b')

        self.assertTrue(self.compare(self.formula3, f3))

        f3 = formula_evaluation.rename_variable(self.formula1, 'Course_Code_b',
                                                'Course_Code_a')
Beispiel #3
0
    def rename_variable(self, old_name, new_name):
        """
        Function that renames a variable present in the action content
        :param old_name: Old name of the variable
        :param new_name: New name of the variable
        :return: Updates the current object
        """

        if self.is_out:
            # Action out: Need to change name appearances in content
            new_text = var_use_res[0].sub(
                lambda m: '{{ ' +
                          (new_name if m.group('vname') == escape(old_name)
                           else m.group('vname')) + ' }}',
                self.content
            )
            self.content = new_text
        else:
            # Action in: Need to change name appearances in filter
            fcond = self.conditions.filter(is_filter=True).first()
            if fcond:
                fcond.formula = formula_evaluation.rename_variable(
                    fcond.formula, old_name, new_name
                )
                fcond.save()

        self.save()
Beispiel #4
0
    def rename_variable(self, old_name, new_name):
        """
        Function that renames a variable present in the action content
        :param old_name: Old name of the variable
        :param new_name: New name of the variable
        :return: Updates the current object
        """

        if self.action_type == self.PERSONALIZED_TEXT or \
                self.action_type == self.PERSONALIZED_JSON or \
                self.action_type == self.PERSONALIZED_CANVAS_EMAIL:
            # Need to change name appearances in content
            new_text = var_use_res[0].sub(
                lambda m: '{{ ' + (new_name if m.group('vname') == escape(
                    old_name) else m.group('vname')) + ' }}', self.content)
            self.content = new_text
        else:
            # Action in: Need to change name appearances in filter
            fcond = self.get_filter()
            if fcond:
                fcond.formula = formula_evaluation.rename_variable(
                    fcond.formula, old_name, new_name)
                fcond.save()

        self.save()