Mostly Linux & Python syntax notes and hyperlinks.

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'

Tuesday, December 1, 2009

Linux: basename

basename is cool.
Takes out any piece of a filename you'd like to get rid of.  It doesn't take wildcards though.
$ basename ABC_s.vpf _s.vpf
ABC
$ basename AB123 3
AB12
$ basename AB12_34 12*4
AB12_34
It also removes the directory names from the path:
$ basename a/b/c/d/AB12_34
AB12_34
$ basename a/b/c/d/AB12_34 2_34
AB1

Wednesday, November 25, 2009

Linux Link

head, tail, cat, tac, nl, fmt, fold, tr, pr, basename, dirname, all described as Dogs of the Linux World.

Linux: EBCDIC to ASCII conversion

To convert from EBCDIC to ASCII:
$ iconv -f EBCDIC-US -t ASCII  EBCDICinput > ASCIIoutput
You can also use dd:
$ dd if=EBCDICinput of=ASCIIoutput conv=ascii

$ file -i EBCDICinput 
EBCDICinput : text/plain; charset=iso-8859-1
$ file ASCIIoutput
ASCIIoutput: ASCII text, with very long lines, with no line terminators
Though I've found the first one (iconv) will keep pipe characters as "|", and the second (dd) will have them as "!" in the ASCII output file.

Wednesday, September 16, 2009

Windows Explorer XP 5.1 Windows often hangs when I open a file

The issue is that my windows explorer (what a Mac user would call the Finder Window) keeps hanging on me.  In the folder-name bar, it has the folder name, then "(Not Responding)". I end up clicking the red X at the top right to close it, then selecting 'End Program', letting it start up again, and then opening a new windows explorer and navigating my way back to the folder.  This happens often when I double click to open a file.  It happens frequently and is getting annoying.

Someone suggested that I defragment my hard drive.  I've run the defragger over and over until there's not much left to de-frag.  No help.

Search on Microsoft site didn't help.
Attempt to do a Microsoft updatate didn't show any patches for XP 5.1, service pack 3.

Google search resulted in http://www.analogduck.com/main/explorer_hangs  which links to http://www.annoyances.org/exec/forum/winxp/t1167888467

I followed the advice from http://www.annoyances.org/exec/forum/winxp/t1167888467 here:
To stop ctfmon from autostarting and running all the time:
Control Panel>Regional and Language Options>Languages>Details>Advanced,
 and check the box that says "Turn off advanced text services." 
And I've gone into the Task Manager and stopped ctfmon.exe.

I've gone into the Control Panel/Administrative Tools/Services window and stopped the SSDP Discovery Services (along with other services that I didn't think I needed.)

Let's see if it helps...

2:56pm.  The (Not Responding) happened when I tried to open a text document by double-clicking, but it didn't last as long.
7pm.  No, I'm still getting the hanging when I try to double-click to edit a text file.  And I have re-booted since the previous changes.
Oh well.

Tuesday, September 15, 2009

python day 2: functions, if, else, TypeError

To create a function, define it before you need to use it, and don't forget the colons. Not for the if either. (Python seems to like colons. It's kind of like how C likes semicolons, and Lisp likes parentheses.)

import os, sys

def run_cmd(cmd,fd):
try:
if sys.platform.startswith("win")
print cmd
fd.write('running cmd from win: '+cmd+'\n')
EXITCODE = os.system(cmd) >> 8
#Here's where I got the TypeError:fd.write('cmd returned '+EXITCODE+'\n')
else:
import popen2
process = popen2.Popen3(cmd)
EXITCODE = process.wait() >> 8
return EXITCODE
except:
print "run_cmd: Some Exception Caught."
fd.write('run_cmd: Some Exception Caught\n')

The TypeError complained:

    TypeError: cannot concatenate 'str' and 'int' objects

I didn't realize Python was so fussy about types. I thought it was a cavalier sort of Javascript kind of interpreted thingy. OK, so to solve that...it looked like I needed to use a sort of C syntax with the print statements, but that wasn't quite right. I tried it, but got another TypeError:

      fd.write('cmd returned %d\n',EXITCODE)
TypeError: function takes exactly 1 argument (2 given)

Oh, wow, this really is fussy. Experimenting with the shell again:

    >>> num=3
>>> num
3
>>> print "%d" %(3)
3
>>> print "num=%d" %(num)
num=3
>>> print "num=%d" % num
num=3

OK, so it's not C/Java. I need to say:

    fd.write('cmd returned %d\n' % EXITCODE)

Now it works.

One weird thing is that the tutorial I've been using has both %d and %i listed as "signed decimal integer". The python.org page on string formats only lists the 'd' as an integer type, so I'll stick with that. Researching further, there's a section on String Formatting Operations that lists both d and i again. Must be some historic reason for it...