Objective
Add a lightweight Python utility named max_all_red_explicit.py that inspects a parsed traffic cycle and returns the maximum duration among all phases where the green interval is empty (an all red phase).
What you build
A single Python script max_all_red_explicit.py
It reads a JSON representation of a cycle from a file path given as the first command line argument, or from standard input if no file is provided.
It prints a single number: the maximum duration in seconds among all red phases. If there are no all red phases, print 0.
Input format
The script should be robust to two common shapes:
Top level is a list of phase objects. Each phase is a dict with at least a green field and a duration.
Top level is a dict with a key named either phases or cycle that maps to a list of phase objects.
Each phase object may contain:
green: could be a number, a string, None, or missing. An all red phase is defined as green being None or an empty string.
duration: a numeric value representing the duration in seconds (preferred).
length: an alternative numeric duration if duration is not present.
start and end: optional timestamps; if both are present as numbers, duration may be computed as end minus start.
The script should ignore phases without a computable duration.
Algorithm
Normalize input to a list of phase dicts.
For each phase, determine if all red (green is None or empty string).
If all red, extract duration via the first available method in the order: duration, length, end minus start.
Track the maximum of these durations.
If no all red phases exist, output 0.
Output
Print a single line with the maximum all red duration in seconds.
Do not print anything else.
Edge cases
No input, invalid JSON, or no all red phases: print 0
Durations may be integers or floats; print as a float if needed, otherwise as an int.
Example (conceptual)
A cycle with several phases some of which are all red with durations 8, 12, 9 seconds. The script should print 12.
Notes
Use only Python standard library.
The script should be easy to run in a containerized or CI like environment.