Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Program architecture

Separating computation from input and output lets a program rule be tested without opening files.

Version: Python 3.14; repository image, production runtime not attested

Syntax

def run(lines, emit):
    for line in lines:
        emit(parse(line))

Separating computation from input and output lets a program rule be tested without opening files.

parse converts one line to an integer. run controls iteration and passes each result to the supplied emit callable, such as a list method. If a later line fails, earlier emissions have already happened.

The receiver collects [2, 5] for the two valid lines.

def parse(line): return int(line.strip())
def run(lines, emit):
    for line in lines: emit(parse(line))
out=[]
try: run(['3','bad','7'],out.append)
except ValueError: pass
else: raise AssertionError
assert out==[3]

Examples

def parse(line):
    return int(line.strip())

def run(lines, emit):
    for line in lines:
        emit(parse(line))

result = []
run([" 2 ", "5"], result.append)
print(result)

Result

[2, 5]

The receiver collects [2, 5] for the two valid lines.

Sources