Lab, In Depth Projects
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write family(code) that returns "success" for 2xx codes, "redirect" for 3xx, "client error" for 4xx, "server error" for 5xx and "unknown" for anything else. Print each code and its family for [200, 301, 404, 503, 99], so the program prints:
200 success 301 redirect 404 client error 503 server error 99 unknown
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ with the Counter method that returns the top pairs, so the program prints [('10.0.0.1', 3), ('10.0.0.2', 2)].
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The pattern should capture only the quoted request, but each printed request runs on into the status and user agent. Fix the pattern so the program prints:
GET /index.html HTTP/1.1 POST /login HTTP/1.1
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows status and errors each time one of them changes, once both exist.
errors = 0
for status in [200, 503, 404, 200]:
if status // 100 in (4, 5):
errors = errors + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Build counts with Counter(statuses): no for loop.
This loop counts status codes by hand. Replace the loop with collections.Counter. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write analyze(lines) that parses access log lines like those in LOG and returns a dict with requests (lines that matched), bad_lines (lines that did not), error_rate (the percentage of requests with a 4xx or 5xx status, rounded to 1 decimal place, or 0.0 when there are none), top_client (the most frequent address, or None) and slowest (the path of the single slowest request, or None). The program should print:
requests: 5 bad_lines: 1 error_rate: 40.0 top_client: 10.0.0.1 slowest: /api/items
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.