Open lesson

Mission: demonstrate the tool to Imani

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Create a simple explainable batch-checking command with numeric arguments, an optional name, automatic help and a failure signal.

In the story, Imani enters the evidence room with two colleagues. Voss insists your ID is enough to suspend you. Mara asks for a reproducible demonstration: batch P has 7 rows and 6 IDs; Q has 2 rows and 3 IDs; R has 0 and 0. The practice script is check.py; original receipts remain sealed. Say what you will type and what the computer must answer.

Your mission

  1. Write commands for P, Q and R with batch labels.
  2. Give expected output or refusal and exit code for each.
  3. Explain how to display help and distinguish required arguments from the option.
  4. Say what the demonstration proves and what still needs human investigation.

How to check your work

  • Each command supplies two counts and --label followed by the name.
  • P gives 1 with code 0; Q is refused with code 2; R gives 0 with code 0.
  • --help shows usage; 7 and 6 are required, while --label P is optional.
  • The counting rule is repeatable; source and author of traces still need checking.

Course example

Workspace version: the sample arguments 5 and 4 are supplied in the code. The complete command-line program remains in the lesson.

import argparse

def main():
    parser = argparse.ArgumentParser(description='Count repeated rows')
    parser.add_argument('rows', type=int)
    parser.add_argument('distinct', type=int)
    parser.add_argument('--label', default='batch')
    args = parser.parse_args(['5', '4'])

    if args.rows < 0 or args.distinct < 0 or args.distinct > args.rows:
        parser.error('impossible count')
    repeats = args.rows - args.distinct
    print(f'{args.label}: {repeats} repeat(s)')
    return 0

if __name__ == '__main__':
    main()

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# Write commands for P, Q and R with batch labels.
# Give expected output or refusal and exit code for each.
# Explain how to display help and distinguish required arguments from the option.
# Say what the demonstration proves and what still needs human investigation.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

import argparse

def main():
    parser = argparse.ArgumentParser(description='Count repeated rows')
    parser.add_argument('rows', type=int)
    parser.add_argument('distinct', type=int)
    parser.add_argument('--label', default='batch')
    args = parser.parse_args(['5', '4'])

    if args.rows < 0 or args.distinct < 0 or args.distinct > args.rows:
        parser.error('impossible count')
    repeats = args.rows - args.distinct
    print(f'{args.label}: {repeats} repeat(s)')
    return 0

if __name__ == '__main__':
    main()