Tuesday, September 29, 2009

python: How to add a directory to the module search path

It's easy to add a directory to the module search path. (Note: I like running stuff and testing new things in the interpreter. This tidbit enables me to test out modules I'm writing without having to modify the PYTHONPATH environment variable. The python interpreter rocks.)

import sys
sys.path.append( 'newpath' )

Wednesday, September 23, 2009

python optparse mini tutorial / how to

Here's a little tutorial on how to use the optparse module in the Python standard library.

  1. So far I've only been using optparse.OptionParser so I do the

    from optparse import OptionParser

  2. Define a usage string. I've been using:

    usage = "Usage: %prog [options]"

  3. Define a parser: parser = OptionParser( usage = usage )

  4. Set the defaults: parser.set_defaults( somevar = defaultvalue )

  5. Add options:

    1. Example of command line options that have arguments:

      help="help info for --someopt" )

    2. Example of command line option that acts as a switch/toggle:

      help="help for turniton" )

    3. Another useful argument is metavar - helps you to name the variable being addressed by the option. The default is ok.

  6. Get the options and arguments:

    (options, args) = parser.parse_args()

  7. At this point it all depends on what you want to do. Let's say we want to access the options defined above:

    • To access someoptname:

      someoptname = options.someoptname

    • To access turnitonflag and store in a differently named variable:

      myturnitonflag = options.turnitonflag

    • You get the picture.

  8. You can use parser.error( "some message to indicate what went wrong" ) to convey to user that the options didn't work.

Monday, September 21, 2009

pythonic perl 4: Finding an element in a list

In Python, you can do this:

a = "findme"
mylist = [ "no", "notthis",
"findme", "toomany",
"not here" ]

if a in mylist:
print "Found it!"
print "D'oh! Didn't find it."

True to its roots, there is more than one way to do this in Perl. The more common ways would be to use an array (the semantic equivalent of a Python list) and grep.

But the most Pythonic way I found would be not to use an array but to use a hash. Note that the mapped values are arbitrary and are not really used. In fact, you probably don't need to define any values since we are using exists and not the defined function.

$a = "findme";
%myhash = (
"no" => 0,
"notthis" => 1,
"gettingwarmer" => 2,
"findme" => 3,
"toomany" => 4,
"not here" => 6,

if (exists $myhash{$a)) {
print "Found it!\n";
} else {
print "D'oh! Didn't find it.";

More Perl articles:

Sunday, September 20, 2009

perl 101: use FindBin

FindBin does one simple thing. It allows you to determine the full path of the Perl script you're running. This allows you to build relative paths to other parts of the filesystem that you might need.

More Perl articles:

Thursday, September 17, 2009

how to do pythonic perl 3: To carp and croak is better

Use carp and croak instead of warn and die.
The primary reason is that the carp module produces more useful information, especially when used inside modules. That is, the user is more interested in where the offending code was called from instead of where your module crapped out.

Find the Carp module here on CPAN.

Thanks to chorny for pointing out useful resources that led me to the Carp module.
It was the Perl::Critic module that led me to this particular bit and I will be doing more tips from the Perl Best Practices book by Daman Conway

Wednesday, September 16, 2009

how to do pythonic perl 2: Explicit > Implicit

As a followup to my earlier post, this is an even better way to do it, IMNSHO.

Don't use $ARG or $_ at all. Assign to a variable that explicitly names the variable you expect.

Because the Pythonic way is: Explicit is better than implicit.

Let me just say now that if you don't like the Pythonic way, that's cool. I'm not here to beat you over the head with it. So please give me the same respect I give you and just move along if you don't find this blog to your liking. If mocking other people is more to your liking, go do it somewhere else.

Tuesday, September 15, 2009

how to do pythonic perl: $ARG is not argh

Well, it had to happen sooner or later. I'm forced to maintain someone else's Perl program. I really don't like Perl. (And as I mentioned before, don't take it personally Perl-mongers.)

So here's my series of articles on how to make Perl more Pythonic. (I know this is heresy and blasphemy in some circles, but hey, it's a big world out there.)

One of my primary gripes is the use of $ variables. One in particular. $_ which is the default input and search space variable.

In fact, a lot of perl programmers will leave it out since it is implied by default. Which makes for totally unreadable code unless you know the secret handshake and secret password.

So, my first step to wading in the Perl pool is to use the alternative form of this variable:


So whenever you feel like using $_ or using $_ implicitly, please. Please use $ARG.


Stay tuned for more "how to do pythonic perl" articles.

Update: Here's the next parts to the Pythonic Perl series:

vim: enable line numbers in printout

If you want to print line numbers like I sometimes do, use printoptions to control this.
Do the following to enable line numbers in the printout:
:set printoptions=number:y

If you wanted to enable line numbers in vim (on the screen and not on the printout), see this post: vim 101: how to enable line numbers in vim

Sunday, September 13, 2009

vim: how to split a line

In vim/gvim, you can join two lines using the J command. So I was wondering if there was a simple command to split a line.

After much searching, I found that there really isn't one, but the replace command r, can do so by placing the cursor on the whitespace you want to split and doing the following:


where Enter means press the enter key.

This works pretty well for splitting a line without having to enter insert mode.
It's a little tip, but it's the little things in life that make life a little brighter. :) Obviously it has to have whitespace at the point of the split to make this work. Otherwise, going to insert mode is probably faster.

Friday, September 11, 2009

python dictionary little hack

The situation is this: Let's say I have a dictionary that maps to dictionaries.
I want to get a list of all the keys in the secondary dictionaries, take out duplicates and return as a list.

So let's say we have something like this:
d1 = { 1 : "aaa", 2 : "bbb" }
d2 = { 22 : "yyy", 33 : 'zzzzz' }
d3 = { 141 : 'xyz', 123 : 'abc', 231 : 'lmnop' }
myd2d = {
'a' : d1,
'b' : d3,
'c' : d2 }
The desired answer is
Not necessarily in that order. The straightforward way to do it would be:
result = []
for k,v in myd2d.iteritems():
for mykey in v.keys():
result.append( mykey )
I suppose the other way might be more pythonic:
r = sum( (v.keys() for k,v in myd2d.iteritems()), [] )

Thursday, September 10, 2009

how to un-highlight vim searches

The problem is basically this. If you have hlsearch set, then after you've searched for an expression, it remains highlighted. (I believe this is the default behavior in gvim. In vim on the console, I've seen the opposite.)

I got into the habit of turning off the highlight by searching for a bogus expression like


Turns out you can do this:


to turn of the highlighting.

If you do:

:set nohlsearch

then it turns off highlighting of searches compeltely. :)

Where to find Google Apps Standard Edition

So I was looking for the standard (read: free ;) edition of Google Apps. I did it already once and it was pretty easy. I had a hard time looking for the standard edition since the landing page only seemed to advertise the premium (read: give Google your money ;) edition.

After a little bit of googling (ironic isn't it?), I found it here.

I did notice one change. The number of users allowed went from 200 to 50. :-/ Oh well. :) It's still pretty awesome.

Up next: how to set up Google Apps Standard Edition

Wednesday, September 9, 2009

how to iterate through a hash in Perl

Yes, I'm a Perl newb. And I really don't like Perl much. But I still have to read through Perl code and sometimes modify it. So here's how a bit of Perl 101 for myself.
Here's how to iterate through a hash in Perl:

foreach my $key (keys %myhash)
print "key: $key value: $myhash{$key}\n";

I like this construct because it's pretty clear what's happening.

Ad: Learning Perl, 5th Edition

how to flatten a list of lists in Python

Say we have:

mylist = [ [ 1, 2, 3, 4 ],
[ 'bb', 'uuu', 'bbbb' ] ]

There are different ways of flattening mylist:

  1. The first is the obvious way:

    result = []
    for x in mylist:
    for y in mylist:
    result = result.append( y )

  2. The next one is my preferred way using sum and a list comprehension:

    result = sum( mylist, [] )

  3. The third way is an obfuscation of the previous one

    from itertools import chain
    result_iterator = reduce( chain, mylist )

    Or if you want a list instead of an iterator:

    result = [ x for x in reduce( chain, mylist ) ]

If anyone can think of anything else, I would like to hear about it.

Wednesday, September 2, 2009

vim negative match using negative look-ahead assertion

The second part to the mildly popular vim regex negative match post. This time we'll use the zero-width negative look-ahead assertion feature in vim.

I'll show 3 different ways this can be used.

  1. The first one is in my opinion, the most useful. Suppose you want to find all instances of foo which are NOT followed by bar. For example, we want to match foobear but not foobarstool. The vim search command for this is:


    So the first part is easy. We want to match foo, so that starts the regex. Next we want to indicate that we don't want bar following foo, so we put bar and the negative look-ahead assertion multi indicator

    The negative-look ahead assertion multi applies only to an atom though. So if we didn't put the parentheses around bar, the assertion would only apply to the letter r. So that's why we need the parentheses.

  2. The second way to do use it is simply apply the negative look-ahead assertion to an atom without a match in front of it like so:


    Try it and you'll see that it is pretty useless as it will match most everything. (Exercise: how can you get this expression to not match a certain spot? :)

  3. The third way to use this can mimic the


    command described in an earlier post. So do this:


    This kind of mimics the global command. I'm not entirely certain if the behavior is the same on blank lines (ie. lines withonly the newline/CR character on it). This one is mostly academic though as I would use the global v command anyway. :)