How and when to use python’s lambda

There is a long running debate in python land of the usefulness of the lambda function, and I usually air on the side of avoidance.  However, for simple occasions it can make for cleaner code.

The python docs are essential for quick reference, but sometimes they skimp on obscure features.  Lambda is one of those features.

In a nutshell, lambda is the inline function creator for python. In other languages, inline functions often look like this:

myFunction = function(arg1,arg2) { return arg1+arg2 }

myFunction is a function, and myFunction(1,2)=3.  That makes a lot of sense! myFunction can similarly be defined in python using lambda:

myFunction = lambda arg1,arg2:  arg1+arg2

It’s true that this syntax requires less typing, but at the cost of readability.  In case you were wondering, here is a good example of when to use it.

Without lambda:

def isLessThanFour(x):
    return x<4
list_full = [1,2,3,4,5]
list_subset = filter(isLessThanFour, list_full)

With lambda:

list_full = [1,2,3,4,5]
list_subset = filter(lambda x: x<4, list_full)

Head caution though, because readability goes out the window whenever the lambda expression gets to complicated.

When not to use lambda:

reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

Automating Browsers with Selenium

Automating browser actions from an external script is easier than you think.

The answer:  Selenium.

Selenium is a browser automation tool suite which is compatible with several popular browsers, such as Firefox, Chrome, Internet Explorer and Opera. It is primarily written in Java, but it also has extensions for Python and Ruby.

Building on my Python and Javascript experience,  my experience was mostly painless for Firefox and Internet Explorer.  Getting the extensions is fairly easy, and source code was straight forward.  I really like how I can grab html objects by id OR class or xml.  Here is a very simple example of using the Selenium python extension to 1) open Firefox, 2) navigate to google.com, 3) enter text into the search box, then 4) hit enter.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('http://google.com')
assert 'Google' in browser.title
searchBoxes = browser.find_elements_by_id('gbqfq')
searchBoxes[0].send_keys('hello' + Keys.RETURN)

Migrating WordPress the easy way

UPDATE
I now advise using interconnectit’s php script method instead of the method below, because it’s more reliable.

If you search on Google, there are tons of tutorials on migrating WP.  I found all of them hard to follow and overly complicated.  Follow these simple steps to easily move a WordPress (WP) website:

1) Copy the WP root folder to the new location

2) If also migrating the db, use phpMyAdmin to export the WP tables from the old db and import them into the new

3) Point a browser to the new WP location.  It should work, but all of the db is still pointing to the old WP location so we have to fix that

4) Run these sql commands in phpMyAdmin:

update wp_posts set post_content = replace ( post_content, "<old website location>", "<new website location>");

update wp_posts set guid = replace ( guid, "<old website location>", "<new website location>");

update wp_options set option_value = replace ( option_value, "<old website location>", "<new website location>");

5) Last, make sure you setup mod_rewrite on your new web server if you are using mod_rewrite on your old one.