Config File Formats: Practice Questions
For each snippet, write the output, the exception type, or “Runs Forever.” If it prints nothing, write “No Output”.
Question 1
import yaml
settings = yaml.safe_load("""
users:
- name: Ada
tags: [python, web]
- name: Grace
tags: [compilers]
""")
print(settings['users'][1]['name'])
print(settings['users'][0]['tags'][1])Answer
Grace
web
Question 2
import yaml
settings = yaml.safe_load("""
welcome: |
Hello!
Welcome back.
summary: >
Two posts
this week.
""")
print(repr(settings['welcome']))
print(repr(settings['summary']))Answer
'Hello!\nWelcome back.\n'
'Two posts this week.\n'
Question 3
import yaml
unquoted = yaml.safe_load('on: push')
quoted = yaml.safe_load('"on": push')
print(unquoted[True])
print('on' in unquoted)
print('on' in quoted)Answer
push
False
True
Question 4
import yaml
settings = yaml.safe_load("""
version: 3.10
release: "3.10"
enabled: no
country: "no"
""")
print(settings['version'])
print(settings['release'])
print(settings['enabled'])
print(settings['country'])Answer
3.1
3.10
False
no
Question 5
import tomllib
settings = tomllib.loads("""
title = "My Blog"
[owner]
name = "Ada"
[export.csv]
columns = ["username", "text"]
header = true
""")
print(settings['title'])
print(settings['owner']['name'])
print(settings['export']['csv']['columns'][-1])Answer
My Blog
Ada
text
Question 6
import tomllib
settings = tomllib.loads("""
[export]
formats = ["json", "csv"]
options = {header = true, delimiter = ","}
[owner]
name = "Grace"
active = false
""")
print(settings['export']['options']['delimiter'])
print(settings['export']['options']['header'])
print('active' in settings['export'])
print(settings['owner']['active'])Answer
,
True
False
False