Mostly Linux & Python syntax notes and hyperlinks.

Wednesday, May 23, 2012

windows 7: how to turn off auto-expansion of windows shoved to the edge of the screen

A windows thing that was driving me crazy, with my habit of mouse-button down, wrist flick, to shove windows out of the way from the window I wanted to look at.  In the version of Windows that arrived with new PC's, the wrist-flick was causing the unwanted window to expand and fill the entire screen.

To turn this off, under control panel, select "Ease of Access", then "Change how your mouse works".  Then check the box that says:

Prevent windows from being automatically arranged when moved to the edge of the screen.

Control Panel\Ease of Access\Ease of Access Center\Make the mouse easier to use\


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'
>>>