fdate=time.strptime("SEPTEMBER 19 2010", "%B %d %Y")worked, and
fdate=datetime.strptime("SEPTEMBER 19 2010", "%B %d %Y")triggered an exception.
Mostly Linux & Python syntax notes and hyperlinks.
fdate=time.strptime("SEPTEMBER 19 2010", "%B %d %Y")worked, and
fdate=datetime.strptime("SEPTEMBER 19 2010", "%B %d %Y")triggered an exception.
from my_one_function_file import my_one_functionThen 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.
import my_one_function_filethen 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.
>>> x=['a','b']That's not what I want.. Instead:
>>> x
['a', 'b']
>>> y=['c','d']
>>> z={}
>>> z.fromkeys(x,y)
{'a': ['c', 'd'], 'b': ['c', 'd']}
>>> d=dict(zip(x,y))That's as close as I can get it.
>>> d
{'a': 'c', 'b': 'd'}
>>>
>>> d['a']It seems awkward, but if you put it in a loop, it looks fine:
'c'
>>> d['b']
'd'
>>> 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
>>>
>>> a,b="c","d"
>>> a
'c'
>>> b
'd'
>>> a,b='f g'.split()
>>> a
'f'
>>> b
'g'
$ basename ABC_s.vpf _s.vpf
ABC
$ basename AB123 3
AB12
$ basename AB12_34 12*4It also removes the directory names from the path:
AB12_34
$ basename a/b/c/d/AB12_34
AB12_34
$ basename a/b/c/d/AB12_34 2_34
AB1
You can also use dd:$ iconv -f EBCDIC-US -t ASCII EBCDICinput > ASCIIoutput
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.$ 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
And I've gone into the Task Manager and stopped ctfmon.exe.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."
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...