Useful Python executable modules

In the article "Using Python command line" I've described some executable modules and now want to give more details about the most interesting ones. I do not pretend to cover everything, just want to point out some directions where you can go if you find this useful.



SimpleHTTPServer

The executable module which I use if there is no possibility to send files from one computer to another by other methods (ssh, smb, ftp etc). It is not so fast, but can be very useful sometimes. The usage is very simple - just change directory to the required one, and start SimpleHTTPServer:


calendar

Very simple module - just shows the year calendar:


urllib

Very simple download manager. Useful if you want to download a file or a page via command line, but don't have wget utility (for example, in Windows). Now you don't even need to open IE for FF downloading:

$ python -m urllib "http://download.mozilla.org/?product=firefox-3.6.6&os=win&lang=en-US" > firefox_setup.exe

timeit

Tool for measuring execution time of small code snippets. For example, let's measure classical list comprehension example:

$ python -m timeit "l = []" "for i in range(1000):l.append(i ** 2)"
1000 loops, best of 3: 243 usec per loop

$ python -m timeit "[i ** 2 for i in range(1000)]"
10000 loops, best of 3: 181 usec per loop

$ python -m timeit "map(lambda i: i ** 2, range(1000))"
1000 loops, best of 3: 278 usec per loop

Standard list comprehension wins! :-)

trace

This module allows trace the script execution and gather various statistics. As an example, I'll use the makepwd.py script. The --trace option trace all the Python commands executed during the script running:

$ python -m trace --trace makepwd.py
 --- modulename: threading, funcname: settrace
threading.py(90):     _trace_hook = func
 --- modulename: trace, funcname:
(1):   --- modulename: trace, funcname:
makepwd.py(2): """ Simple password generator """
makepwd.py(4): import random
 --- modulename: random, funcname:
..... skipped ....
 --- modulename: random, funcname: randrange
random.py(171):         istart = int(start)
random.py(172):         if istart != start:
random.py(174):         if stop is default:
random.py(182):         istop = int(stop)
random.py(183):         if istop != stop:
random.py(185):         width = istop - istart
random.py(186):         if step == 1 and width > 0:
random.py(200):             if width >= maxwidth:
random.py(202):             return int(istart + int(self.random()*width))
makepwd.py(15):        for __ in range(options.length)])
$GdEu3Xz

Of course, it is VERY verbose, but it can be useful for Python learning or debugging multithreaded application.

profile

This module allows to profile a script. It supports various sorting options. As an example, I'll use the same script:

$ python -m profile -s calls makepwd.py
&<^8i*i=
         430 function calls in 0.000 CPU seconds

   Ordered by: call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       48    0.000    0.000    0.000    0.000 :0(endswith)
       48    0.000    0.000    0.000    0.000 :0(startswith)
       36    0.000    0.000    0.000    0.000 :0(append)
       25    0.000    0.000    0.000    0.000 :0(setattr)
       18    0.000    0.000    0.000    0.000 :0(get)
       18    0.000    0.000    0.000    0.000 :0(find)
       16    0.000    0.000    0.000    0.000 :0(stat)
       16    0.000    0.000    0.000    0.000 genericpath.py:15(exists)
       16    0.000    0.000    0.000    0.000 posixpath.py:59(join)
       12    0.000    0.000    0.000    0.000 :0(replace)
       11    0.000    0.000    0.000    0.000 :0(len)
        8    0.000    0.000    0.000    0.000 :0(random)
        8    0.000    0.000    0.000    0.000 random.py:160(randrange)
        8    0.000    0.000    0.000    0.000 :0(chr)


smtpd


This module is useful for testing e-mail sending. It allows to run "dumb" SMTP server that receives the e-mails locally and displays them to the terminal, but does not actually send anything.


$ python -m smtpd -n -c DebuggingServer localhost:1025


This command will start a simple SMTP server listening on port 1025 of localhost. This server simply prints to standard output all e-mail headers and the e-mail body.



This brief post shows just a little part of the Python magic. Study, experiment, and let the Python force be with you!

Comments

Popular posts from this blog

Web application framework comparison by memory consumption

Trac Ticket Workflow

Python vs JS vs PHP for embedded systems