Last active
August 25, 2016 18:29
-
-
Save edenzik/7c669c777cd556522a94d2f7bd0b9846 to your computer and use it in GitHub Desktop.
A poor man's REPL written in Python. Guess what? No dependencies. Just fun.
This file contains 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
#!/usr/bin/python -i | |
# For implementing custom exception ahndler | |
import sys | |
# This is an error handler for badly typed input | |
def repl_exchandler(type, value, traceback): | |
# If a name doesn't exist in the namespace | |
if (type == NameError): | |
print "Command {}. Type \"help\".".format(value) | |
return | |
# If the user typed something dumb | |
if (type == SyntaxError): | |
print "Bad input. Type \"help\".".format(value) | |
return | |
# For all other issues (like bugs in your code) get adult supervision. | |
sys.__excepthook__(type, value, traceback) | |
# Only do stuff if it's interactive | |
if sys.flags.interactive: | |
# Actually override the exception handler | |
sys.excepthook = repl_exchandler | |
# Replace the prompt with something funny | |
sys.ps1 = "[REPL]>> " | |
# This is the help string, which will happen when the user types "help" | |
help = """ | |
Welcome to my big fat python REPL! Here's what to do: | |
command() = executes command | |
For any questions, email [email protected] | |
""" | |
print "Hello! Welcome to my dumb REPL in Python. If you need help, type \'help\'. Thanks for playing" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment