Created
February 1, 2019 08:29
-
-
Save omeranson/deef510e1694139e5c82cebb990f8eea to your computer and use it in GitHub Desktop.
Utility to convert python values to json (Uses `eval`. Use only on trusted data!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import json | |
import sys | |
def get_streams(): | |
if len(sys.argv) == 1: | |
yield sys.stdin | |
return | |
for arg in sys.argv[1:]: | |
if arg == "-": | |
yield sys.stdin | |
continue | |
with open(arg, "r") as f: | |
yield f | |
def main(): | |
for s in get_streams(): | |
d = eval(s.read()) | |
print(json.dumps(d)) | |
if __name__ == "__main__": | |
try: | |
main() | |
sys.exit(0) | |
except Exception as e: | |
print(e, file=sys.stderr) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment