Exemple #1
0
def calculate(tax_benefit_system, input_data):
    simulation = SimulationBuilder().build_from_entities(tax_benefit_system, input_data)

    requested_computations = dpath.util.search(input_data, '*/*/*/*', afilter = lambda t: t is None, yielded = True)
    computation_results = {}

    for computation in requested_computations:
        path = computation[0]
        entity_plural, entity_id, variable_name, period = path.split('/')
        variable = tax_benefit_system.get_variable(variable_name)
        result = simulation.calculate(variable_name, period)
        entity = simulation.get_entity(plural = entity_plural)
        entity_index = entity.ids.index(entity_id)

        if variable.value_type == Enum:
            entity_result = result.decode()[entity_index].name
        elif variable.value_type == float:
            entity_result = float(str(result[entity_index]))  # To turn the float32 into a regular float without adding confusing extra decimals. There must be a better way.
        elif variable.value_type == str:
            entity_result = to_unicode(result[entity_index])  # From bytes to unicode
        else:
            entity_result = result.tolist()[entity_index]

        dpath.util.new(computation_results, path, entity_result)

    dpath.merge(input_data, computation_results)

    return input_data
def test_get_entity_not_found():
    simulation = SimulationBuilder().build_default_simulation(
        tax_benefit_system)
    assert simulation.get_entity(plural="no_such_entities") is None
def test_get_entity_not_found():
    simulation = SimulationBuilder().build_default_simulation(tax_benefit_system)
    assert simulation.get_entity(plural = "no_such_entities") is None