Write a function that parses `key=value` lines (like a .env file) into a dict, ignoring comments.
BeginnerHands-onPython for DevOps
Answer
def parse_env(path):
env = {}
with open(path) as f:
for raw in f:
line = raw.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
env[key.strip()] = value.strip().strip('"').strip("'")
return env
Key trick → partition("=") splits only on the first =, so values like DB_URL=postgres://u:p@h/db?sslmode=require stay intact.
More on Python for DevOps
Write a Python script using boto3 that lists all running EC2 instances with their Name tag.BeginnerWrite a script that finds unattached EBS volumes and deletes them (with a dry-run flag).IntermediateCall a REST API with `requests`, handle timeouts and non-2xx responses properly.BeginnerAdd retry with exponential backoff to HTTP calls without writing the loop yourself.IntermediateWrite your own retry decorator with exponential backoff and jitter.AdvancedParse a YAML file and print every container image in a multi-document Kubernetes manifest.Intermediate