Screen Too Small

This course material requires a larger screen to properly display the terminal and editor interfaces.

Please use a tablet, laptop, or desktop computer for the best learning experience.

You can continue browsing, but the experience will be significantly limited.

Vim Dojo challenge #23

From Debug to Deploy: Upgrade Logging Statements

Refactor messy print debug statements into structured logger calls using Python’s logging module. Macros will save you from repetition.

challenge.txt :set list

Start

 1import logging
 2logger = logging.getLogger(__name__)
 3
 4def fetch_data(url):
 5    print("start")
 6    response = requests.get(url)
 7    return response.json()
 8
 9def process_data(data):
10    print("debug")
11    cleaned = clean(data)
12    return cleaned
13
14def save_results(results):
15    print("yo")
16    with open('out.json', 'w') as f:
17        json.dump(results, f)

Goal

 1import logging
 2logger = logging.getLogger(__name__)
 3
 4def fetch_data(url):
 5    logger.info("Entering fetch_data")
 6    response = requests.get(url)
 7    return response.json()
 8
 9def process_data(data):
10    logger.info("Entering process_data")
11    cleaned = clean(data)
12    return cleaned
13
14def save_results(results):
15    logger.info("Entering save_results")
16    with open('out.json', 'w') as f:
17        json.dump(results, f)

Members only

Hold on, cowboy

You gotta sign in to take on challenges. New ones drop every week — they're fun, and insightful.