Last active
February 26, 2016 08:28
-
-
Save pirate/1259eec02266d3fdd9f6 to your computer and use it in GitHub Desktop.
Pythonrc with Shortcuts for ls, ll, cd, and quit
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
"""Some common helper functions to put in your ~/.pythonrc to make the python REPL behave more like a real shell.""" | |
import os | |
def cd(folder): | |
"""e.g. cd('/bin')""" | |
os.chdir(folder) | |
def quit(): | |
raise SystemExit(0) | |
def listdir(dir=None): | |
return os.listdir(dir or os.getcwd()) | |
class InstantShortcut: | |
"""run a command instantly on __repr__, also supports adding parens to call it with args""" | |
def __init__(self, command, delimiter='\n'): | |
self.__call__ = command | |
self.delimiter = delimiter | |
def __repr__(self): | |
return self.delimiter.join(self()) | |
quit = q = InstantShortcut(quit) | |
ls = InstantShortcut(listdir, ' ') | |
ll = InstantShortcut(listdir, '\n') | |
# usage: type `ll` and press enter in a REPL to list the current directory on seperate lines | |
# type just `quit` and press enter to quit python instead of having to type quit() |
Author
pirate
commented
Feb 26, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment