Nice Python function

Nice Python function

This python function calculates the current value of a parameter which depends on the previous value of the same parameter but the current values of other parameters. Use, like and leave a comment.
# Let the zeroth value of some_parameter = 3
# Say we have the following relationship
"""
some_parameter{current} = some_parameter{previous} + parameter_2{current} *
(some_parameter{previous} + parameter_1{current})
"""

# column to store result
new_column = 'some_parameter'

# decorator function
def decorator_function(formula_function):
    prev_row = {}
    def wrapper(curr_row, **kwargs):
        value = formula_function(curr_row, prev_row)
        prev_row.update(curr_row)
        prev_row[new_column] = value
        return value
    return wrapper

@decorator_function
def some_parameter_computation(curr_row, prev_row):
    return round(prev_row.get('some_parameter', 3) +\
                 curr_row['parameter_2'] * (prev_row.get('some_parameter', 3) -\
                                            curr_row['parameter_1']), 2)
11
1

Comments

  • 1223213

    Oct 09

    What is your thoughts?