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) population = simulation.get_population(entity_plural) entity_index = population.get_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 = str(result[entity_index]) 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 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) population = simulation.get_population(entity_plural) entity_index = population.get_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 = str(result[entity_index]) 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 _get_agents_statistics(cls, company_id: str) -> Sequence[dict]: result = cls._get_resource_stats_per_agent(company_id, key="resources") dpath.merge( result, cls._get_experiments_stats_per_agent(company_id, key="experiments")) return [{ "uuid": agent_id, **data } for agent_id, data in result.items()]
def load_config(directory="config"): """ Returns a dictionary with the contents of all JSON files found in the specified `directory`. Raises a configuration error if the directory is not found, a file is not readable, or a file cannot be parsed. """ config = {} for root, directories, files in os.walk(directory): for file in files: base, ext = os.path.splitext(file) if ext == ".json": fragment = _load_config_file(os.path.join(root, file)) dpath.merge(config, fragment) return util.Configuration(config)
def upsert(self, key: str, value: Union[List[Any], dict]) -> dict: """Update or insert values into key list or dict. Args: key: Key to value to upsert. value: Value to upsert by. Returns: Updated config. """ to_update = deepcopy(self.get(key, value)) dpath.merge(to_update, value, flags=dpath.MERGE_REPLACE) self.add(key, to_update) return self.sync()
def extend(self, key: str, value: List[Any], unique: bool = False) -> dict: """Extend a list in config at key path. Args: key: Key to path to extend. value: List of values to extend by. unique: Only extend values if not already in values. Returns: Updated Config """ to_update = list(deepcopy(self.get(key, value))) if unique: value = [v for v in value if v not in to_update] dpath.merge(to_update, value, flags=dpath.MERGE_ADDITIVE) self.set(key, to_update) return self.sync()