Thomas Eapen · Selected work

Putting a reporting method behind a clear command

Typed options, useful defaults and built-in help turn an analysis function into a reproducible command.

Synthetic example · independently invented dataOne-page print edition · A3 landscape
  1. 01 · ChooseSelect a view and a threshold.
  2. 02 · ValidateReject invalid choices before processing.
  3. 03 · ReportApply the same rule to the same synthetic rows.

SYNTHETIC INPUT / THREE GROUPS

A small exception report

Invented values; differences are observed minus planned
GroupPlannedObservedDifference
Group A6063+3
Group B7571-4
Group C9091+1

ACTUAL DEMONSTRATION INVOCATION AND OUTPUT

report --threshold 2 --view exceptions
Group A: +3 units
Group B: -4 units
  • --threshold accepts non-negative integers; the default is 2.
  • --view accepts all or exceptions.
  • --help describes the options and their defaults.

THE METHOD / PYTHON

A new Click command

import click

@click.command()
@click.option('--threshold', type=click.IntRange(min=0),
              default=2, show_default=True)
@click.option('--view', type=click.Choice(['all', 'exceptions']),
              default='exceptions', show_default=True)
def report(threshold, view):
    """Summarise the three built-in synthetic rows."""
    for row in CLI_ROWS:
        delta = row['observed'] - row['planned']
        if view == 'all' or abs(delta) > threshold:
            click.echo(f"{row['group']}: {delta:+d} units")

Selected code from the new demonstration that computes these results.

Separate the interface from the records CLI_ROWS contains the three invented rows shown above. A difference is reported when its absolute value is greater than the threshold. Group C is omitted because its difference is only +1.