Send As SMS

9/21/2006

find the key for the minimum (or maximum) value in a python dict

the trouble with dicts or hashes is that there's no easy way to search for a value and return a key.

for instance, i'm not aware of a simple python command to find the dict key associated with the smallest value in the dict.

nevertheless, there are quick and dirty ways to pull off the above job.

take dictionary a:

>>> a_dict = {"me": 5, "you": 6, "she":200}
to find the key associated with the minimum value, we can "flip" the dictionary (assuming there are no duplicate values):
>>> b_dict = dict(map(lambda item: (item[1],item[0]),a_dict.items()))
now, we use the min (or max) built-in function to find the smallest (or largest) value:
>>> print b[min(b.keys())]


installing the curl extension for php

my wordpress installation on my debian box demanded that i have the curl extension for php.

to install that extension:

$ sudo apt-get install php4-curl
$ sudo apache2ctl graceful


installing the curl php extension

my wordpress installation on my debian box demanded that i have the curl extension for php.

to install that extension:

$ sudo apt-get install php4-curl
$ sudo apache2ctl graceful


spell-check keyboard shortcut in emacs

you can spell check in emacs with the command:

$ M-x ispell-buffer
however, that's not very convenient to do over and over; to create a shortcut to the spell-checker, add the following to your .emacs file:
(global-set-key "\C-c" 'ispell-buffer)
when you restart emacs, you'll find that pressing ctrl-c will instantly pull up the spellchecker.


how to open zip files in debian/linux

to open a zip file in debian, install "unzip"

$ sudo apt-get install unzip
you can then call unzip to uncompress your zip file.


how to uncompress a .tar.gz file

to decompress a .tar.gz file, try:

$tar -zxvf myfile.tar.gz


8/4/2006

read a list backwards in python

to iterate over a list in reverse order in python, i use the 'reverse' method. i'm sure there are faster ways to do this in terms of running time, but its hard to conceive of anything easier to implement.

here goes: to iterate over 'my_list' backwards, write:

my_list.reverse()
for item in my_list:
print item


7/30/2006

boot?


7/27/2006

read a file from the command line in perl

to read in a file and print its contents (filename specified as the first command-line argument):

my($my_file) = $ARGV[0];
open(MYFILE,$my_file);

while ()
{
print $_;
}


7/26/2006

build sturdy speaker or interconnect cable dirt cheap

when i needed to wire my stereo to my music server 60 feet away, i immediately thought of a forum post a couple months back, where a fellow used ethernet cable to produce cheap audio cable.

it made sense. cat5 ethernet cable appears relatively well shielded against noise-inducing radio waves: each of the 8 small wires in cat5 cable is insulated; all 8 of those insulated wires are further insulated with one big outer housing. in fact, i bet cat5 underwent a lot of testing to make sure that it was robust enough to outside interference to cleanly transmit hundreds of megabits of information per second. something tells me the voodoo priests who make audio cables don't construct tests of the same rigor as the computer engineers.

and more importantly, cat5 ethernet cable is dirt cheap. high end interconnect/speaker cable will run you $5 a foot. that means i'd have to pay $300 to wire my stereo to my music server. fat chance. on the other hand, cat5 can be bought at custom lengths from home depot @ 9 cents a foot. that's 50 times cheaper than the ritzy (and not necessarily better) cables.

it turns out to be super-easy to make audio cables from cat5 ethernet cable:

[1] procure ethernet cable (i suggest home depot - they're so cheap)

[2] procure appropriate plugs. if, like me, you're making interconnects for connecting things like sound cards to pre-amps, you'll probably need these rca plugs (sets you back a whole $3!):


3] use a sharp knife, or preferably, some wire cutters, to expose the ends of the cat5's individual cables:


4] perhaps the hardest part (which isn't that hard at all) comes up now. you need to pick which of the colored wires will go to your right plug and which will go to your left plug. make sure to keep track of the corresponding group wires for each colored wire. the ground wires should be white and clearly be wrapped around each colored wire.

5] attach two of the wires to the positive pin of each plug and a different pair of wires to the negative pin of each plug. it doesn't matter which color goes to the positive or negative pin, or even if you know which is the postive or negative pin of a plug. instead, all you need to do is be consistent - if on one end of your cable, the blue and green wires go to the ground pin of the red plug; well, on the other end of the wire, you should have an identical wiring configuration on the other red plug.

6] you should have something that looks like this:


plug 'em in and enjoy!


get need for speed 2 se working in windows xp

i rediscored the other day one of my middle-school games - need for speed 2 se (special edition). unfortunately, the install kept crashing on the windows XP box i had handy.

upon running the game, i kept getting the error:

initmem - INSUFICIENT MEMORY TO CONTINUE FILE win\initmem.c LINE 242
to fix this problem, i found out that you need to:

1.

Open System in Control Panel.

2.

On the Advanced tab, under Performance, click Settings.

3.

On the Advanced tab, under Virtual memory, click Change.

4.

Click Drive [C:].

5.

Under Paging file size for selected drive, click Custom size, and type 512 megabytes in the Initial size (MB) and Maximum size (MB) box, and then click Set.


7/22/2006

modify the header or body of gallery2 pages

you can modify the header and/or body of gallery2 pages to customize your photo pages. for instance, i've added links back to my homepage and inserted some statcounter code to my gallery.

to make changes, first figure out what theme you're using. (go to site admin --> themes.)

i'll go through an example with my default template: matrix.

navigate to your gallery2 install and descend into the template folder:

$ cd ~/gallery2/themes/matrix/templates/
create a folder named 'local' (don't change this name):
$ mkdir local
copy your theme.tpl file into this folder:
$ cp ./theme.tpl ./local/
now, your header information lives between the and tags in the code. you can also add code that you want to run in the body of pages. to prevent formatting errors, i'd advise adding the code right before the


7/18/2006

how to make a transparent histogram in matlab


to make a partially transparent histogram in matlab, use the facealpha setting.

for instance, the following code produces the histogram seen above:

figure
hist(data1,20)
h = findobj(gca,'Type','patch');
set(h,'FaceColor','r','EdgeColor','w','facealpha',0.75)
hold on
hist(data2,20)
h = findobj(gca,'Type','patch');
set(h,'facealpha',0.75);
ylabel('counts')
xlabel('gene-tree-length/species-tree-length')
legend('no HGT','HGT')
title('p = 0.5');


create a directory in python

assuming you're running some *nix (including mac os x), here's how to create a directory from within a python script. note that we need to check if the directory exists before creating a directory (if you try and overwrite a directory, python will throw an error).

here goes:

import os;
...
dirname = "my_dir_name"
if not os.path.isdir("./" + dirname + "/"):
os.mkdir("./" + dirname + "/")


note that the last line needs to be indented (silly blogger won't let me indent that line!)


numerically integrate f(x) in matlab

let's say you've got y = f(x), where x and y are both matlab arrays.

to numerically integrate f(x), just use the command trapz:

>> area_under_the_curve = trapz(x,y);


get the time in python

to get the time in python:

import time;
...
time.localtime()


how to get latex working on mac os x

to get latex and pdflatex running in mac os x, first download i-installer.

then, install the 'tex' package found in the i-installer.

then, compile a tex file into a pdf with the command:

>>pdflatex mytexfile.tex


My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://stinkpot.afraid.org:8080/tricks/ and update your bookmarks.