Chapter 5 · Real programs

A project of your own

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 18 · Concept

A tool for a small shop

By the end of this lesson you have finished a real tool of several files.

The tool keeps a shop's stock. It reads the items from a CSV file, answers commands, and prints a report.

Every piece comes from an earlier lesson. You will:

  • write the item, a dataclass,
  • load the items from the file, and skip bad lines,
  • print a report, then add a command of your own.
from app.report import report
from app.store import load_items

if __name__ == "__main__":
    items, errors = load_items("data/items.csv")
    for message in errors:
        print(message)
    while True:
        command = input("> ")
        match command.split():
            case ["list"]:
                for item in items:
                    print(item)
            case ["report"]:
                print(report(items))
            case ["quit"]:
                break
            case _:
                print("unknown command:", command)
    print("bye")
from app.models import Item

pen = Item("pen", 12, 1.5)
ruler = Item("ruler", 8, 1.2)
print(pen)
print(ruler)
print(f"{pen.total():.2f}")
print(f"{ruler.total():.2f}")
from app.store import load_items

items, errors = load_items("data/items.csv")
for item in items:
    print(item)
print("items:", len(items))
print("bad lines:", len(errors))
from app.store import load_items

items, errors = load_items("data/items.csv")
for message in errors:
    print(message)
for item in items:
    print(item)
print("items:", len(items))
print("bad lines:", len(errors))
name = "pen"
qty = 12
total = 18.0
print(f"[{name:<12}][{qty:>4}][{total:>9.2f}]")
print(f"[{'eraser':<12}][{30:>4}][{12.0:>9.2f}]")
from app.report import report
from app.store import load_items

items, errors = load_items("data/items.csv")
print(report(items))
from app.report import report
from app.store import load_items

if __name__ == "__main__":
    items, errors = load_items("data/items.csv")
    for message in errors:
        print(message)
    while True:
        command = input("> ")
        match command.split():
            case ["list"]:
                for item in items:
                    print(item)
            case ["report"]:
                print(report(items))
            case ["quit"]:
                break
            case _:
                print("unknown command:", command)
    print("bye")
name,qty,price
stapler,2,7.50
pen,40,1.50
glue,6,2.00
Terminal
Step 1 of 8The finished tool

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)