Put a Python Boundary Around the Database · practice
Return Plain Available-Item Records
The caller wants catalog records, not SQLite machinery. A useful public return looks like this:
{'id': 1, 'asset_tag': 'IT-100', 'name': 'Cordless drill',
'category': 'tools', 'loan_days': 7}
The current (result, rows) pair served its purpose: you saw that execution returns a cursor and fetchall() returns a separate sqlite3.Row values. Keeping that pair public would force every caller to understand those details.
Open catalog_db.py. Change only the final return in list_available_items(connection):
available_items = []
for row in rows:
available_items.append(dict(row))
return available_items
Each configured Row knows the five names from the SELECT list, so dict(row) creates a built-in
id, asset_tag, name, category, loan_days
The sqlite3 or remembering column positions.
Keep the SQL unchanged. Its left join matches only active loans, and WHERE loans.id IS NULL keeps items with no such match. That means an item with returned history is available, just like an item that has never been loaned. ORDER BY items.asset_tag gives the caller stable ordering directly from the database.
Press Run. The call is still list_available_items(connection), using a connection from open_database(database_path). The observed public shape now changes:
Returned container type: list
First row type: dict
First row values: 1 | IT-100 | Cordless drill | tools | 7
The output also prints all four dictionaries in asset-tag order. The settings block remains unchanged because moving the public boundary does not change connection setup.
Submit uses new values, a name containing an apostrophe, shuffled insertion order, active loans, returned history, and items never loaned. It calls twice and checks exact built-in
Task
Change list_available_items(connection) to build a dict(row) values with a
Keep the query, fetchall(), and connection ownership unchanged. Return a list of built-in id, asset_tag, name, category, and loan_days, ordered by asset tag. Do not return the cursor or sqlite3.Row values.