Practical data workflows
Data records and schema checks
Define the shape and rules of incoming records.
Lesson 59 of 63
Reading: about 1 min
Lesson + practice: about 15 min
Difficulty: Intermediate
On this page5 sections
External data arrives as text or loosely typed values. Validate required fields before calculating. Reject Boolean durations because bool is an int subclass.
Worked example
def validate(row):
if not isinstance(row, dict) or not isinstance(row.get("subject"), str):
raise ValueError("subject required")
if not row["subject"].strip() or type(row.get("minutes")) is not int or row["minutes"] <= 0:
raise ValueError("invalid session")
return row
print(validate({"subject": "Python", "minutes": 25})){'subject': 'Python', 'minutes': 25}Why this works
The function checks record shape, nonblank subject, exact integer type, and positive range before returning data.
Another case
print(type(True) is int, isinstance(True, int))False True
Practice
What happens for {"subject": "Python", "minutes": True}?
Check the practice result
Reveal answer
It raises ValueError because True is not exactly an int.
Try another case
What happens for {"subject": "Python"} with no minutes key?
Check your answer
Reveal answer
It raises ValueError("invalid session"): row.get("minutes") returns None, which is not an int.
Your progress
Reveal the check answer or reach the end to complete this lesson.