Designing small automation tasks and input/output contracts

Write a pure transformation with an explicit output shape and a policy for blank input.

Learning objective

Write a pure transformation with an explicit output shape and a policy for blank input.

Before you start

You should be comfortable with variables, loops, functions, lists and dictionaries. This lesson uses standard Python in an isolated browser; no uploads, external services or personal files are needed.

The idea

Automation is a sequence of decisions, not a loop copied into a script. Start by describing who supplies the data, what a record means, and what the caller needs back. A pure function accepts data and returns data; it does not read a personal file, send an email, or change its caller’s list. This boundary makes the same transformation useful in a browser exercise or a later command-line script.

A batch summary counts source records even if some are blank. Define blankness using stripped text, but retain the original total. Empty input is a valid batch, not an exceptional situation.

Worked example

Read the contract before running this example. The assertion gives an independently stated expected result; a mismatch should lead you back to the input rules.

def summarize_batch(records):
    return {"total": len(records), "blank": sum(not r.strip() for r in records)}

assert summarize_batch(['a', ' ', '']) == {'total': 3, 'blank': 2}

The total counts every record, including rejected blanks. The blank count tests stripped copies, so the source remains unchanged.

Common mistakes

Do not confuse a printed result with a returned result. Do not silently mutate source records; a second stage might still need them.

Guided practice

Trace the worked example one record or character at a time. Identify which values are copied, which are normalized, and which are rejected. Then replace its input with the empty case and predict the exact return type before running it. Add one boundary case that would expose an overly broad shortcut.

Independent practice

Recap

Write a pure transformation with an explicit output shape and a policy for blank input. Keep input contracts, data transformations and presentation separate. Preserve information needed to explain rejected data, and return deterministic results that another function can use.