예제 #1
0
def main(args, extra):

    # Default file for grids is grids.yml
    if not args.input:
        args.input = ["grids.yml"]
    input_file = args.input.pop(0)

    if not os.path.exists(input_file):
        sys.exit(f"{input_file} does not exist.")

    runner = GridRunner(input_file)
    grids = runner.get_grids()

    # If no name specified, print grid listing
    if args.input:
        name = args.input[0]
        if name in grids:
            grid = grids[name]
        else:
            sys.exit(f"{name} is not a valid grid name in {input_file}")

        if args.arg and args.arg not in grid.args:
            sys.exit(f"{args.arg} is not an argument in grid {grid}.")

        # Print count (length) of a variable, or number o grids
        if args.count and args.arg:
            try:
                print(
                    f"Variable {args.arg} has length {len(grid.args[args.arg])}."
                )
            except:
                print(f"{grid.args[args.arg]}")

        # Just count of global
        elif args.count:
            print(f"{len(list(grid))} argument sets produced.")

        # Just print the argument
        elif args.arg:
            print(grid.args[args.arg])

        # Export data to file
        elif args.export:
            grids = list(grid)
            write_json(grids, args.export)
        else:
            for argset in grid:
                print(argset)
    else:
        print("\n".join(list(grids.keys())))
예제 #2
0
def test_grids():
    """Test loading and using different kinds of grids.
    """
    from gridtest.main.grids import Grid
    from gridtest.main.test import GridRunner
    from grids.script import get_pokemon_id

    grids_file = os.path.join(here, "grids", "grids.yml")
    runner = GridRunner(grids_file)

    # Test get_grids function via runner
    grids = runner.get_grids()
    for key, grid in grids.items():
        print(list(grid))

    # Case 1: a grid with a custom function
    grid = Grid(name="generate_pids",
                params={
                    "functions": {
                        "pid": get_pokemon_id
                    },
                    "count": 10
                })
    assert grid.name == "generate_pids"
    assert len(list(grid)) == 10

    # Case 2: Grid with system function
    entry = {
        "functions": {
            "pid": "random.choice"
        },
        "count": 10,
        "args": {
            "seq": [[1, 2, 3]]
        },
    }
    grid = Grid(name="random_choice", params=entry)

    assert grid.name == "random_choice"
    assert len(list(grid)) == 10
    assert len(grid.argsets) == 0

    # Case 3: cache results
    entry["cache"] = True
    grid = Grid(name="random_choice", params=entry)
    assert len(grid.argsets) == 10

    # Case 4: Generate empty returns empty arguments
    grid = Grid(name="generate_empty", params={"count": 10})
    assert len(list(grid)) == 10

    # Case 5: Generate matrix with single level lists parameterizes over them
    params = {"args": {"x": [1, 2, 3], "y": [1, 2, 3]}}
    grid = Grid("generate_matrix", params=params)
    assert len(list(grid)) == 9

    # Case 6: List of lists uses list as input argument
    params = {
        "args": {
            "x": [[1, 2, 3], [4, 5, 6]],
            "y": [[1, 2, 3], [4, 5, 6]]
        }
    }
    grid = Grid("generate_lists_matrix", params=params)
    assert len(list(grid)) == 4

    # Case 7: min, max and by with one argument
    entry = {"args": {"x": {"min": 0, "max": 10, "by": 2}}}
    grid = Grid("generate_by_min_max", params=entry)
    assert len(list(grid)) == 5

    # Case 7: min, max, and two arguments
    entry = {
        "args": {
            "y": {
                "min": 0,
                "max": 10,
                "by": 2
            },
            "x": {
                "min": 10,
                "max": 20,
                "by": 2
            },
        }
    }
    grid = Grid("generate_by_min_max_twovars", params=entry)
    assert len(list(grid)) == 25