newbie question

Tim Roberts timr at probo.com
Mon Dec 12 02:48:24 EST 2005


"Bermi" <bermi7 at gmail.com> wrote:

>i have this program
>===================
>from sys import *
>import math
>import math, Numeric
>from code import *
>from string import *
>from math import *
>from dataSet import *
>from string import *

I know you said you are a newbie.  That means there is still time for you
to learn to do things the proper way.  Change all of that to:

  import sys
  import math
  import Numeric
  import dataSet

You don't need to import string at all, and you will probably never use
"code".


>def drawAsciiFile():
>    _fileName=str(argv[1])

argv is already an array of strings.  The "str" is useless.

>    __localdataSet=DataSet(_fileName)
>
>    #_PlotCols=string.split(str(argv[2]),' ')
>    #_PlotColsInt=[]
>    '''for s in _PlotCols:
>        _PlotColsInt.append(int(s))
>    CountourPlots(__localdataSet,_PlotColsInt)
>'''
>    print
>    __data=__localdataSet.GetData()
>    print max(__data[:,11])
>if __name__ == "__main__":
>    drawAsciiFile()
>================================
>
>how i can link it to read my file examle.txt?

Instead of referring to argv inside your function, you should pass the file
name as a parameter.  That way, at a later time, you can pass it file names
from sources other than argv.  Also, there is no point in using leading
underscores for local variables.  That's only interesting for members of
the class.

You are splitting argv[2].  Ordinarily, sys.argv has already been split.
That is, "xxx.py file 1 3 5 7" will produce 6 elements in sys.argv.  If you
really want the columns as a single parameter, you'll have to use quoting:
    xxx.py file "1 3 5 7"

Something like this, maybe:

def drawAsciiFile(filename, columns):
    localdataSet = DataSet.DataSet( filename )
    PlotCols = [int(s) for s in columns.split()]
    ContourPlots( localdataSet, PlotCols )

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print 'Usage:  xxxx.py filename "3 5 7"' )
    drawAsciiFile( sys.argv[1], sys.argv[2] )
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list