コード例 #1
0
    def _format_dict(_val):
        if not is_dict(_val):
            return _val

        for k, v in _val.items():
            if is_dict(v):
                if len(v) == 1:
                    _val[k] = next(iter(v.values()))
            else:
                if len(_val) == 1:
                    _val = v
        return _val
コード例 #2
0
ファイル: columns.py プロジェクト: marcelomata/Optimus
        def parse_col_names_funcs_to_keys(data):
            """
            Helper function that return a formatted json with function:value inside columns. Transform from
            {'max_antiguedad_anos': 15,
            'max_m2_superficie_construida': 1800000,
            'min_antiguedad_anos': 2,
            'min_m2_superficie_construida': 20}

            to

            {'m2_superficie_construida': {'min': 20, 'max': 1800000}, 'antiguedad_anos': {'min': 2, 'max': 15}}

            :param data: json data
            :return: json
            """
            functions_array = ["min", "max", "stddev", "kurtosis", "mean", "skewness", "sum", "variance",
                               "approx_count_distinct", "na", "zeros", "percentile"]
            _result = {}
            if is_dict(data):
                for k, v in data.items():
                    for f in functions_array:
                        temp_func_name = f + "_"
                        if k.startswith(temp_func_name):
                            _col_name = k[len(temp_func_name):]
                            # If the value is numeric only get 5 decimals
                            if is_numeric(v):
                                v = round(v, 5)
                            _result.setdefault(_col_name, {})[f] = v
            else:
                if is_numeric(data):
                    data = round(data, 5)
                _result = data

            return _result