Python Knowledge Base Update -- March 2nd, 2000

Nathan Wallace nathan at synop.com
Thu Mar 2 18:52:20 EST 2000


Thanks to everyone who is jumping in with contributions:

    http://python.faqts.com

WIN A PALM V!  This month one lucky contributor will win a Palm.  Every
contribution you make gives you an entry in the competition.  The more
contributions you make, the more entries you receive.  Read more here:

    http://www.faqts.com/about/competition.phtml

You will notice two sections below; new entries and edited entries.
These are all the entries that have changed since the last posting.

If you see anything wrong or incomplete in any of these entries, 
please edit them in the knowledge base.  Please help us build this 
valuable resource.

Cheers,

Nathan


## New Entries #################################################


-------------------------------------------------------------
Where is Numerical Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/1384
-------------------------------------------------------------
Paul F. Dubois, Joseph VanAndel


Numerical Python is at http://numpy.sourceforge.net .


-------------------------------------------------------------
Does Python run on any RTOS? (ie Phar Lap ETS)?
http://www.faqts.com/knowledge-base/view.phtml/aid/1388
-------------------------------------------------------------
Warren Postma
http://www.geocities.com/SiliconValley/Hills/7282/

What is it?

Phar Lap is a subset of the Windows APIs intended to replace DOS 
Extenders with a fully 32 bit pre-emptively multitasked Real Time 
operating system, with networking, filesystem, etc.  More information 
on Phar Lap at www.pharlap.com. It is a commercial RTOS, and it is NOT 
open source, and it is NOT free.

Resources:

Anyone interesting in Scriping in Embedded Systems should consider 
Python. Particularly, if you are working on Phar Lap's realtime ETS 
kernel, you may wish to know that I am also using it. Please contact me 
or check my web url above for more information on issues specific to 
Phar Lap and Python.    A mailing list will be announced here if any 
interest exists.  

Warren Postma
ZTR Control Systems
wpostma at zdnetmail.com


-------------------------------------------------------------
How do I write a Numeric Python Extension?
http://www.faqts.com/knowledge-base/view.phtml/aid/1401
-------------------------------------------------------------
Janko Hauser, Joseph VanAndel
Konrad Hinsen has written some guidelines.

http://starship.python.net/crew/hinsen/NumPyExtensions.html

An additional document which also shows how to write ufuncs can be found
at:

http://oliphant.netpedia.net/packages/Numerical_Extensions.pdf.gz


-------------------------------------------------------------
What is the #!/usr/bin/python line for?
What different forms of "#!" hack are used, and when?
http://www.faqts.com/knowledge-base/view.phtml/aid/1406
-------------------------------------------------------------
Steve Holden, Nathan Wallace
Peter Funk

The string following the "#!" tells the system execution
library to
treat the script as standard input to the interpreter, which is
assumed to be located there.  This is sometimes called the
"exec\hack".

This allows UNIX users to make their Python scripts executable
and call them by name rather than having to explicitly call the 
Python interpreter.

So they can say

    myscript.py

rather than

    python myscript.py

Since MacOS and Windows don't care about this very first line,
this is only interesting to increase portability on unixoid platforms.  

The utility 'env' normally lives in /usr/bin.  This habit seems to
be even older than the invention of the '#!' exec hack.  Env will
look among all the "usual suspect" directories for an
executable,
and sets a standard environment up for program execution.

So using '#!/usr/bin/env python' as first line is best available 
solution for portable Python programs today.  But:

Today unix like systems can be divided in two major groups:

  - freely available (FreeBSD, NetBSD, Linux, where the several flavours
    of Linux are the most prominent ones)

  - commercial Unices (AIX, HP-Ux, Sun Solaris, SGI IRIX, Unixware)

Today the freely available Unices have about 90% market share (This is 
hard to estimate, since you may copy them over and over without having 
to buy new licenses) and the commercial Unices are slowly dying out 
one after the other.  Most Linux distributions come with a bunch of
useful open source software and so include a ready to run Python system,
where the Python interpreter can usually be found as /usr/bin/python.

However those commercial Unices have some niches, where professionals
may still earn some Money. ;-)  Unfortunately these systems usually
come with the original Unix utilities, which many professionals
(including me) consider simply as broken.  So if you want to make
e.g. a Solaris system usable for many practical tasks you normally
end up first installing gcc, bash, GNU-find and so on and not to
forget Python under /usr/local/bin.  After that you usally extend
the default search path $PATH to also include /usr/local/bin.

However there may be people out there, who are permitted to install
Python under /usr/local/bin but are *NOT* permitted to change the
default search path to include /usr/local/python (e.g. for some
mysterious security policy).  Under this rare circumstances the second
form '#!/usr/local/bin/python' comes into play.  This would allow
to call for example a CGI-script from a web server process, which 
didn't has Python on its search path.  But this situation should 
be considered as very exotic and rare today.


-------------------------------------------------------------
How can I access a Postgres database from Python on Windows?
http://www.faqts.com/knowledge-base/view.phtml/aid/1420
-------------------------------------------------------------
Nathan Wallace
Timothy Grant

Having just been through this, I believe that the easiest solution would
be to use ODBC. You can download a PostgreSQL ODBC driver from the
PostgresSQL ftp site, and then use either the ODBC module that ships
with the Win32 extensions to Python, or download the excellent mxODBC
and use it.

I have successfully been able to code with mxODBC/Tkinter and had the
code developed on my Linux box run pretty much immediately on a Win box.


-------------------------------------------------------------
How can I set my system path from within Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/1421
-------------------------------------------------------------
Nathan Wallace
Emile van Sebille

Try setting it explicitly, e.g.:

    sys.path.insert(0,"yourpath")


-------------------------------------------------------------
How can I check if a number is contained in a range?
http://www.faqts.com/knowledge-base/view.phtml/aid/1422
-------------------------------------------------------------
Nathan Wallace
Gerrit Holl

>>> x=5
>>> 1<x<6
1
>>> 1<x<4
0


-------------------------------------------------------------
How can I use C code in my python script?
http://www.faqts.com/knowledge-base/view.phtml/aid/1423
-------------------------------------------------------------
Nathan Wallace
Alex Farber

Have you read

  http://www.python.org/doc/current/api/api.html


-------------------------------------------------------------
How can I trap a keyboard interrupt using Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/1424
-------------------------------------------------------------
Nathan Wallace
Oleg Orlov

import sys

def Worker(argv):
  while 1:
    pass

try:
  if __name__ =='__main__':
    Worker(sys.argv)
except KeyboardInterrupt:
  print "Saving state..."


-------------------------------------------------------------
Why isn't a pyc file created upon execution of my script?
Do scripts have pyc files?
http://www.faqts.com/knowledge-base/view.phtml/aid/1425
-------------------------------------------------------------
Nathan Wallace
Oleg Broytmann

PYC files aren't created for scripts - only for imported modules. But if
modules do not have their .pyc's too - check for directory permissions.


-------------------------------------------------------------
How can I read all the lines from a file?
How can I detect end-of-file when reading it?
http://www.faqts.com/knowledge-base/view.phtml/aid/1428
-------------------------------------------------------------
Nathan Wallace
Mike Fletcher, ze, Steve Holden

There are a number of ways to approach this problem.

First approach:

  file = open( filename )
  line = file.readline()
  while line:
        doprocessing( line )
        line = file.readline()

The first call to readline() which returns an empty string indicates
you have hit the end of the file.  Empty lines in the file are
returned as strings with the newline still in them.  Don't forget
that every line you actually read has the newline in it, too!

Second approach:

  import fileinput
  file = fileinput.FileInput( filename )
  for line in file:
        doprocessing(line)

This one is straight from the standard tutorial, Chapter 7. Say you want
to read from a file named "file.txt", and print each line.
Just do:

  input=open('file.txt','r')     #open file.txt for reading
  while 1:
    newline = input.readline()   #read a line
    if newline == '': break   #is it empty? you've hit eof. stop reading
    print newline             #else, print the line

Unless the files are too large to consider this, the readlines() file
method is easier to use.


-------------------------------------------------------------
How can I access MySQL from Python?
Should I use MySQLmodule or MySQLdb?
http://www.faqts.com/knowledge-base/view.phtml/aid/1432
-------------------------------------------------------------
Nathan Wallace
Andy Dustman

This was written by Andy Dustman.

Well, I'm the author of MySQLdb. MySQLdb is designed to work well in a
threaded environment (gives up the interpreter lock during blocking
calls), and to work with MySQL-3.22 and up; I have not tested it with
3.23, but once it stabilizes a bit, I will ensure that it continues to
work. Architecturally, it's really two modules:

_mysql: low-level C interface, transliteration of the MySQL C API to an
object-oriented Python interface.

MySQLdb: Python wrapper to _mysql that provides a DB API v2.0-compatible
interface. 

One weird/interesting thing it does is it lets you specify how values
are converted going from MySQL to Python and vice versa. There are two
dictionaries that key on types, which map to functions which do the
conversion, with sensible default conversions. (MySQL actually returns
all values to the C API as strings, and all values passed to MySQL must
be part of a literal query string.)

0.1.2 is the current version. I have reports that it dumps core on
RedHat/Alpha platforms, but I can't reproduce it on RedHat/Intel;
patches welcome. 0.1.1 doesn't have this problem, and it's still
available in the same location (no direct link on the page, however).
Other people have reported getting it to work on that crackpot OS from
out of Redmond. It also includes a patch to make Zope's ZMySQLDA to work
with it instead of MySQLmodule. Also check this page:

http://www.zope.org/Members/adustman/MySQLdb

I haven't looked at MySQLmodule a whole lot lately. Here's what I THINK
I know about it, some of which may be wrong:

- Doesn't give up the interpreter lock during blocking calls, causing
threads to block (bad for threaded things like Zope).

- MySQL-3.21 compatible only.

- Also a split interface (C module + Python wrapper).

- The C module exports a Perl-style interface (ick).

- Not really actively supported. Joe Skinner (original MySQLmodule
author) has expressed optimism that he might finally stop getting bugged
about MySQLmodule. :)


-------------------------------------------------------------
How can I control MS-Access from Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/1417
-------------------------------------------------------------
Nigel Linnett, Nathan Wallace
http://starship.python.net/crew/mhammond/win32/Downloads.html

This simple code depends on having Mark Hammond's win32-all extension 
installed.

For the best functionality run MakePy (In the tools menu of Pythonwin) 
on all Microsoft Excel Object Libraries.

###### Begin code here
# This code is available for you to use as you see fit
# Tested with Python 1.5.2 and Excel 97 and Excel 2000
# No Problems were apparent, but this does not mean there are none


import win32com.client
# Launch Excel and make it visible
ExcelApp=win32com.client.Dispatch("Excel.Application")
ExcelApp.visible = 1
# Add a new workbook to the collection (Excel doesn't always start up
# with a workbook open)
workbook = ExcelApp.Workbooks.Add()
# Add a worksheet to the current workbook
worksheet=workbook.Worksheets.Add()
 
# Do two simple nested loops
for row in range(1,10):
    for column in range(1,10):
        # Get a reference to the current cell
        cell = worksheet.Cells(row, column)
        # Create a value
        val = "Row %i, Col %i" % (row, column)
        # Set the cell Value
        # Use cell.formula to set a formula
        cell.Value=val
        #Do some fancy stuff
        cell.Font.Bold=1
# Loop through all the columns we just used
for column in range(1,10):
    # Set the width appropriately
    worksheet.Cells(1,column).ColumnWidth=12
# Save the workbook in the default location
workbook.SaveAs("exceldemo.xls")
# Quit Excel
ExcelApp.Quit()
# Then delete all the objects we just used
del(cell)
del(worksheet)
del(workbook)
del(ExcelApp)

#
###### Code Ends here
 
Hope this helps you out a bit, it works a lot better if you run makepy 
on the excel object libraries you will find.


-------------------------------------------------------------
What interfaces are available between Python and Java?
http://www.faqts.com/knowledge-base/view.phtml/aid/1433
-------------------------------------------------------------
Sean Blakey
http://www.vex.net/parnassus

The most famous interface is jpython (http://www.jpython.org), a 
complete implementation of Python in Java.  This allows you to call 
Java classes directly from Python code, embed Python code in Java 
classes, and compile Python scripts into Java byte-code.

There are also some efforts to access Java from cpython (The standard 
python interpreter) through the JNI (Java Native Interface) PyJava, 
http://students.cs.byu.edu/~butler/jni/PyJava.html provides some MS 
Windows-specific binaries for this.  The JPI (Java Python Interface, 
http://www.ndim.edrc.cmu.edu/dougc/jpi/Home.html) is a similar binary-
only distribution available for Solaris 2.5, Windows 95/NT, and Irix 
6.x.

Unfortunately, these projects both seem to be bandoned without publicly 
available source code.  The PyJava site has not been updated since 1997 
and the JPI site, although it does not mention any explicit dates, 
makes no mention of later operating system releases (Solaris 7, Windows 
98, Windows 2000).


## Edited Entries ##############################################


-------------------------------------------------------------
How can I read and interpret email attachments using Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/1324
-------------------------------------------------------------
Sean Blakey, Nathan Wallace
Oleg Broytmann

Goto

    http://sun.med.ru/~phd/Software/Python/misc.html

and download

    extract_mime.tgz

There you'll find an example and some docs.  Just feed a MIME message
into the program...

Assuming you have a file pointer to the message, you can create a 
mimetools.Message object to access a multipart mime message.  Here is a 
snippet of code:

def decode_item(item):
    '''
    Decode an indevidual mime item

    item should be a mimetools.Message object.
    '''
    encoding = item.getencoding()
    name = item.getparam('name')
    print 'Decoding to', name
    outfile = open(name, 'wb')                # Write the decoded
                                              # Attatchment to a file
    mimetools.decode(item.fp, outfile, encoding)

def decode_multipart(datafile):
    '''
    Decode a multipart MIME message.

    datafile is the filepointer to a MIME mesage.
    '''
    msg = mimetools.Message(datafile)
    boundary =  msg.getparam('boundary')      # The boundry between
                                              # Message parts
    encoding = msg.getencoding()
    if encoding != '7bit':
        raise 'Unknown encoding!', encoding
    if msg.gettype() != 'multipart/mixed':
        raise 'Message is not multipart!'     # This could probably be
                                              # made more robust
    mmsg = multifile.MultiFile(msg.fp)        # Abstract the message as
                                              # A multifile object
    mmsg.push(boundary)
    while 1:
        mmsg.next()
        if mmsg.last: 
            break
        mime_item = mimetools.Message(mmsg)
        decode_item(mime_item)


-------------------------------------------------------------
Why can I run an executable program using "python prog.py" but
not as "./prog.py"?
http://www.faqts.com/knowledge-base/view.phtml/aid/1353
-------------------------------------------------------------
Nathan Wallace, Michael Hudson
Michael Hudson

So long as your python file is executable by you, starts with

#!/path/to/executable

and /path/to/executable actually exists, you might have problems with
control characters having crept into the line - try `cat -v'-ing it.


-------------------------------------------------------------
Are there any Python functions to help with linear algebra?
What is numpy used for?
http://www.faqts.com/knowledge-base/view.phtml/aid/1354
-------------------------------------------------------------
Nathan Wallace, Paul F. Dubois
William Park, Gary Herron,William Park, Gary Herron ,Paul Dubois

I'm looking for a module or a set of functions that does linear algebra,
things like inverses, multiplication, eigenvalues, etc..

That would be Numeric Python. Numeric Python adds an array-based 
computation facility to Python. Some of the packages based on Numeric 
include FFTs, linear algebra, random numbers, and more. 

Numerical Python is available on sourceforge:

  http://numpy.sourceforge.net/

You can wrap your own Fortran routines with Pyfort:
  http://pyfortran.sourceforge.net


-------------------------------------------------------------
What are my options for HTML parsing?
http://www.faqts.com/knowledge-base/view.phtml/aid/1306
-------------------------------------------------------------
Sean Blakey, Shae Erisson, Nathan Wallace
Python Library Reference http://www.python.org/doc/current/lib/lib.html

The Python Standard library includes an htmllib module which supports
HTML parsing.
To use this module, create a subclass of the htmllib.HTMLParser class. 
Within your subclass, define start_tag() and end_tag() methods for each
tag you wish to handle.  Feed data into your parser by calling the
feed() method.



More information about the Python-list mailing list