Last active
August 29, 2015 14:00
-
-
Save chugcup/29c76d7ccecd53aefded to your computer and use it in GitHub Desktop.
Personal .pythonrc
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
| ################################################################# | |
| # Add the following to your bashrc to enable loading this script | |
| # alias py='python -i $HOME/.pythonrc' | |
| ################################################################# | |
| import os, sys | |
| from pprint import pprint | |
| home=os.path.expanduser('~') | |
| history_file=home+'/.python_history' | |
| if not os.path.exists(history_file): open(history_file, 'w').close() | |
| cwd = os.getcwd() | |
| if cwd not in sys.path: sys.path.append(cwd) | |
| del os | |
| del sys | |
| # this add completion to python interpreter | |
| import readline | |
| import rlcompleter | |
| readline.parse_and_bind("set show-all-if-ambiguous on") | |
| readline.parse_and_bind("tab: complete") | |
| readline.read_history_file(history_file) | |
| readline.set_history_length(-1) | |
| # this cleans the history of the interpreter and save it to file | |
| def clean_history(histfile): | |
| L=readline.get_current_history_length() | |
| history=[] | |
| for i in range(1,L+1): history.append(readline.get_history_item(i)) | |
| history.reverse() | |
| clean_history=[] | |
| for i in range(len(history)): | |
| command=history[i] | |
| if command not in clean_history: | |
| clean_history.append(command) | |
| else: | |
| readline.remove_history_item(len(history)-(i+1)) | |
| readline.write_history_file(histfile) | |
| # here we register the "clean_history" function | |
| import atexit | |
| atexit.register(clean_history,history_file) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds tab completion and command history to Python interpreter