Seth Michael Larson pointed out that the Python gzip module can be used as a CLI tool like this:
Terminal window
1
python-mgzip--decompresspypi.db.gz
This is a neat Python feature: modules with a if __name__ == "__main__": block that are available on Python’s standard import path can be executed from the terminal using python -m name_of_module.
Seth pointed out this is useful if you are on Windows and don’t have the gzip utility installed.
This made me wonder: what other little tools are lurking in the Python standard library, available on any computer with a working Python installation?
This showed me that the standard library itself for my Homebrew installation of Python 3.11 is in /opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11.
So I switched there and used ripgrep to find likely packages:
1
cd /opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11
grep -v still means “everything that doesn’t match this” - but then the multiple -e '...' patterns are used to construct a “this pattern or this pattern or this pattern” filter. This saves on having to pipe through grep -v multiple times. Thanks for the tip, dfc.
Here’s the result:
1
tabnanny.py
2
pyclbr.py
3
netrc.py
4
heapq.py
5
fileinput.py
6
site.py
7
telnetlib.py
8
smtplib.py
9
timeit.py
10
__hello__.py
11
aifc.py
12
json/tool.py
13
asyncio/__main__.py
14
runpy.py
15
mailcap.py
16
tokenize.py
17
smtpd.py
18
sysconfig.py
19
tarfile.py
20
lib2to3/pgen2/tokenize.py
21
lib2to3/pgen2/driver.py
22
lib2to3/pgen2/literals.py
23
xmlrpc/server.py
24
xmlrpc/client.py
25
getopt.py
26
dbm/__init__.py
27
doctest.py
28
pickle.py
29
imaplib.py
30
compileall.py
31
shlex.py
32
ast.py
33
venv/__init__.py
34
py_compile.py
35
ensurepip/__main__.py
36
ensurepip/_uninstall.py
37
http/server.py
38
pickletools.py
39
poplib.py
40
quopri.py
41
calendar.py
42
pprint.py
43
symtable.py
44
pstats.py
45
inspect.py
46
pdb.py
47
platform.py
48
wsgiref/simple_server.py
49
random.py
50
ftplib.py
51
mimetypes.py
52
turtle.py
53
tkinter/dialog.py
54
xml/sax/expatreader.py
55
tkinter/colorchooser.py
56
tkinter/dnd.py
57
tkinter/filedialog.py
58
tkinter/messagebox.py
59
tkinter/simpledialog.py
60
tkinter/font.py
61
tkinter/scrolledtext.py
62
xml/sax/xmlreader.py
63
tkinter/__init__.py
64
code.py
65
difflib.py
66
pydoc.py
67
uu.py
68
imghdr.py
69
filecmp.py
70
profile.py
71
cgi.py
72
codecs.py
73
modulefinder.py
74
__phello__/__init__.py
75
__phello__/spam.py
76
multiprocessing/spawn.py
77
textwrap.py
78
base64.py
79
curses/textpad.py
80
curses/has_key.py
81
zipapp.py
82
cProfile.py
83
dis.py
84
webbrowser.py
85
nntplib.py
86
sndhdr.py
87
gzip.py
88
ctypes/util.py
89
zipfile.py
90
encodings/rot_13.py
91
distutils/fancy_getopt.py
That’s a lot of neat little tools!
I haven’t explored my way through all of them yet, but running python -m module_name usually outputs something useful, and adding -h frequently provides help.
Even more fun than tokenize - a debug mode for the Python AST module!
Terminal window
1
python-mastcgi.py|head-n10
1
Module(
2
body=[
3
Expr(
4
value=Constant(value='Support module for CGI (Common Gateway Interface) scripts.\n\nThis module defines a number of utilities for use by CGI scripts\nwritten in Python.\n\nThe global variable maxlen can be set to an integer indicating the maximum size\nof a POST request. POST requests larger than this size will result in a\nValueError being raised during parsing. The default value of this variable is 0,\nmeaning the request size is unlimited.\n')),
5
Assign(
6
targets=[
7
Name(id='__version__', ctx=Store())],
8
value=Constant(value='2.6')),
9
ImportFrom(
10
module='io',
I used this module to build my symbex tool - this debug mode would have helped quite a bit if I’d found out about it earlier.
I thought this might provide a utility for generating random numbers, but sadly it’s just a benchmarking suite with no additional command-line options:
Terminal window
1
python-mrandom
1
0.000 sec, 2000 times random
2
avg 0.49105, stddev 0.290864, min 0.00216092, max 0.999473
3
4
0.001 sec, 2000 times normalvariate
5
avg -0.00286956, stddev 0.996872, min -3.42333, max 4.2012
6
7
0.001 sec, 2000 times lognormvariate
8
avg 1.64228, stddev 2.13138, min 0.0386213, max 34.0379
9
10
0.001 sec, 2000 times vonmisesvariate
11
avg 3.18754, stddev 2.27556, min 0.00336177, max 6.28306