Skip to content

Instantly share code, notes, and snippets.

@lost-theory
Created February 1, 2012 00:25
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 lost-theory/1714168 to your computer and use it in GitHub Desktop.
Save lost-theory/1714168 to your computer and use it in GitHub Desktop.
py
#!/usr/bin/env python
"""
Command line tool for showing the result of a simple python expression.
Some common modules are imported for you (e.g. math, datetime, pprint, etc.).
Exceptions are caught and printed, instead of showing a full traceback.
Examples:
$ py "2+2"
2+2 -> 4
$ py "1/0"
1/0 -> ZeroDivisionError('integer division or modulo by zero',)
$ py "map(lambda x: x**2, range(10))"
map(lambda x: x**2, range(10)) -> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
$ py "math.sqrt(81)"
math.sqrt(81) -> 9.0
$ py "glob.glob('*.py')"
glob.glob('*.py') -> ['called_from.py', 'hmm.py', 'mc.py', 'xx.py', 'yvfc.py']
$ py "os.getcwd()"
os.getcwd() -> '/home/steve'
"""
import math, sys, os, calendar, datetime, collections, dis, pprint, functools, glob, json, hashlib, random, re, subprocess, time, uuid
if __name__ == "__main__":
import sys
arg = sys.argv.pop(-1)
try:
res = eval(arg)
except Exception, e:
res = e
print arg, "->", repr(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment