Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Python meets MySQL
EXPERIMENT 87 / 101

A report backed by real records.

Build a small parameterized reporting boundary and test it against fixture data.

ADVANCED LAB / BUILD PROJECT
BY NAVAN SAMI◷ 10 MIN READPYTHON 3

01 The idea

This project brings database access behind an explicit function. The caller supplies an existing connection-owned cursor and a project id; the function returns structured data rather than printing or opening secret connections. The practice fixture gives the project a repeatable baseline. A real application would place schema migration and persistent connection configuration outside the reporting function.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

mysql_project.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    def open_report(cursor, project_id):
        cursor.execute("SELECT name, minutes FROM lab_tasks WHERE project_id = %s AND done = %s ORDER BY id", (project_id, False))
        tasks = cursor.fetchall()
        return {"names": [row["name"] for row in tasks], "minutes": sum(row["minutes"] for row in tasks)}
    report = open_report(cursor, 1)
    assert report == {"names": ["Build", "Test"], "minutes": 60}
    print(report)
↳ EXPECTED OUTPUT
{'names': ['Build', 'Test'], 'minutes': 60}

03 Under the hood

  1. 1

    The function performs one narrow query and returns an ordinary dictionary. That shape is easy to validate, display, export, and unit test.

  2. 2

    The fixture assertion is an integration expectation: it depends on actual server semantics and the known seeded records. A mocked cursor alone cannot prove the SQL works.

  3. 3

    Extend the project with a CLI argument, a typed result record, and an export option. Keep secrets in environment configuration and distinguish connection failures from empty reports.

04 Your turn, builder

A SMALL MISSION

Add a report for a project with no matching records and verify the zero-total contract.

Reveal one possible solution +
one_possible_solution.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    def total_for_project(cursor, project_id):
        cursor.execute("SELECT COALESCE(SUM(minutes), 0) AS total FROM lab_tasks WHERE project_id = %s AND done = %s", (project_id, False))
        return int(cursor.fetchone()["total"])
    assert total_for_project(cursor, 999) == 0
    print(total_for_project(cursor, 1))

↗ Definition of done

  • □Bind every external value.
  • □Report Orbit as Build and Test, totaling 60 minutes.
  • □Return an empty result and zero minutes for a missing project.
  • □Keep connection failures distinct from empty data.
  • □Add an integration test before swapping the fixture for persistent tables.

05 Check your signal

QUICK SIGNAL CHECK

What should the reporting function return?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.