Mostly Linux & Python syntax notes and hyperlinks.

Tuesday, March 27, 2012

Linux: moving a directory into itself

I didn't know you could do this.

OK, I had a directory with code that I was changing, and I had a bak/ directory in which I was saving current versions.

I had a some files with the string "bak" in them.  I tried to move *bak into bak and lost the entire directory.

Yeah, it was late in the day.

[fire@ice frost]$ mv _* bak
`_some.cmd' -> `bak/_some.cmd'
[fire@ice frost]$ mv say_bak.cmd bak
`say_bak.cmd' -> `bak/say_bak.cmd'
[fire@ice frost]$mv *bak bak
`bak' -> `bak/bak'
`the_world.cmd.bak' -> `bak/the_world.cmd.bak'
mv: cannot move `the_world.cmd.bak' to `bak/the_world.cmd.bak': No such file or directory

Thursday, March 15, 2012

python: ImportError: No module named slythy.toves

Why can't python see my module when it's right there?

I was copying code from one installation to another and thought I had everything.
But, the code ran on one system but not the other.

I finally found the difference: an empty (all comments) file called __init.py__

That is, if in twas/brillig.py you have "from slythy.toves import gyre"
and in twas/slythy/toves.py you define the gyre class,
you need a file called __init.py__ in the twas/slythy/ directory, or you'll get an error like:

    ImportError: No module named slythy.toves
    Python Error: Undefined Python module brillig


There are definite disadvantages to learning python piece-by-piece as you go along.  I was checking environment variables and everything before finally finding out about __init.py__


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.