Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, November 29, 2011

python: note the non-intuitive behavior of range(start,end+1)


range(a,b) starts at a and ends at b-1

>>> for n in range(1,3):
...    print str(n)
...
1
2
>>>

You could introduce bugs in your code by not being careful of this.

Monday, November 28, 2011

python: 2-D dictionary, somewhat dynamically filled

counts=dict(A=dict(),B=dict())
for n in range(0,6): 

    Fn='F'+str(n)
    counts['A'][Fn]=n
    counts['B'][Fn]=-n



Then counts = {'A': {'F0': 0, 'F1': 1, 'F2': 2, 'F3': 3, 'F4': 4, 'F5': 5}, 'B': {'F0': 0, 'F1': -1, 'F2': -2, 'F3': -3, 'F4': -4, 'F5': -5}} 

That is:
for t in ('A','B'):
     for n in range(0,6):
         Fn='F'+str(n)
         print("counts[%s][%s]=%d" % (t,Fn,counts[t][Fn]))
will print


counts[A][F0]=0
counts[A][F1]=1
counts[A][F2]=2
counts[A][F3]=3
counts[A][F4]=4
counts[A][F5]=5
counts[B][F0]=0
counts[B][F1]=-1
counts[B][F2]=-2
counts[B][F3]=-3
counts[B][F4]=-4
counts[B][F5]=-5



Monday, October 24, 2011

python: real path after symbolic link

After calling a library method that renamed a file and linked it, I needed the name it renamed it to.

  filename=os.path.basename(os.path.realpath(symbolicLinkName))

 More at http://www.python.org/doc//current/library/os.path.html

Friday, August 19, 2011

python: Define an empty function using pass

I want to define a function for a parent class that can be used by child.
Using pass seems to be the proper way to do it:
def emptyFunction(self):
     pass

Thursday, June 23, 2011

python: get the last directory name in a path

If I want to pull just the parent directory name out of a long path to a file:
>>> import os
>>> pth="/abeja/blanca/zumbas/ebria/de/miel/en/mi/alma/y.te.tuerces"
>>> os.path.split(os.path.dirname(pth))[1]
'alma'
>>>

Friday, May 27, 2011

Linux: grep for $

This was harder than grep for tab.
I found instructions to escape the special character, but none of these
grep $ *
grep \$ *
grep "$" *
grep "\$" *
grep - $ *
grep "$" *
grep '$' *
egrep '$' *


worked until I read "Note that a $ sign loses its meaning if characters follow it" and remembered I was looking for a $ followed by a capital letter. So this works:

grep '$[A-Z]' *

but I still don't know how to find a $ by itself.

Wednesday, May 25, 2011

Python: os.path.lexists() vs os.path.exists() before os.symlink()

We were having intermittent failures with shutil.copy(src,dest) and decided to replace it with just a symbolic link, using os.symlink(src,dest). This triggered an error when we tried to create a symbolic link with the name of an existing file. So the code checks first. It uses os.path.lexists(), since that will return True for broken as well as valid links. os.path.exists() returns True for valid links, False for broken ones.
The os.remove() works whether or not the link is broken or valid or an actual file.
if os.path.lexists(dest):
    os.remove(dest)
os.symlink(src,dest)
(For more detail, see also June2013)

Linux: paste and diff -b

tidbits recently of use to me:
  • paste to put two files together by merging their columns, without the complications of join
  • diff -b to ignore blank lines etc, when comparing how my new code is different from the old code.