Portal | Level: L0: Entry | Topics: Python Automation | Domain: DevOps & Tooling
Python for DevOps Automation - Skill Check¶
Mental model (bottom-up)¶
Python is an interpreter + standard library that's great at orchestration and data shaping. You win by: timeouts, input validation, idempotency, and good error messages.
Visual stack¶
[Your script ] CLI, config, logging, exit codes
|
[Stdlib ] subprocess, pathlib, json, logging, argparse
|
[Interpreter ] imports, bytecode, runtime
|
[OS ] processes, files, sockets
Glossary¶
- venv - per-project dependency isolation
- module/package - importable code unit / directory of modules
- exception - error object used for control flow on failure
- idempotent - safe to run multiple times without changing result
- timeout - upper bound on waiting; prevents hangs
- schema - expected shape/types of input data
Roadmap core (10, easy -> hard)¶
- What's the difference: list vs tuple?
- List mutable; tuple immutable.
- What's a dict used for?
- Key/value mapping; fast lookup by key.
- What is a virtual environment and why use it?
- Isolated deps per project; avoids system Python conflicts.
- How do you read a file safely?
with open(...) as f:ensures close even on exceptions.- Exceptions:
try/except/finallypurpose? - Handle failures;
finallyruns cleanup regardless. - What's the difference:
subprocess.run()vsos.system()? subprocessgives control and avoids shell injection;os.systemis blunt.- JSON/YAML parsing basics?
- Parse then validate shape/types; don't assume keys exist.
- Write a CLI in Python (the right way)?
argparse(orclick/typerin many shops) with subcommands.- Concurrency: threads vs processes (in one line)?
- Threads share memory; processes parallelize CPU-bound.
- Packaging for reuse?
pyproject.toml+ pinned deps + entrypoints + tests.
Automation essentials (easy -> hard)¶
- How do you make HTTP calls?
- Use
requests(common) orhttpx; set timeouts. - Why always set timeouts?
- Prevent hangs; makes retries/backoff meaningful.
- How do you run shell commands safely?
subprocess.run([...], check=True, text=True, capture_output=True)- How do you stream output without buffering everything?
- Use
subprocess.Popenwith iterated reads orrunwithout capture. - What's the right default logging setup?
loggingmodule + structured fields; avoid printing secrets.- How do you handle config?
- CLI args override env override config file; validate at startup.
- How do you model external state safely?
- Make operations idempotent; retry on transient errors; use locks when needed.
- When do you move from scripts to a package?
- Reuse across repos, need tests/versions, or multiple CLIs.
Data & file formats (easy -> hard)¶
- JSON vs YAML tradeoff?
- JSON strict and machine-safe; YAML flexible but can surprise.
- How do you validate input data?
- Type checks + required keys; optionally schema validation.
- What's the safe pattern for writing files?
- Write temp + fsync (if needed) + atomic rename.
- Why avoid parsing with regex alone?
- Edge cases and ambiguity; use proper parsers when formats exist.
- How do you handle large files?
- Stream line-by-line; avoid reading whole file into memory.
Testing (easy -> hard)¶
- Why write tests for infra scripts?
- Prevents regressions; locks invariants; safer refactors.
- Unit vs integration tests?
- Unit: pure functions; Integration: real system interactions.
- What's a good "minimal test harness"?
pytest+ temp dirs + fixtures + golden files.- Mocking: when is it harmful?
- Over-mocking hides real behavior; prefer integration tests for APIs.
- How do you test CLI tools?
- Run subprocess; assert exit codes and output; use temp workspace.
Sources¶
- Python docs,
subprocessdocs,argparsedocs,loggingdocs. - https://docs.python.org/3/
Wiki Navigation¶
Related Content¶
- Perl Flashcards (CLI) (flashcard_deck, L1) — Python Automation
- Python Async & Concurrency (Topic Pack, L2) — Python Automation
- Python Debugging (Topic Pack, L1) — Python Automation
- Python Drills (Drill, L0) — Python Automation
- Python Exercises (Quest Ladder) (CLI) (Exercise Set, L0) — Python Automation
- Python Flashcards (CLI) (flashcard_deck, L1) — Python Automation
- Python Packaging (Topic Pack, L2) — Python Automation
- Python for Infrastructure (Topic Pack, L1) — Python Automation
- Software Development Flashcards (CLI) (flashcard_deck, L1) — Python Automation