Feb 20, 2011

Dictionary of Methods/Functions (Python)

Construct a dictionary with String (or other) keys and unbound methods or functions as values. During execution, use the string keys to select which method or function to execute. Can be used for simple parsing of tokens from a file thru a kind of object-oriented case statement.

import string
def function1():
print "called function 1"

def function2():
print "called function 2"

def function3():
print "called function 3"

tokenDict = {"cat":function1, "dog":function2, "bear":function3}

# simulate, say, lines read from a file
lines = ["cat","bear","cat","dog"]
for line in lines:
    # lookup the function to call for each line
    functionToCall = tokenDict[line]
    # and call it

It's embarrasingly simple really, but I use it a whole lot. Instead of functions, methods might also be used (self.method1), remember to follow the binding rules if you use methods directly from classes.

You can also pass in any attributes (including those read from a file) to the functions you call. The possibilities are pretty diverse.

I primarily use this in places where in other languages I might want a case statement. I also use it to provide a poor man's way to parse command files (say an X10 macro control file).

the general idea and concept from matias

Feb 17, 2011

How to Install NVIDIA drivers manually on Lucid Lynx Ubuntu 10.04

  • Download Newest Nvidia drivers from their website
  • Open module blacklist as admin
  • Code: sudo gedit /etc/modprobe.d/blacklist.conf
  • Add these lines and save:
  • Code: blacklist vga16fb blacklist nouveau blacklist rivafb blacklist nvidiafb blacklist rivatv
  • Uninstall any previously installed Nvidia drivers:
  • Code: sudo apt-get --purge remove nvidia-*
  • Reboot your computer
  • When an error message pops up saying that Ubuntu cannot load Nvidia drivers, choose Exit to terminal (Exit to console)
  • Login and cd to the directory where you saved your file
  • Install drivers
    Code: sudo sh NVIDIA-Linux-x86_64-195.36.24-pkg2.run
  • Start GDM
  • Code: sudo service gdm start Enjoy
Enjoy :)

Feb 1, 2011

SCP (Secure Copy)

Copying file to host:
scp SourceFile user@host:directory/TargetFile
Copying file from host:
scp user@host:/directory/SourceFile TargetFile
scp user@host:/directory/SourceFile TargetFolder
Note that if the remote host uses a port other than the default of 22, you can specify it in the command. For example, copying a file from host:
scp -P 2222 user@host:directory/SourceFile TargetFile