예제 #1
0
def store_cfg(filename: Union[str, Path], d: dict):

    filename = str(filename)
    data = Box(d)
    if "yml" in filename or "yaml" in filename:
        Box(XConfig.decode(data.to_dict())).to_yaml(filename)
    if "json" in filename:
        Box(XConfig.decode(data.to_dict())).to_json(filename)
    if "toml" in filename:
        Box(XConfig.decode(data.to_dict())).to_toml(filename)
예제 #2
0
def complex_data():
    np.random.seed(666)

    placeholders = []
    env_variables = []
    pl_generator = PlaceholderGenerator()

    sample_dict = {
        "one": pl_generator.create_placeholder(
            placeholders, "int", [6, 7, 8, "default=10"]
        ),
        "two": np.random.randint(-50, 50, (3, 3)).tolist(),
        "three": {
            "3.1": "TrueValue",
            "2.1": [False, False],
            "name": {
                "name_1": pl_generator.create_placeholder(placeholders, "float"),
                "name_2": 2,
                "name_3": {
                    "this_is_a_list": [3.3, 3.3],
                    "this_is_A_dict": {
                        "a": {
                            "f": True,
                            "s": False,
                            "numpy_array": np.array([1.0, 2, 3]),
                            "numpy_data": np.array([1.0, 2, 3])[0],
                            "tuple": ("a", "b", "2"),
                            "boxlist": BoxList([1, 2, 3, 4]),
                            "boxdict": Box({"a": 2.2}),
                        }
                    },
                },
            },
        },
        "first": {
            "f1": pl_generator.create_placeholder(
                placeholders, "str", ["alpha", "beta", "gamma"]
            ),  # placeholders[2],
            "f2": 2.22,
            "f3": [3, 3, 3, 3, 3, 3],
            "external": {
                "ext": np.random.uniform(-2, 2, (2, 3)).tolist(),
                "ext_name": [pl_generator.create_placeholder(placeholders, "bool")],
            },
            "ops": {
                "o1": pl_generator.create_placeholder(placeholders),  # placeholders[4],
                "o2": pl_generator.create_placeholder(
                    placeholders, "path"
                ),  # placeholders[5],
                "o3": pl_generator.create_placeholder(
                    placeholders, "date"
                ),  # placeholders[6],
                "o4": pl_generator.create_placeholder(
                    placeholders, "str", ["A", "B", "C", "default=C"]
                ),  # placeholders[7],
                "o5": pl_generator.create_placeholder(placeholders),  # placeholders[8],
                "o6": pl_generator.create_placeholder(placeholders),  # placeholders[9]
            },
            "env": [
                pl_generator.create_placeholder(
                    env_variables, "env"
                ),  # env_variables[0],
                pl_generator.create_placeholder(env_variables, "env"),
            ],
            "key": {
                "with": 120,
                "dots": pl_generator.create_placeholder(placeholders, "object"),
            },
            "key.with.dots": pl_generator.create_placeholder(
                placeholders
            ),  # placeholders[10],
            "key.with...many.....dots": pl_generator.create_placeholder(placeholders),
            "nested.key": {"with.dots": pl_generator.create_placeholder(placeholders)},
            "placeholder_object": pl_generator.create_placeholder(placeholders),
            "placeholder_cfg": pl_generator.create_placeholder(
                placeholders, "cfg"
            ),  # placeholders[15],
            "placeholder_cfg_root": pl_generator.create_placeholder(
                placeholders, "cfg_root"
            ),  # ,
        },
    }

    schema = Schema(
        {
            "one": Or(int, str),
            "two": list,
            "three": {
                "3.1": str,
                "2.1": [bool],
                "name": dict,
            },
            "first": {Regex(""): Or(str, int, list, float, dict)},
        }
    )

    to_be_raplaced_keys = sorted(
        [
            "three.name",
            "three.name.name_3",
            "three.name.name_3.this_is_A_dict",
            "first",
            "first.external",
        ],
        reverse=True,
    )

    return {
        "data": XConfig.decode(sample_dict),
        "schema": schema,
        "placeholders": placeholders,
        "environment_variables": env_variables,
        "to_be_replaced": to_be_raplaced_keys,
    }