Modules · wrap-up
Cheat sheet & self-check
26 questions across 9 lessons. Each answer links back to the lesson it came from.
Pick an answer to see if you got it, and why.
Q1. Why use a virtual environment (venv)?
Show answer
B. Installing into the system Python can break OS tools. A venv keeps dependencies per project and makes them reproducible.
From lesson 01 · Python essentials for ops peopleQ2. What does ports.get('ssh', 22) return when 'ssh' isn't in the dictionary?
Show answer
B. dict.get(key, default) returns the default instead of raising KeyError.
From lesson 01 · Python essentials for ops peopleQ3. What's wrong with a bare 'except:' that catches everything?
Show answer
B. Catch what you can handle (FileNotFoundError, TimeoutError…). Let everything else fail loudly.
From lesson 01 · Python essentials for ops peopleQ4. Why use yaml.safe_load instead of yaml.load?
Show answer
B. Config files may come from other people or repositories. safe_load limits YAML to strings, numbers, lists and dicts.
From lesson 02 · Files, JSON & YAMLQ5. What's the advantage of writing a config to a temporary file and then renaming it over the original?
Show answer
B. If the program crashes mid-write, the original stays intact. The final rename replaces it in one step.
From lesson 02 · Files, JSON & YAMLQ6. kubectl get pods -o json returns a dict. How do you get a list of pod names?
Show answer
B. Kubernetes list responses keep objects under 'items'; each object's name is under metadata.name.
From lesson 02 · Files, JSON & YAMLQ7. Why is subprocess.run(f'ping -c1 {host}', shell=True) dangerous when host comes from user input?
Show answer
B. With shell=True the string goes to /bin/sh. Pass a list (['ping', '-c1', host]) so the value is a single argument, never shell syntax.
From lesson 03 · Running commands safelyQ8. What does check=True do?
Show answer
B. Without check=True, a failed command just returns a non-zero returncode you might forget to inspect.
From lesson 03 · Running commands safelyQ9. A command sometimes hangs forever. How do you protect your tool?
Show answer
B. timeout kills the child after the given seconds and raises TimeoutExpired, which you can log and handle.
From lesson 03 · Running commands safelyQ10. Why always pass timeout= to requests calls?
Show answer
B. requests has no default timeout. One hung connection can freeze a cron job or an operator loop indefinitely.
From lesson 04 · HTTP & REST APIsQ11. Which errors are usually safe to retry automatically?
Show answer
B. 4xx (except 429) mean the request itself is wrong: retrying won't help. 429 and 5xx are often temporary. Respect Retry-After.
From lesson 04 · HTTP & REST APIsQ12. An API returns 100 results per page and a 'next' link. How do you get everything?
Show answer
B. Pagination is how APIs protect themselves. Follow the next link (or cursor) until the API says you're done.
From lesson 04 · HTTP & REST APIsQ13. Why use the logging module instead of print() in tools?
Show answer
B. Levels let users choose verbosity, and separating logs (stderr) from results (stdout) lets output be piped into other tools.
From lesson 05 · Command-line toolsQ14. A common precedence for settings is…
Show answer
B. The most specific, most recent intent wins: a flag typed right now beats an environment variable, which beats a config file, which beats a built-in default.
From lesson 05 · Command-line toolsQ15. Why offer --output json?
Show answer
B. Human tables for people, JSON for machines. kubectl's -o json is the model.
From lesson 05 · Command-line toolsQ16. Your script works on your laptop but fails inside a pod with 'Invalid kube-config file'. Why?
Show answer
B. In-cluster, credentials come from the mounted ServiceAccount token. Try in-cluster first and fall back to kubeconfig.
From lesson 06 · Automating KubernetesQ17. Why use a watch instead of listing all pods every few seconds?
Show answer
B. Repeated full LISTs are expensive on big clusters. Watch (or an informer-style list-then-watch) is the efficient pattern.
From lesson 06 · Automating KubernetesQ18. A patch returns 403 Forbidden. What should you check?
Show answer
B. 403 is authorization. Give the tool's ServiceAccount exactly the verbs and resources it needs.
From lesson 06 · Automating KubernetesQ19. Your script only finds 50 of your 300 S3 objects. Most likely reason?
Show answer
B. Many AWS list/describe calls return results in pages. Paginators iterate all pages for you.
From lesson 07 · Automating AWS with boto3Q20. Where should credentials for a script running on EC2 or in an EKS pod come from?
Show answer
B. Roles give short-lived, automatically rotated credentials. boto3's credential chain picks them up with no code changes.
From lesson 07 · Automating AWS with boto3Q21. How do you find out which account and identity your script is actually using?
Show answer
B. STS GetCallerIdentity works with any valid credentials and needs no special permission. Log it at start-up.
From lesson 07 · Automating AWS with boto3Q22. What does @pytest.mark.parametrize give you?
Show answer
B. Parametrisation covers edge cases (0, 79, 80, 90, 100%) without copy-pasting test functions.
From lesson 08 · Testing & packagingQ23. How do you test code that calls subprocess.run without running real commands?
Show answer
B. Mocking isolates your logic from the environment, making tests fast, deterministic and safe.
From lesson 08 · Testing & packagingQ24. What does [project.scripts] in pyproject.toml do?
Show answer
B. An entry point turns 'python -m mytool.cli' into a normal command like 'opsctl', installed with the package.
From lesson 08 · Testing & packagingQ25. One of 20 clusters is unreachable. What should the report do?
Show answer
B. Fleet tools must tolerate partial failure: show what you could check, and make what you couldn't obvious.
From lesson 09 · Capstone: a fleet report toolQ26. Why use a bounded thread pool instead of checking clusters one by one?
Show answer
B. The work is I/O-bound. A pool of N workers gives speed without opening hundreds of connections at once.
From lesson 09 · Capstone: a fleet report tool