]> code.delx.au - dotfiles/blob - .pythonrc.py
bashrc: find SSH keyring in more places
[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 binascii
34 import collections
35 import datetime
36 import hashlib
37 import io
38 import json
39 import math
40 import os
41 import pickle
42 import random
43 import re
44 import socket
45 import struct
46 import subprocess
47 import tempfile
48 try:
49 import tomllib
50 except ImportError:
51 try:
52 import tomli as tomllib
53 except ImportError:
54 pass
55 import sqlite3
56 import time
57 import urllib
58 import urllib.parse
59 import urllib.request
60 import yaml
61
62
63 #######################
64 # Readline completion #
65 #######################
66
67 def setup_readline_completion():
68 import atexit
69 import readline
70 import rlcompleter
71
72 history_path = os.path.expanduser("~/.python_history")
73
74 default_completer = rlcompleter.Completer()
75
76 readline.parse_and_bind("tab: complete")
77
78 if os.path.exists(history_path):
79 readline.read_history_file(history_path)
80
81 def my_completer(text, state):
82 if text.strip() == "":
83 if state == 0:
84 return text + "\t"
85 else:
86 return None
87 else:
88 return default_completer.complete(text, state)
89 readline.set_completer(my_completer)
90
91 def save_history(history_path=history_path):
92 readline.write_history_file(history_path)
93 atexit.register(save_history)
94
95 setup_readline_completion()