Learn Python video series from Google

I’ve been trying to expand my Python knowledge and recently came across Nick Parlante’s 6-part learn Python series on Youtube. I’ve watched several of the videos, and I am impressed with Nick’s teaching ability. Here are links to the 6-part series:

Day 1 part 1: Introduction and Strings

Day 1 part 2: Lists, Sorting and Tuples

Day 1 part 3: Dicts and Files

Day 2 part 1: Regular Expressions

Day 2 part 2: OS and Commands

Day 2 part 3: URLs, HTTP and Exceptions

If you are looking to learn Python this is a great place to start!

Serve out content over HTTP from your cwd immediatly

Ever want to immediatly serve content from a specific directory over HTTP, but didn’t want to bother messing with httpd.conf or other webserver configiurations?

If you’ve got Python installed, this is a snap.  Execute python with the SimpleHTTPServer module, using port 8080 so there isn’t a need to elevate privs to root.

$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 …

Sure enough, pointing a browser to the IP address :8080 of the box hits my home directory listing.  Super easy, super fast, super simple!

I use this to serve content to my PS3.  The PS3 doesn’t support NFS or CIFS, so to download content to the hard drive, the best method is by pulling it over HTTP with the embedded web brower.   On my MacBook, I change into the directory containing whatever media I want to transfer, fire up HTTP, and suck it down to the hard drive on the PS3.  Nice!

Accessing the Python help facility from the Python shell

Python has a ton of useful modules, and the built-in help facility is extremely useful for gaining quick access to a description of methods in a given module. Once a module is imported with import:

>>> import pexpect

You can run dir(MODULE_NAME) to view the list of methods in the module:

>>> dir(pexpect)
[‘EOF’, ‘ExceptionPexpect’, ‘TIMEOUT’, ‘__all__’, ‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__revision__’, ‘__version__’, ‘errno’, ‘fcntl’, ‘os’, ‘pty’, ‘re’, ‘resource’, ‘run’, ‘searcher_re’, ‘searcher_string’, ‘select’, ‘signal’, ‘spawn’, ‘split_command_line’, ‘string’, ‘struct’, ‘sys’, ‘termios’, ‘time’, ‘traceback’, ‘tty’, ‘types’, ‘which’]

To get help on a specific method, you can pass the module and method name to the built-in help function:

>>> help(pexpect.spawn)
Help on class spawn in module pexpect:

class spawn(__builtin__.object)
 |  This is the main class interface for Pexpect. Use this class to start
 |  and control child applications.
 |  
 |  Methods defined here:

   .....



This is pretty sweet, and makes coding in Python super easy.

Installing Python modules with easy_install

I have been spending some of my spare time reading Learning Python, and experimenting with a number of Python modules. One of the cool things about the Python modules facility is easy_install, which allows you to install modules from local files or a remote location. To install a module from a local directory, you can pass the name of the module to easy_install:

$ easy_install simplejson-2.0.9
Processing
Running setup.py -q bdist_egg –dist-dir /Users/matty/simplejson-2.0.9/egg-dist-tmp-vNeFam
unable to execute gcc: No such file or directory
***************************************************************************
WARNING: The C extension could not be compiled, speedups are not enabled.
Failure information, if any, is above.
I’m retrying the build without the C extension now.
***************************************************************************
***************************************************************************
WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python installation succeeded.
***************************************************************************
Adding simplejson 2.0.9 to easy-install.pth file

Installed /Library/Python/2.5/site-packages/simplejson-2.0.9-py2.5.egg
Processing dependencies for simplejson==2.0.9
Finished processing dependencies for simplejson==2.0.9

To install from a network location, you can pass a URL with the location of the module to easy_install:

$ easy_install http://pyyaml.org/download/pyyaml/PyYAML-3.08.tar.gz
Downloading http://pyyaml.org/download/pyyaml/PyYAML-3.08.tar.gz
Processing PyYAML-3.08.tar.gz
Running PyYAML-3.08/setup.py -q bdist_egg –dist-dir /var/folders/-a/-aYP7CaPEk00kLo-WDtJ9U+++TI/-Tmp-/easy_install-3IzAQN/PyYAML-3.08/egg-dist-tmp-5VIWBq
unable to execute gcc: No such file or directory

libyaml is not found or a compiler error: forcing –without-libyaml
(if libyaml is installed correctly, you may need to
specify the option –include-dirs or uncomment and
modify the parameter include_dirs in setup.cfg)
zip_safe flag not set; analyzing archive contents…
Adding PyYAML 3.08 to easy-install.pth file

Installed /Library/Python/2.5/site-packages/PyYAML-3.08-py2.5-macosx-10.5-i386.egg
Processing dependencies for PyYAML==3.08
Finished processing dependencies for PyYAML==3.08

You can also pass the name of a module to easy_install:

$ easy_install pexpect
Searching for pexpect
Reading http://pypi.python.org/simple/pexpect/
Reading http://pexpect.sourceforge.net/
Reading http://sourceforge.net/project/showfiles.php?group_id=59762
Best match: pexpect 2.4
Downloading http://pypi.python.org/packages/source/p/pexpect/pexpect-2.4.tar.gz#md5=fe82d69be19ec96d3a6650af947d5665
Processing pexpect-2.4.tar.gz
Running pexpect-2.4/setup.py -q bdist_egg –dist-dir /tmp/easy_install-3b5oOV/pexpect-2.4/egg-dist-tmp-aYlhyn
zip_safe flag not set; analyzing archive contents…
Adding pexpect 2.4 to easy-install.pth file

Installed /Library/Python/2.5/site-packages/pexpect-2.4-py2.5.egg
Processing dependencies for pexpect
Finished processing dependencies for pexpect

Which will cause easy_install to search the Python package index, and install the module based on information in the package index. I loves me some Python!

Awesome online Python resources

I have been spending a fair amount of time learning Python, and have come across a couple of cool online resources. If you are interested in learning Python but don’t want to buy a book, these (and lots of time writing code) resources are really all you need to become a Python guru:

Python documentation: http://docs.python.org/

Python tutorial: http://www.alan-g.me.uk/

Mozilla team tutorials: http://groups.google.com/group/mozqa-team-python-learning

Various Python tutorials:http://www.awaretek.com/tutorials.html

Dive into Python book: http://diveintopython.org/toc/index.html

Printing fractions with Python

I recently converted from bc to Python for basic calculations, and I like the fact that I can do basic arithmetic from the Python interpreter:

$ python
>>> 5.0 / 3.0
1.6666666666666667

One thing that bugged me about this was the fact that I had to add a decimal point to the input values to get a fraction as a result. While reading through one of my Python books, I came across the future module’s division feature:

$ python
>>> 5 / 3
1
>>> from __future__ import division
>>> 5 / 3
1.6666666666666667

This allow fractional values to be displayed by default, which is exactly what I’m looking for. I have been spending a bunch of time learning Ruby and Python, and so far I am really digging these languages.