43

I'm currently aware of two tools:

  1. base64 encoder/decoder:

    python -m base64 -e <input
    python -m base64 -d <input

  2. json validator and pretty printer

    python -m json.tool <input

where input can be stdin or file.

I'm curious if there are other tools exposed by SPL that work in similar fashion?

0

4 Answers 4

109

Not a complete list...

Encoding

Base64 en/decoding:

python -m base64 -d [file]
python -m base64 -e [file]

ROT-13 en/decoder:

python -m encodings.rot_13

Macintosh BinHex:

# binhex <file> to <file>.hqx, and unbinhex <file>.hqx to <file>.viahqx
python -m binhex <file>

UUencode/decode:

python -m uu [infile [outfile]] # encode
python -m uu -d [infile [outfile]] # decode

MIME quoted-printable en/decoding:

python -m mimify -e [infile [outfile]] # encode
python -m mimify -d [infile [outfile]] # decode

Quoted-printable en/decoding:

python -m quopri [file] # encode
python -m quopri -d [file] # decode

Compression

GZip:

python -m gzip [file] # compress
python -m gzip -d [file] # decompress

Zipfile extraction, etc.:

python -m zipfile -l <file> # list
python -m zipfile -t <file> # test
python -m zipfile -e <file> <dir> # extract
python -m zipfile -c <file> sources... # create

Internet

HTTP servers:

python -m BaseHTTPServer
python -m CGIHTTPServer
python -m SimpleHTTPServer

Simple FTP client:

python -m ftplib host [-l<dir-to-list>] [-d<dir-to-cwd>] [-p] [file-to-retrieve]

HTML Text extraction:

python -m htmllib <file>

JSON Validator and pretty-printer:

python -m json.tool [infile [outfile]]

List POP3 mailbox:

python -m poplib <server> <username> <password>

SMTP server:

python -m smtpd

Send a mail message (to localhost):

python -m smtplib

Telnet client:

python -m telnetlib [host [port]]

MIME type/extension database:

python -m mimetypes file.ext # print type for filename
python -m mimetypes -e mime/type # print extension for type

Open webbrowser:

python -m webbrowser -n <url> # new window
python -m webbrowser -t <url> # new tab

Antigravity:

python -m antigravity

Python

Pure-Python REPL:

python -m code

Python bytecode batch compiler:

python -m compileall

Python code profiler:

python -m cProfile <script>
python -m profile <script>
python -m pstats <filename> # print profiling statistics

Python doctest executor:

python -m doctest <script>

Python benchmark:

python -m test.pystone [iterations]
python -m hotshot.stones

Python interactive debugger:

python -m pdb

Extract Python classes and methods from a module:

python -m pyclbr <script>

Python documentation browser:

python -m pydoc <topic>
python -m pydoc -g # graphical browser
python -m pydoc -p <port> # start HTTP docs server on port

Python snippet timer:

python -m timeit

Misc

Calendar (like cal, but can do HTML and various fancy formatting stuff):

python -m calendar

Directory comparer:

python -m filecmp [-r] dir1 dir2 # -r for recursive directory compare

Paragraph formatting:

python -m formatter [file]

Show current platform (like uname but simpler):

python -m platform
4
  • 3
    Thanks for explicitly showing examples, and adding description. It's a very nice reference to have.
    – theta
    Jan 27, 2013 at 8:00
  • 1
    While many of these are convenient - please don't start using these in lieu of real applications/commands. I am unhappy whenever someone shells out to a command, I would hate to start seeing python -m gzip -d file instead of gunzip file in a bash script. May 6, 2013 at 4:42
  • 1
    @NathanAdams It's years later, but I just found this. What reason other than speed is there to use the native tools instead of the python scripts? The python scripts seem like they would be more portable and more widely installed.
    – Ben
    Sep 9, 2016 at 18:42
  • I would argue gunzip is more standard than Python (it's part of the GNU library of programs) so it's very unlikely they are going to change including command line parameters. Python developers on the other hand seem to play fast and loose with releases and like to change things (for good or bad). The gzip Python library probably won't change - but I personally would rather not have to depend on it (see urllib). I'm not against using Python but using gunzip which was written in C just seems more logical than spinning up the entire Python interpreter into memory. Sep 10, 2016 at 0:34
30

Many.

$ grep "if __name__ == '__main__':" /usr/lib64/python2.7/* | wc -l
55

Not all work as a filter though, so examine the module in question before running.

8
  • +1 awesome. Eye-opening. Jan 27, 2013 at 6:37
  • Yes, that seems like a reasonable pattern to look for. I thought that there is maybe a web page describing these tools, but couldn't find one. This is closest as it gets I guess, while still being on a simple side. Thanks
    – theta
    Jan 27, 2013 at 6:39
  • Is there a Mac OS X equivalent for this? Jan 27, 2013 at 6:50
  • 1
    grep "if __name__ == '__main__':" `python -c 'import os; print os.path.dirname(os.__file__)'`/* | wc -l. And yes, OS X has wc.
    – nneonneo
    Jan 27, 2013 at 15:17
  • 3
    Dont forget to check for the other quote style, and use recursive grep -- there are more goodies, e.g. calandar.py: grep -rs "if __name__ == [\'\"]__main__[\'\"]:" /usr/lib/python2.7
    – David Pope
    May 6, 2013 at 4:04
12

Also, there is:

python -m this
0
1

There's also -m compatible packages on the Cheeseshop. Try "e" or "oo" :-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.