A useful quickie function to read data.

Steven D. Majewski (sdm7g@aemsun.med.Virginia.EDU)
Wed, 4 Dec 91 13:54:11 EST

I am already finding python quite useful.
I'll wait till I debug them before I post any full programs,
( I've got a 'du' in python that does't follow links or mount points
and takes a max-depth and/or a prune-path argument to limit
how detailed a listing it generates. There is some bug with
the max-depth, and I haven't had time to get back to it. )
but here is a quickie function that is useful.
I am using python to do some quick calculations. I found it easy to
enter the data in "python" format, and use the following function
to load the data. The data looks like this:

( 29, 'Mg-Cl2', (('Mg', 1, 0.66099e+6), ('Cl', 2, 0.12413e+7 )))
( 31, 'Ca-S-O4', (('Ca', 1, 0.11708e+7), ('S', 1, 0.83657e+6 )))
( 32, 'Mg-S-O4', (('Mg', 1, 0.41470e+6), ('S', 1, 0.28843e+6 )))
( 33, 'Ca-Cl2', (('Ca', 1, 0.64138e+6), ('Cl', 2, 0.81340e+6 )))
( 34, 'Al2-S-O4', (('Al', 2, 0.11451e+6 ), ('S', 1, 0.17072e+6 )))
( 35, 'Na2-S-O4', (('Na', 2, 0.41116e+6 ), ('S', 1, 0.44132e+6 )))
( 36, 'K-H2-P-O4', (('K', 1, 0.11479e+7 ), ('P', 1, 0.89652e+6 )))
( 37, 'Na4-P2-O7', (('Na', 4, 0.12013e+7 ), ('P', 2, 0.10849e+7 )))
( 73, 'K-Cl', (('K', 1, 0.45241e+6), ('Cl', 1, 0.20116e+6 )))
( 72, 'Ca3-P-O4', (('Ca', 3, 0.17659e+7), ('P', 1, 0.70474e+6 )))

And is read into a list with:

>>>f = open( filename, 'r' )
>>>L = []
>>> while 1:
... n = feval( b )
... if n = '' :
... break
... else:
... L.append(n)
...

Note:
It is a snap in python to generate tuple pairs like: ( 'Ca/P', ratio ),
take a list of those pairs, invert all of the tuples to:
( 'P/Ca', 1.0/ratio ), merge and sort the 2 lists. I'm working on
the "given any one absolute value, calculate the others from the
ratio pairs" routine. I just took some time off to read the note
on classes that is in the 'misc' directory and reorganize it as
more Obj-Or. I note this mainly to stress how easy to learn the
language is, and how quickly one can start doing useful work.
[ As a counter-example, I remember when I first tried to use
LISP for something other than a textbook exercise, I spent days
re-inventing functions that were actually in the library, but were
too obscurely named/indexed/documented to find. ]

Also note: sys.exc_type not in the library reference. I found it with
dir(sys). Several times I have been ready to write a "why doesn't
python have" or "why can't you" message, only to find, with a little
poking around that what I needed *was* there somewhere.

#! /usr/local/python
# (no main - not executable - above is just there for unix 'file' cmd )
#
# feval( file )
# evaluates a line from file
# Intended for loading python formatted data from a file
# Will not work for expressions longer than a line.
# Should modify and make a version that reads multi-line statements
# but 'input' seems to only work on one liners, so this is
# consistent. Should also try a version using a symbol table
# arg to eval. ( i.e.
# >>> a = 1
# >>> b = input( '?:' )
# ?: a
# Fails: a is not defined within the scope of input()
# unless you also pass a symbol table. )
#

from string import strip
import sys

def feval( File ):
try:
line = File.readline()
if line = '' : return line ;
return eval( strip( line ))
except EOFError:
return EOFError
except ( TypeError,NameError,RuntimeError), what:
return ( sys.exc_type, what, strip(line))


########
# note:
# This also works:
# reassign sys.stdin and use the input() function.
# Should probably wrap a try: except: finally: around
# it so it can't terminate without restoring sys.stdin
#

>>> import sys
>>> def infrom( f ):
... saved = sys.stdin
... sys.stdin = f
... tmp = input( `f`+':\n' )
... sys.stdin = saved
... return tmp
...
>>> b = open( 'binaries', 'r' )
>>> b = infrom( b )

- Steve Majewski