Notes from MPUG, June 2013: “Python one-liners” talk

Python one-liners

The Python interpreter command line arguments

Three useful command line arguments for Python one-liners:

    graeme@psylocke:~/Projects/Presentations/MPUG_Jun2013$ python -h
    usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments (and corresponding environment variables):
    -c cmd : program passed in as string (terminates option list)
    -m mod : run library module as a script (terminates option list)
    -      : program read from stdin (default; interactive mode if a tty)

Useful programs & tools

Calendar:

    python -m calendar
    python -m calendar 1999

(type python -m calendar -h for more options)

ZIP and GZIP tools (for when you are stuck at a DOS prompt on a Windows box…)

    python -m zipfile -l pcblib.zip
    python -m zipfile -e pcblib.zip
    python -m zipfile -c release.zip *.py

    python -m gzip -d mydata.tar.gz

Pretty JSON printer:

    python -m json.tool < mydata.json

Unicode character printers, by name or symbol:

    python -c "import unicodedata; print unicodedata.name(unichr(0x2249))"
    python -c "print unichr(0x2249)"

A simple webserver:

    python -m SimpleHTTPServer

You can specify the port number if needed and there is also a CGI version:

    python -m CGIHTTPServer

A basic SMTP server:

    python -m smtpd -n -c DebuggingServer localhost:8025

Again, when you are stuck on a Windows box:

    python -m telnetlib  

There are similar command line tools for FTP, POP, SMTP (client as well as server)
and web.

pydoc tool to read & search Python documentation:

    python -m pydoc 
    python -m pydoc -k 

Python debugger:

    python -m pdb 

Interactive profile statistics browser:

    python -m pstats

String processing:

    python -c "import sys; print ''.join(x.capitalize() for x in sys.stdin)"  < names.txt

Numerical processing:

    python -c "import sys, math; print math.fsum(float(x) for x in sys.stdin)" < numbers.dat

(From Andrew Walker) "what is the easiest way to get a URL encoded password?" With a Python one liner!

    python -c 'from getpass import getpass; from urllib import quote; print quote(getpass("Proxy Password: "))'

Clever/quirky one-liners

The Zen of Python:

    python -m this
    python -c "import this"

xkcd 353:

    python -c "import antigravity"

Braces instead of whitespace?

    python -c "from __future__ import braces"

A CS lecture:

    python -c "import heapq; print heapq.__about__"

How does '-m' work?

    if __name__ == '__main__':
        print "do stuff..."

Some others that don't quite fit into this category

A bash function to grep on all Python files in the current directory:

    pygrep () { grep $* *.py; }

Find the source code directory for a Python module (from here )

    cdp () {
        cd "$(python -c "import os.path as _, ${1}; \
        print _.dirname(_.realpath(${1}.__file__[:-1]))"
        )"
    }

Other useful resources

  • The 'e' module

  • A great discussion at StackOverflow with lots more examples

  • pyp - "Pyp is a linux command line text manipulation tool similar to awk or sed, but which uses standard python string and
    list methods as well as custom functions evolved to generate fast results in an intense production environment."

This entry was posted in Python, Talks. Bookmark the permalink.