-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpythonrc.py
More file actions
85 lines (61 loc) · 2.03 KB
/
pythonrc.py
File metadata and controls
85 lines (61 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Dirty up the main namespace with some extra imports"""
try:
see # type: ignore
except NameError:
def see(thing, regexp=None):
"""layout a dir listing filtered by the given regexp"""
if regexp:
import re
print(
"\n".join(
sorted([item for item in dir(thing) if re.match(regexp, item)])
)
)
else:
print("\n".join(sorted(dir(thing))))
def _path_to_history(os):
"""Get the path to our history file - hidden in the home directory"""
path_to_history = os.path.expanduser("~/.pythonhistory")
if not os.path.isfile(path_to_history):
open(path_to_history, "w").close()
return path_to_history
def _write_time_stamp(path_to_history):
"""Add a commented time stamp to that history file"""
import datetime
with open(path_to_history, "a") as history_file:
print("# %s" % datetime.datetime.now().ctime(), file=history_file)
def _read_history(readline):
"""Load the history (if any) into readline"""
import os
import atexit
path_to_history = _path_to_history(os)
if os.path.isfile(path_to_history):
_write_time_stamp(path_to_history)
try:
readline.read_history_file(path_to_history)
except IOError:
pass
atexit.register(readline.write_history_file, path_to_history)
def complete():
try:
import readline
readline.parse_and_bind("tab: complete")
_read_history(readline)
except (OSError, ImportError) as e:
import sys
print(
"Python shell enhancement modules not available because %s" % e,
file=sys.stderr,
)
def set_prompt():
"""set shell prompt"""
import sys
sys.ps1 = "\033[1;32m>>>\033[0m "
sys.ps2 = "\033[1;34m...\033[0m "
complete()
set_prompt()
# https://www.reddit.com/r/Python/comments/4ivd2k/what_is_your_favorite_python_error_message/d329j8l
class Quit(object):
def __repr__(self):
exit()
quit = Quit()