MySQL has an operator SOUNDS LIKE .
a sounds like b
is equivalent to
soundex(a) == soundex(b)
Cute, though not something I would use in production code.
Monday, December 29, 2008
Thursday, December 25, 2008
Put last word in quotes
Really useful tidbit from the readline manual:
$if Bash
# quote the current or previous word
"\C-g": "\eb\"\ef\""
$endif
Put the following in your .inputrc:
$if Bash
# quote the current or previous word
"\C-g": "\eb\"\ef\""
$endif
Now cntrl-g will surround the previous or current argument with quote characters.
Tuesday, December 23, 2008
Fix up screen good
Awhile ago, I set up screen on another machine.
I was setting up my screen config for a new machine, and I had completely forgotten how I had fixed it up.
Here's what I did:
I was setting up my screen config for a new machine, and I had completely forgotten how I had fixed it up.
Here's what I did:
- don't bother with aliasing 'screen' to 'TERM=screen screen'
- to fix backspace-delete, instead put 'bindkey -k kD stuff ^H' in .screenrc (note ^H should be entered as cntrl-V-cntrl-H to get a literal)
- to get color shell prompts in bash:
- there should be a line in .bashrc that looks like case "$TERM" in xterm-color) color_prompt=yes;; esac
- add screen) color_prompt=yes;; into the body of the case statement
- to get window titles added correctly, for bash:
- add this to .screenrc: shelltitle '$ |bash'
- add this to .bashrc: PS1=$PS1'\[\033k\033\\\]'
- to get window titles added correctly for zsh, add the following to .zshrc:
- preexec () { echo -ne "\ek${1%% *}\e\\"}
- precmd () { echo -ne "\ekzsh\e\\" }
- put 'shell zsh' in .screenrc to start with zsh automatically
- startup_message off to disable the startup message
- a status line config that I like: '%{kw}[%{kw}%?%-Lw%?%{=b wR}(%{k}%n*%f %t%?(%u)%?%{=b wR})%{= wk}%?%+Lw%?%?%= %{= }]%{g}[%l]'
I found some of these example configs helpful, especially for deciphering the caption/hardstatus lines:
Saturday, December 20, 2008
make python understand ~ for home directories
Python does not automatically expand ~ for user home directories. There is a function in os.path that does that for you:
mypath = os.path.expanduser('~/somepath')
mypath == '/home/nick/somepath'
any and all
Python has two builtin functions for evaluating iterables for truth/falseness: any() and all(). any() returns true if any member of the iterable evaluates to True, while all returns True only if every single member of the iterable evaluates to True.
>>> any([True, False, False])
True
>>> any([False, 0])
False
>>> all([True, 0])
False
>>> all([1])
True
>>> all([1, 1, True])
True
True
>>> any([False, 0])
False
>>> all([True, 0])
False
>>> all([1])
True
>>> all([1, 1, True])
True
There are identically named subquery operators in sql, however they are only really useful for correlated subqueries which are poorly optimized and generally to be avoided in the current versions of mysql.
Wednesday, December 17, 2008
Commands that can see themselves
You can see the contents of the current command line in /proc/self/cmdline . Of course this is only supported by systems that support /proc, i.e. not a BSD-based system like OS X.
cat /proc/self/cmdline --> cat /proc/self/cmdline
I'm not sure what this is useful for.
The different permutations of grep
bzegrep (1) - search possibly bzip2 compressed files for a regular expression
bzfgrep (1) - search possibly bzip2 compressed files for a regular expression
bzgrep (1) - search possibly bzip2 compressed files for a regular expression
egrep (1) - print lines matching a pattern
fgrep (1) - print lines matching a pattern
grep (1) - print lines matching a pattern
pgrep (1) - look up or signal processes based on name and other attributes
rgrep (1) - print lines matching a pattern
zegrep (1) - search possibly compressed files for a regular expression
zfgrep (1) - search possibly compressed files for a regular expression
zgrep (1) - search possibly compressed files for a regular expression
bzfgrep (1) - search possibly bzip2 compressed files for a regular expression
bzgrep (1) - search possibly bzip2 compressed files for a regular expression
egrep (1) - print lines matching a pattern
fgrep (1) - print lines matching a pattern
grep (1) - print lines matching a pattern
pgrep (1) - look up or signal processes based on name and other attributes
rgrep (1) - print lines matching a pattern
zegrep (1) - search possibly compressed files for a regular expression
zfgrep (1) - search possibly compressed files for a regular expression
zgrep (1) - search possibly compressed files for a regular expression
According to passing the string 'grep' to the always useful 'apropos' command
commandline fun tr and seq
Uppercase on the command line:
tr '[:lower:]' '[:upper:]'
Strip all newlines:
tr -d '\n'
Strip all non-printing characters:
tr -dc '[:print:]'
print from 1 to 100:
seq 100
print from 2 to 50, incrementing by 3:
seq 2 3 50
print from 1 to 10, all on the same line:
seq 10 | xargs echo
Friday, December 12, 2008
Parse any date easily
Use dateutil.parser.parse, possibly with the fuzzy flag set to True.
>>> from dateutil.parser import parse
>>> parse("Jan. 1st, 1991", fuzzy=True)
datetime.datetime(1991, 1, 1, 0, 0)
This is a huge boon over the tedious and underfeatured time.strptime and similar. Unhelpfully, the documentation is non-existent.
How to swap two tables without downtime
You can swap two tables in the same schema without messing up any readers by combining rename table operations into a single statement.
rename my_table to my_table_old, my_table_new to my_table;
I haven't checked this but the statement should use a name lock so that attempts to access either table by other clients will be blocked until the statements complete.
Thursday, December 11, 2008
See how python compiles some code
use the dis module to get the bytecode of any python callable.
Example:
import dis
dis.dis(lambda: x is True)
1 0 LOAD_GLOBAL 0 (var)
3 LOAD_GLOBAL 1 (True)
6 COMPARE_OP 8 (is)
9 RETURN_VALUE
Example:
import dis
dis.dis(lambda: x is True)
1 0 LOAD_GLOBAL 0 (var)
3 LOAD_GLOBAL 1 (True)
6 COMPARE_OP 8 (is)
9 RETURN_VALUE
This is another way to get the efficiency of different parts of code
Sunday, December 7, 2008
See how python encodes a string internally
Fun fact: you can see how python encodes a string internally by asking it to encode a unicode object to the 'unicode_internal' codec.
Example:
>>> u'\N{SNOWMAN}'.encode('utf16')
'\xfe\xff&\x03'
Example:
>>> u'\N{SNOWMAN}'.encode('utf16')
'\xfe\xff&\x03'
>>> u'\N{SNOWMAN}'.encode('unicode_internal')
'&\x03'
As you can see, it's UTF16 without a BOM.
Fun stuff with unicode in python
Fun fact: you can interpolate a unicode character into a unicode string literal by using \N{name of character}
Example:
>>> print u'this sign is \N{VULGAR FRACTION ONE HALF} off'.encode('utf8')
this sign is ½ off
Example:
>>> print u'this sign is \N{VULGAR FRACTION ONE HALF} off'.encode('utf8')
this sign is ½ off
You can tell the name of any given character by using the unicodedata module
>>> import unicodedata
>>> unicodedata.name(u'\xbd')
'VULGAR FRACTION ONE HALF'
The name of the famous snowman character is simply enough 'SNOWMAN.
>>> unicodedata.name(unichr(9731))
'SNOWMAN'
So you can represent this character in python simply by u'\N{SNOWMAN}'
>>> import unicodedata
>>> unicodedata.name(u'\xbd')
'VULGAR FRACTION ONE HALF'
The name of the famous snowman character is simply enough 'SNOWMAN.
>>> unicodedata.name(unichr(9731))
'SNOWMAN'
So you can represent this character in python simply by u'\N{SNOWMAN}'
Thursday, December 4, 2008
Use vim's multiple paste buffers
Ever find yourself avoiding cutting something in vim because you don't want to lose the contents of your paste buffer?
Fear not! Vim has been silently saving away the last 16 things you've cut into multiple paste buffers, which you could have been accessing all this time had you only heard the first thing of it.
Type :reg to see the contents of your paste buffers. To paste something that you cut 3 cuts ago, just use "3p
Wednesday, December 3, 2008
Hash images
Now, this is what I call cool: findimagedupes.
It's a image hashing program, that outputs a "perceptual hash" of image files. Unlike a cryptological hash, a small change in an image should result in a small change, so you can use this to tell how similar two slightly different images are.
You can also use it to recursively search directories for duplicate images.
It's in apt for debian and ubuntu.
Tuesday, December 2, 2008
Keep a series of python objects in a shelf
You can store a dict of pickled objects in a "shelf", a dbm-based hash table stored to disk, using the shelve module.
Monday, December 1, 2008
Tell whether two clients are using the same connection
select connection_id() will tell you the unique id of your connection. Try it on two different client objects to see if they are using the same connection or not.
Subscribe to:
Posts (Atom)