Skip to content

Instantly share code, notes, and snippets.

@briceburg
Last active February 4, 2023 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briceburg/491ab5d4ccd188abae8ce48bf937b85a to your computer and use it in GitHub Desktop.
Save briceburg/491ab5d4ccd188abae8ce48bf937b85a to your computer and use it in GitHub Desktop.
Python multiple replacements using reduce
#!/usr/bin/env python3
import sys
import functools
for line in map(str.rstrip, sys.stdin):
repls = ('prod', 'production'), ('sdlc', 'staging'), ('sandbox', 'development')
print("terraform state mv %s %s" % (line, functools.reduce(lambda a, kv: a.replace(*kv), repls, line)))
# the proverbial followup
while IFS="" read -r p || [ -n "$p" ]
do
eval "$p"
done < py-output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment