Intermediate
Validate a filter from a model
Write parse_filter(reply, allowed). reply is text from a model and allowed maps each allowed key to a list of allowed values.
Parse the reply as JSON. If it is not valid JSON, or not an object, return {}. Otherwise return a dict with only the keys that are in allowed and whose value is in that key's list.
What Check tests
- A valid filter is kept
- An unknown key is dropped
- An unknown value is dropped
- Text that is not JSON gives no filter
- JSON that is not an object gives no filter
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.