Sick of messages like this when logging in?
Last login: Thu May 21 23:31:00 2009
Simply
touch .hushlogin
and they’ll be all gone.
I found it difficult to find help on the various ways of modifying the filename in a vim script. Turns out that :help filename-modifiers uses the correct keyword.
I wrote a few notes on test-driven development.
A good page about single table inheritance.
cProfile and friends work well for profiling larger programs where things are split into functions. For more detailed investigation, however, you need something like line_profiler, where you can see output like this (trimmed):
Line # Hits Time Per Hit % Time Line Contents ============================================================== ... 202 1741 12946 7.4 0.0 for k in zip(K.I, K.J): 203 1740 222659 128.0 0.9 K[k] = randn() ...
Once installed, decorate any function worthy of analysis with @profile, run kernprof.py -l <scriptname.py> and then view results with python -m line_profiler <scriptname>.lprof. Works well.
I wrote down below about enumerate, which lets you access (index, item) pairs within a loop. I guess I thought it was a good idea, because I was about to write it here again. If you need both the index and item from a list, use enumerate:
for index, item in enumerate(items): pass
I haven’t used it in the intervening two months, but oh well.
A nice page explaining how to set up .gitignore rules for rails apps.
For some reason, Terminal.app appeared to be completely ignoring my request to disable the audible bell. This worked perfectly instead: xset b off.
Reminder: use ctrl+t and ctrl+d to increase/decrease the indent while in insert mode.
In smartindent mode, vim removes the indent if # is the first character on the line. This is annoying. I've tried various solutions that should work but don't. Here's one that should, and does:
:inoremap # a#<Left><BS><Right>
(Courtesy of this vim tip, but beware; it contains some false information as well.) Use ^v (ctrl+v) to enter the special sequences easily.
This code will fail:
l = []
def app():
l += (1,)
app()
It will fail with an UnboundLocalError. According to this website, this is because ‘If a value is assigned to a variable in a function body, the variable will be local, even if there is a global variable with the same name, and this global variable has been used before the assignment.’
This is quite different to the behaviour if you don’t try and perform an assignment, and so is a bit confusing. Of course, there are few cases where global variables are the correct tool, but sometimes they can be handy.
Could py.test be used for test-driven development in Python?
This is a better summary of git than I’ve seen before. I intend to write my own guide, sometime — mostly for my own learning — and will probably use some of this material to remind me what to write about.
Ruby has an unless statement equivalent to if not. Nice. And the RailsTips blog has some good suggestions on usage.
Compare the popularity of two search terms over time at Google Trends.
Start using a remote server to backup / distribute a local app:
# On the server, to avoid having copies of local files. git --bare init # On the client. git remote add origin ssh://hostname/path/to/repo git push origin master
A brilliant way of creating a ‘Site Specific Browser’ so that your dock contains a particular web app / site as a separate process. Makes a web app feel even more like it belongs as a fully-fledged app.
Undo the last git commit while leaving the tree as is. (Perhaps your commit message was wrong, or you forgot to add certain files.)
git reset --soft HEAD^
Set up a global ignore file for git using
git config --global core.excludesfile ~/.gitignore
The first javascript libraries I used were prototype and scriptaculous. However, I just started using jQuery, which is insanely great. The (css-like) selectors they use, for example, are so much fun: consider
$('li:visible:not(:has(img.checkmark.checked))').hide('slow')
This slowly hides and removes any visible list items that have no child elements that are images in the checkmark (tick for commonwealth readers) and checked (ticked) classes.
In other words, this slowly fades any checked off items that haven’t yet been hidden. So clean!
A few interesting things about Python that I had forgotten come from here. I’ve modified the examples.
First, you can use an instance’s namespace to give an interesting way to print strings. Sometimes this will be better:
# Basic version. print 'My name is %s and I am %d.' % (p.name, p.age) # Advanced version. print 'My name is %(name)s and I am %(age)d.' % p.__dict__
There is also the enumerate function, which returns (index, item) pairs. For example,
for (index, item) in enumerate(items): print index, item
Dictionaries have a setdefault method. Consider these alternative pieces of code:
# Naive piece of code. for (cat, amount) in data: if cat not in d: d[cat] = 0 d[cat] += amount # Simpler piece of code. for (cat, amount) in data: d.setdefault(cat, 0) d[cat] += amount # A cunning third option: setdefault also gets. for (cat, amount) in data: d[cat] = d.setdefault(cat, 0) + amount
But, better than all of these optons is the new defaultdict in Python 2.5.
from collections import defaultdict d = defaultdict(generator_function) # Example like those above. d = defaultdict(lambda: 0)
Finally, generator expressions: say we want to sum a large list that we generate on the fly (ie, we don’t care about the list):
sum(i**2 for i in xrange(1e5))
(I didn’t find much difference in evaluation time, though.)
Slicehost clearly know what they’re doing, with a simple, clean website, and nice tools — including a web browser based console for emergency situations.
The brief version first:
# fine. assert expr1, expr2 # WRONG! assert(expr1, expr2)
Be very careful with the syntax of assertions in Python! If you get the syntax wrong, the assertions will just always hold. This is particularly disastrous, as assertions are usually used to expose hidden bugs.
The exact syntax that you should use is assert expr1, expr2. This tests expr1 in the boolean sense, and raises an exception with value str(expr2) if expr1 is False. You can also just use assert expr1, which won’t give a special message. Note the total lack of brackets. If you insert parentheses, though, as in assert(thing1, thing2), what you are really doing is calling assert (thing1, thing2). That is, an assertion with expr1 being (thing1, thing2)! This will never evaluate to False, so you will never get an error.
Show the history of a particular file tracked by git, using
git log -p filename
A nice list of some of git’s functionality.
To immediately view the old version of a file tracked by git, use, for example,
git show HEAD^:filename
To create a new group on mac with a specific gid, then add a user, type
dseditgroup -o create -i 499 groupname dseditgroup -o edit -a username groupname
Bio of Jeff Bezos, Amazon’s CEO.
Nicely designed fruit website. Especially check out the charts which show fruit availability. Very clear.
This dictionary stores a limited number of entries, removing the oldest entries when it gets full. This could be used for a cache. Create a max-50-entry dictionary with f = FifoDict(50), for example.
class FifoDict(object): def __init__(self, maxentries=100): self.lastentries = [] self.mydict = {} self.maxentries = maxentries def __getitem__(self, k): return self.mydict[k] def __setitem__(self, k, val): if len(self.lastentries) >= self.maxentries: del self.mydict[self.lastentries.pop()] self.mydict[k] = val self.lastentries.insert(0, k) def __str__(self): return str(self.mydict) def __repr__(self): return repr(self.mydict)
(mt) Media Temple have a very nicely designed website (though quite small font sizes).
The letter k toggles keep mode in xdvi; with keep on, the horizontal and vertical page offset remains the same, even when switching pages.
Let’s say we want to remove the prefix one from a series of files. Use this:
for FILE in one* ; do mv $FILE `echo $FILE | sed 's/one//'` ; done
Thanks to the reader who pointed out that, in Debian GNU Linux (for example), you can also use rename, as in
rename 's/^one//' one*
Much cleaner, if rename is available.
In Safari (and possibly other browsers), you can log a small message to the debug console with the javascript command console.log(message). This is helpful for debugging.
amix.dk. Has some code segments, which might be interesting to look at if nothing else. (2009-02-26, appears to be broken.)
This page pointed out a bizarre but perfect way of fixing readline on mac. Apparently the GNU license doesn’t play nice for Apple so it doesn’t work by default. I did this:
sudo easy_install ipython sudo easy_install -f http://ipython.scipy.org/dist/ readline
And suddenly readline worked in ipython on mac.
Very odd, but also quite interesting industrial design. Reading this made me realise that sorting these entries by date is silly. I’ll fix this sometime.
I was shown an optimization solver that ran fine with certain parameter sizes, yet started segfaulting with larger problem sizes. It turned out that a desired single large, amorphous array could not be allocated within the available stack size limit. Increasing the stack size limit via ulimit -s 50000 (for example) fixed the problem.
You can view a shell’s resource limits using, for example,
monolith> ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) 6144
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 266
virtual memory (kbytes, -v) unlimited
Peregrine espresso in Washington, DC (map). The best espresso I’ve had in the US.
Useful page with git configuration details.
travelocity done right: ITA software.
After adding a VPN on mac, you might find it doesn’t really work. Add a new (ethernet, say) interface with network settings, and pick an address on the VPN’s subnet. Then you’ll be able to use services on that server by explicitly referring to that IP.
Sadly, mac’s ‘seemless user interface’ doesn’t seem to carry over to leopard server…
(Only) somewhat ok page about re-raising exceptions.
Set up a new tracked repository.
git remote add remotename username@server:~/repo/
Expire a user’s password, requiring them to change it immediately upon their next login:
passwd -e username
Nice looking free fonts: exljbris and Gentium.
Leopard irritatingly prevents you from creating an ssh tunnel to another computer and then connecting a samba share to your localhost. Get around this by first creating an alias for your localhost interface:
sudo ifconfig lo0 alias 127.0.0.2 up
Then, forward the ports via ssh, eg with
sudo ssh -N -L 127.0.0.2:139:localhost:139 -L 127.0.0.2:445:localhost:445 user@server.com
Finally, connect using Finder to smb://127.0.0.2/sharename.
Paste last executed command in vim: ":p.
Nice way of having two different kinds of syntax highlighting within one file, with vim.
Great setup guide for openvpn. Goes well with Tunnelblick.
Check for common memory errors in C with valgrind. Also profile cache misses and branch mispredictions.
Sample usage:
# Check memory accesses. valgrind --tool=memcheck ./prog # View cache and branch profiling information. valgrind --tool=cachegrind ./prog
For more detailed output from valgrind, add the -g tag to your gcc compilation flags to add debugging information to the binary.
The key component of psfrag is the position specification. Here is the relevant syntax:
\psfrag{tag}[position]{Replacement text}
The position is up to two letters; one from {t, b, B, c} (top, bottom, baseline, center) and one from {l, r, c} (left, right, center). Center is assumed if either is omitted.
Infinite labs: excellent little applications for mac, including PlugSuit and Afloat.
Nice color scheme for vim: ir_black.
Disable highlighting of spurious _ and ^ symbols in LaTeX files as errors (say, when a macro is used to start equation environments).
autocmd Filetype tex syntax clear texOnlyMath
Use multiple processes (and hence cores) with make by using make -j.
Disconnect quickly, even when ^d is not allowed, with ~..
A quick way of finding out where a Python package lives:
>>> import pylab >>> print pylab <module 'pylab' from '/usr/lib/python2.5/site-packages/pylab.pyc'>
Run a Python script through a profiler, then interpret the output. I don’t think it’s worth the bother going through iPython; I couldn’t get %run -p to work properly.
At the shell:
> python -m cProfile -o outprof.tmp examples/bb30.py
Then, in iPython:
>>> import pstats >>> p = pstats.Stats('outprof.tmp') >>> p.strip_dirs().sort_stats('cumulative').print_stats(50)
A reminder of how tee works comes from hunterdavis.com.
./prog | tee >(gzip > logfile.tgz)
From this blog, where there is an installer for ipe for mac, it seems that to convert back from a pdf to an eps the following is required.
gs -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=epswrite -sOutputFile=new.eps old.pdf
find and sed operate slightly differently on a mac. Here’s some code that replaces 4.3 with 4.4 in all Makefiles, recursively.
find . -name 'Makefile' -exec sed -i -e 's/4\.3/4.4/g' {} \;
The numbered register "0 contains the text from the most recent yank command, while the numbered register "1 contains the text from the most recent delete or change command. If the delete or change was ‘small’, though, (ie, less than one line), the small delete register "- is used instead.
This means that if you yank some text to move it elsewhere, then rearrange some stuff (including deleting, say, newlines), you can still easily retrieve the text that you yanked with "0p.
I made some aliases for use when writing
documents. Add these
(perhaps unwieldly) lines to your .bashrc:
# Next command on a single line. alias lr='latex -file-line-error-style -interaction=nonstopmode $LR.tex > /tmp/latexoutput || cat /tmp/latexoutput ; grep -e "\(LaTeX Warning\)\|\(Overfull\)\|\(Underfull\)" /tmp/latexoutput | grep -v "There were undefined references"; killall -SIGUSR1 xdvi.bin 2> /dev/null' alias xdj='xdvi -paper us -expert -keep $LR.dvi 2>/dev/null >/dev/null&'
Note: depending on your system, you made need to replace xdvi.bin with xdvi-xaw3d.bin.
Then, when you are preparing a document, you can do this:
> LR=myfilename # set filename. The suffix .tex is added automatically. > lr # compiles your latex document. > xdj # displays your latex document.
This sounds pretty ordinary, but these aliases take advantage of these things:
lr will only show the output if there was a problem. This avoids polluting your screen.
lr will never stop with an error while running
and leave you punching
keys in the hopes of getting back to the command line.
will tell you the line number of any errors.
lr will automatically kick xdvi after compiling the document to show your changes. No need to click on xdvi or wait for it to notice the file changed. I really like the way xdvi permits such updates without flickering.
To see the assembly instructions generated for a piece of C code, use this:
gcc -c -g -Wa,-ahl src.c
Find out the macros, and values, predefined by gcc (includes useful things like __INT_MAX__).
gcc -dM -E - < /dev/null
At http://code.google.com/apis/chart/. Nice charts, like this one.
You generate these by going to a specially written url such as http://chart.apis.google.com/chart?cht=lc&chs=200x125&chd=t:50.0,5.0,4.0,93.0,17.0,18.0,13.0,15.0 for the chart above. It looks like it would be a bit of work to translate from a particular application to this api, though.
Sampling from a discrete distribution, where p is a vector of probabilities:
min(find(rand() < cumsum(p)))
Woah, typing s/search/repl/ in a Skype conversation actually performs a replacement for both parties on your last message (only). You can also remove the last messages with s///. It turns out you can also do the same by right clicking.
Have a look at this bizarre Matlab behaviour:
>> 1 / [1 2 3]' ans = 0 0 0.3333
Apparently, this is how they wanted it. Excusing the use of
as if it were mathematics, we have the equivalence:
leading to the bizarre conclusion that in Matlab,
Of course, the syntax that I really wanted was 1 ./ [1 2 3]'. This is fully consistent with Matlab’s elementwise operations, but would it be so bad if 1 / [1 2 3]' just did not work, especially rather than silently giving you something totally weird but the same shape?
I’ve often tried to use find, but have never really got over its syntax. It seems that it’s one of those powerful tools that’s not necessarily obvious to use at first. Here’s one way to use it:
find . -name '*.sw*' -print
prints filename which have .sw in them (say, leftover vim swapfiles).
You can combine this with grepping:
find . -name "*.bib" -exec echo {}; grep "tribute to" '{}'
looks for the words ‘tribute to’ in files with the extension .bib.
Nice page describing enhancements vim has for Python. Will try the omni-complete from this page soon.
Exceedingly useful for cutting and pasting from the clipboard in vim without indentation problems: :put +. (More info.) Depending on your system, :put * might be what you’re after instead.
Version 4.3 of gcc, apart from significant improvements, has this nice feature:
> gcc -c -Q -Os --help=optimizers The following options control optimizations: -falign-jumps [disabled] -falign-labels [disabled] -falign-loops [enabled] # ---8<--- -fwhole-program [disabled] -fwrapv [disabled]
A few code snippets for timing in C. First, tic() and toc() functions. These work like Matlab: tic() starts the timer, and toc() stops it and gives formatted output. There’s also a tocq() function which just returns the number of elapsed seconds.
#include <time.h> static clock_t tic_timestart; void tic(void) { tic_timestart = clock(); } float toc(void) { clock_t tic_timestop; tic_timestop = clock(); printf("time: %8.2f.\n", (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC); return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC; } float tocq(void) { clock_t tic_timestop; tic_timestop = clock(); return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC; }
A bit of code for counting flops: first, a statistics structure, then some defines.
/* CFDIV divides 1/a[3] * CFADD adds c[12] + d[12] * CFMUL multiplies b[4] * h[7] * CFASN assigns A[5] = 0 * CFSTO stores A[5] = (...) * CFTFR transfers A[5] = b[7] * CFNTF negated transfers A[5] = -b[7] * CFCOM compares c[5] < b[7] */ typedef struct statistics_t *statistics; typedef struct statistics_t { long div; long add; long mul; long asn; long sto; long tfr; long ntf; long com; } Statistics; #define CFDIV(i) if (countflops) stats->div += i #define CFADD(i) if (countflops) stats->add += i #define CFMUL(i) if (countflops) stats->mul += i #define CFASN(i) if (countflops) stats->asn += i #define CFSTO(i) if (countflops) stats->sto += i #define CFTFR(i) if (countflops) stats->tfr += i #define CFNTF(i) if (countflops) stats->ntf += i #define CFCOM(i) if (countflops) stats->com += i