Guide Python Intermediate

7.16 DevOps Use Cases

Real-world operator patterns from DevOps scripts — CPU/disk threshold checks, log-level filtering, HTTP status classification, AWS resource state checks, and service health monitoring.

2 min read

Operators are the backbone of every monitoring/validation script — these five patterns cover most of what shows up in practice.

CPU/Disk Threshold Checks

cpu_usage = 87.5
disk_usage = 92.0
CPU_THRESHOLD = 80
DISK_THRESHOLD = 90

>>> if cpu_usage > CPU_THRESHOLD or disk_usage > DISK_THRESHOLD:
...     print("ALERT: Resource threshold exceeded")
ALERT: Resource threshold exceeded

Log Parsing

Comparison and membership operators filter parsed log fields:

log_level = "ERROR"
>>> log_level in {"ERROR", "CRITICAL"}
True

Deployment Validation

Chained comparisons (see 7.12 Chained Comparisons) are the natural way to classify an HTTP status code:

status_code = 404

>>> if 200 <= status_code < 300:
...     result = "Success"
... elif 400 <= status_code < 500:
...     result = "Client Error"
... elif status_code >= 500:
...     result = "Server Error"
...
>>> result
'Client Error'

AWS Resource Checks

instance_state = "running"
>>> is_healthy = instance_state == "running"
>>> is_healthy
True

Health Monitoring

Combining several conditions with and to define “healthy” as a single boolean, benefiting from short-circuit evaluation (see 7.5 Logical Operators) to skip expensive checks once an earlier one already fails:

service_up = True
response_time = 150

>>> is_service_healthy = service_up and response_time < 200
>>> is_service_healthy
True

Quick Interview Answer

“In DevOps scripts, operators show up constantly in a handful of recurring shapes: comparison operators against thresholds for CPU/disk alerts, in against a set of log levels for log filtering, chained comparisons to classify HTTP status codes into success/client-error/server-error bands, == for AWS resource state checks like instance_state == 'running', and and chains combining multiple boolean health signals into one overall health flag — where short-circuit evaluation naturally skips expensive checks once an earlier one has already failed.”

Common Mistakes

  • Comparing an AWS resource state with is instead of == — string identity isn’t guaranteed across API responses, only value equality is reliable.
  • Writing a chain of separate if statements for status-code classification instead of elif with chained comparisons — more error-prone and harder to keep mutually exclusive.
  • Combining unrelated health checks into one dense and expression instead of naming intermediate booleans, covered in 7.17 Best Practices.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form