Skip to content

Instantly share code, notes, and snippets.

@nezda
Last active December 2, 2022 16:23
Show Gist options
  • Save nezda/e5245ceaa8fc2c790ae637f0a4a4e4d0 to your computer and use it in GitHub Desktop.
Save nezda/e5245ceaa8fc2c790ae637f0a4a4e4d0 to your computer and use it in GitHub Desktop.
expand sqla parameters from logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) back into sql
import sys
def main():
# assumes only command line arg is logging from
# logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
# for 1 query.
# assumes last line is dict of params
with open(sys.argv[1], 'r') as my_file:
lines = my_file.readlines()
params: dict[str, str] = eval(lines[-1])
keys = list(params.keys())
outlines = lines[:-1]
for idx in range(len(outlines)):
for k in keys:
to_replace = f'%({k})s'
replacement = params[k]
if isinstance(replacement, int):
replacement = str(replacement)
else:
replacement = f"'{replacement}'"
outlines[idx] = outlines[idx].replace(to_replace, replacement)
print("".join(outlines))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment