Mostly Linux & Python syntax notes and hyperlinks.

Friday, October 22, 2010

Python IDE: PyScripter

Allen is using Eclipse.  Janak is using Stani's Python Editor.  I tried downloading Stani's some months back, but it never ran.  Janak says it needs configuring.

So I read A Beginner's Guide to IDE's, consulted http://wiki.python.org/moin/PythonEditors, and decided to go with the lightweight and "fast" single-language IDE PyScripter.

It downloaded and installed just fine.  I like that you can choose which version of Python to open it up with from the Open Program/PyScripter menu.  We're currently using 2.6, so I can look at our code in the IDE with 2.6 interpreting it.

Linux: uname -a tells you what version of Linux you're running

$ uname -a
Linux rhel54-64 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

There are other options to get selected info. You can do a "man uname" to see them.

Thursday, August 12, 2010

python: iterating through csv.reader record

import csv

myReader=csv.reader(open(file_name,"rb"),delimiter='\t', quoting=csv.QUOTE_NONE) 

for record in myReader:
    print("len(record)="+str(len(record))
    for i in range(len(record)):
        print("record " + str(i) + "=" + record[i])

Though watch for Error Message
"not all arguments converted during string formatting"

python: printing more than one integer in a string

>>> a=5
>>> b=6
>>> c=7
>>>print("a=%d,b=%d,c=%d" % (a,b,c))

a=5,b=6,c=7

Monday, June 28, 2010

Linux: Testing for string equality in ksh

In .ksh this is how to do it:
$ plex="SIMPLEX"
$ if [ "$plex" = "SIMPLEX" ]
> then
> echo y
> fi
y
$ if [ "$plex" = "DU" ]
> then
> echo y
> else
> echo n
> fi
n

All of these do not work:
$ plex="SIMPLEX"
$ if (($plex=="SIMPLEX"))
> then
>   echo y
> else
>   echo n
> fi
y
$ if  (($plex=="DUP")) then echo t
> fi
t
$ if (($plex="SIMPLEX"))
> then
>   echo y
> else
>   echo n
> fi
n
$ if [["$plex"=="SIMPLEX"]]
> then
>   echo yes
> fi
ksh: [[SIMPLEX==SIMPLEX]]: not found
$ if [$plex="SIMPLEX"]
> then
>   echo yes
> fi
ksh: [SIMPLEX=SIMPLEX]: not found

Friday, June 25, 2010

Linux/Python: setting shell variable to value returned from python function

From stackoverflow.com.  This works with python 2.4:
$ f=$(python foo.py)
$ echo $f
FOOO
Where foo.py is:
def foo():
  print "FOOO"

if __name__ == '__main__':
   foo()
There was a recommendation to use sys.stdout.write("%s\n", fooPy()) instead of print(), but I got an error about sys.stdout.write() expecting one not two input variables when I tried it. If I use sys.stdout.write("%s\n" % foo()) or print(foo()) in the main clause then the output is:
$ python foo.py
FOOO
None
$ w=$(python foo.py)
$ echo $w
FOOO None
With
import sys
def foo():
  sys.stdout.write("FOOO")
if __name__ == '__main__':
   foo()
I get
$ python foo.py
FOOO$
$ w=$(python foo.py)
$ echo $w
FOOO
$

Thursday, June 24, 2010

Linux: One minus the Line Count

To count the number of output records minus the header in one line of script:
CNT=$(($(wc -l file_with_header | awk '{print $1}')-1))

Tuesday, June 22, 2010

Linux: yesterday's date

This is cool:
$ date
Tue Jun 22 14:11:25 EDT 2010
$ date --date='1 day ago'
Mon Jun 21 14:11:36 EDT 2010

Thursday, June 10, 2010

Linux: remove header and trailer lines from input file

  • Use "sed '$d' infile > outfile" to remove the first line.
  • Use "sed '1d' infile > outfile" to remove the last line.
$ cat hf.dat
head
one
two
three
foot
$ sed '$d' hf.dat > nofoot
$ cat nofoot
head
one
two
three
$ sed '1d' nofoot > noheadfoot
$ cat noheadfoot
one
two
three
$

Tuesday, June 8, 2010

python: take the average of a column of numbers from a file

Could use smoothing out, but easier than getting out the calculator:
def average(infile):
  acount=0
  asum=0
  f=open(infile,"r")
  g=open("avg_"+infile,"w")
  for line in f:
      num=float(line.strip())
      asum= asum + num
      acount=acount+1
      g.write('count=%d,' % acount)
      g.write('num=%f' %  num )
      g.write(',sum=%f \n' % asum)
  if acount > 0:
     average=asum/acount
     g.write('\n sum=%f \n' % asum )
     g.write('average=%f\n' % average)
  f.close();
  g.close();

def main(infile):
    average(infile)

if __name__ == "__main__":
    import sys
    main(*sys.argv[1:])

Note that float() of blank returns zero, so make sure there's no blank line at the end of your input file. Or, improve the script to check for that sort of thing.
>>> float()
0.0
>>> float( )
0.0
>>>