Mostly Linux & Python syntax notes and hyperlinks.

Wednesday, March 3, 2010

vi split screen

:vsp
for vertical split screen.
Type control-W control-W to switch between them.

More at http://jmcpherson.org/windows.html

Tuesday, February 23, 2010

tar -z vs tar -j

tar -j uses bzip2
tar -z uses gzip

bzip2 compresses more but slower.

http://jeremy.zawodny.com/blog/archives/000953.html
http://en.wikipedia.org/wiki/Bzip2
tar -cvjf  new.tar dir_or_files
tar -xvjf new.tar
 Or should there be a "j" in the ".tar" so people know they need 'j' to extract?  Like: 
tar -cvzf new.tar.Z dir_or_files
tar -xzvf new.tar.Z

Friday, February 19, 2010

DOS scripting: nesting if's in place of AND

I want to say 'IF EXIST file1 AND EXIST file2'... but there's no AND.

But I can nest the if EXIST's:
if EXIST file1 (if EXIST file2 echo Both There)
For the ELSE, it gets more complicated:
if EXIST file1 (if EXIST file2 (echo Both There) ELSE echo Nope)
 Will echo Nope if file1 exists and not file 2, but not if file2 exists but not file1.  So you need:
if EXIST file1 (if EXIST file2 (echo Both There) ELSE echo Not 2) ELSE echo Not 1
 That will tell you either "Both There", or "Not 1" if only file1 isn't, or "Not 2" if only file2 isn't.

python: function with optional/default argument

>def testf(foo="blah"):
... print foo
...
>testf("hi there")
hi there
>testf()
blah

Wednesday, February 17, 2010

When Python shows you where the shared object is and says it doesn't exist

We had this error and it was driving me crazy:
    ImportError: [/.../..]/python2.4/lib/python2.4/lib-dynload/datetime.so: cannot open shared object file: No such file or directory
    [01,003839]PYTHON Error : Undefined Python Module :
When I did an ls for that directory, the shared object was in fact there.  It didn't even help to move the shared object into the same directory as my code.  I googled and looked on the python.org website and couldn't find any other mention of it.  That is, besides making sure the python path and load path were set and that the .so was executable, but that didn't solve the problem.

It turned out that the problem was that calls to the python module were embedded in a larger C program that had compiled and linked pointing to a 32-bit version of python2.4.  But the script that was trying to run the compiled code was pointing to a 64-bit version of python2.4.

Make sure all your python symbolic links are pointing to the same python.

To see if this is your problem, run the Linux "file" command on the .so and on the c-executable that's linked to the python module that's importing the .so.
 $file /[..]/python2.4/lib/python2.4/lib-dynload/datetime.so
/[..]/python2.4/lib/python2.4/lib-dynload/datetime.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped
$file cpgm
cpgm: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped
To solve this, re-compile cpgm in 64 bits, or point to the 32-bit version of python to run it.
$ file cpgm
cpgm: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), not stripped
You can get into this trouble on a 64-bit machine or VM since it will run 32-bit compiled code (whereas a 32-bit machine won't run 64-bit compiled code.)

Friday, January 15, 2010

python: time.strptime() instead of datetime.strptime()

Either one worked correctly from the python prompt, but when I embedded it in another tool,
fdate=time.strptime("SEPTEMBER 19 2010", "%B %d %Y")
worked, and
fdate=datetime.strptime("SEPTEMBER 19 2010", "%B %d %Y")
triggered an exception.

Monday, January 11, 2010

python: import

Here's a good link that explains how to use import.

If I have one file in the directory with only one function that I want to use in another file, I think it's clearer to say this at the top of the other python file:
from my_one_function_file import my_one_function
Then if I stop using that function, I know I can get rid of the import statement. When I look at the import statement, I know exactly why it is there.

The link recommends always using "import", which would mean:
import my_one_function_file
then calling my_one_function via
my_one_function_file.my_one_function()
In general, this recommendation does look like wise practice. It's more object-oriented.

Friday, January 8, 2010

python: mapping one list to another

I have two lists of strings. I want to associate the names of the first list with the values in the second.
Trying to do it with the mapping or dictionary type:
>>> x=['a','b']
>>> x
['a', 'b']
>>> y=['c','d']
>>> z={}
>>> z.fromkeys(x,y)
{'a': ['c', 'd'], 'b': ['c', 'd']}
That's not what I want..  Instead:
>>> d=dict(zip(x,y))
>>> d
{'a': 'c', 'b': 'd'}
>>>
That's as close as I can get it.
>>> d['a']
'c'
>>> d['b']
'd'
It seems awkward, but if you put it in a loop, it looks fine:
 >>> x=['a','b','c','d','e']
>>> y=['q','r','s','t','u']
>>> z=zip(x,y)
>>> z
[('a', 'q'), ('b', 'r'), ('c', 's'), ('d', 't'), ('e', 'u')]
>>> d=dict(z)
>>> d
{'a': 'q', 'c': 's', 'b': 'r', 'e': 'u', 'd': 't'}
>>> for key in x:
...   print key, "=", d[key]
...
a = q
b = r
c = s
d = t
e = u
>>>

python: looping through filenames in a directory

import os
...
dirname=os.getcwd()
ext_name=".txt"
...
try:
        for file in os.listdir(dirname):
            if file.endswith(ext_name):
                #do whatever
except:

Thursday, January 7, 2010

python: setting two things at once, links for getopt, csv.reader,

The code I need to modify uses csv.reader and getopt

and it can set two values at once!
 >>> a,b="c","d"
>>> a
'c'
>>> b
'd'
>>> a,b='f g'.split()
>>> a
'f'
>>> b
'g'