Two dicts say what each step needs: how many arguments, and which values pass
To check a step list before it runs, the code needs to know what each step expects.
ARITY = {"scale": 1, "clip": 2, "map": 1, "transpose": 0, "sort": 1, "head": 1}
RULES = {
"scale": lambda factor: isinstance(factor, (int, float)),
"clip": lambda low, high: isinstance(low, (int, float)) and isinstance(high, (int, float)) and low <= high,
"map": callable,
"transpose": lambda: True,
"sort": lambda column: isinstance(column, int) and column >= 0,
"head": lambda n: isinstance(n, int) and n >= 1,
}
for step in [("srot", 0), ("clip", 0), ("clip", 40, 0), ("head", 0), ("map", "x")]:
try:
check([step])
except ValueError as error:
print(error)
bad step: ('srot', 0)
bad step: ('clip', 0)
bad step: ('clip', 40, 0)
bad step: ('head', 0)
bad step: ('map', 'x')
An unknown name, a missing argument, a low above its high, a count of 0 and a map without a function: each one raised the same ValueError, with the step in the message.
The check needs two facts per step name: how many arguments it takes, and what makes them valid. Both are data too, so a new step adds one entry to each dict.
ARITY.get(name) returns None for a name it doesn't know, and None never equals a count. So one comparison catches both an unknown name and a wrong count. RULES[name](*args) runs only when the name is known and the count is right, because or stops at the first side that's true.
A problem that depends on the data is the step's own job, when it runs
("sort", 3) is a fine step for a table four columns wide, and a bad one for a table three wide. The width can also change along the way. READINGS has 3 columns:
| Steps | The width as they run |
|---|---|
[("transpose",), ("sort", 3)] | 3 columns, then 4, then sorted |
[("sort", 3)] | 3 columns: ValueError, no column 3 in a table 3 wide |
The check catches what the step list alone can tell you. What depends on the data at that point, the step itself raises when it runs.
The editor holds ARITY and RULES from above. Write check(steps), so it raises ValueError, with the step in the message, for an unknown name, a wrong count, or values that RULES rejects.