]> code.delx.au - dotfiles/blob - .pythonrc.py
README: firefox settings
[dotfiles] / .pythonrc.py
1 #!/usr/bin/env python
2
3 from __future__ import division
4
5 import sys
6
7 ####################################
8 # Python2 compatibility for urllib #
9 ####################################
10
11 try:
12 import urllib
13 import urllib2
14 import urlparse
15
16 class dummy_urllib_module:
17 parse = urlparse
18 request = urllib2
19 sys.modules["urllib"] = dummy_urllib_module
20 sys.modules["urllib.parse"] = dummy_urllib_module.parse
21 sys.modules["urllib.request"] = dummy_urllib_module.request
22 dummy_urllib_module.parse.urlencode = urllib.urlencode
23
24 except ImportError:
25 pass
26
27
28 ##################
29 # Useful imports #
30 ##################
31
32 import base64
33 import collections
34 import datetime
35 import hashlib
36 import io
37 import json
38 import math
39 import os
40 import pickle
41 import random
42 import re
43 import socket
44 import struct
45 import subprocess
46 import tempfile
47 import time
48 import urllib
49 import urllib.parse
50 import urllib.request
51
52
53 #######################
54 # Readline completion #
55 #######################
56
57 def setup_readline_completion():
58 import atexit
59 import readline
60 import rlcompleter
61
62 history_path = os.path.expanduser("~/.python_history")
63
64 default_completer = rlcompleter.Completer()
65
66 readline.parse_and_bind("tab: complete")
67
68 if os.path.exists(history_path):
69 readline.read_history_file(history_path)
70
71 def my_completer(text, state):
72 if text.strip() == "":
73 if state == 0:
74 return text + "\t"
75 else:
76 return None
77 else:
78 return default_completer.complete(text, state)
79 readline.set_completer(my_completer)
80
81 def save_history(history_path=history_path):
82 readline.write_history_file(history_path)
83 atexit.register(save_history)
84
85 setup_readline_completion()