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 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:

With lambda:

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

When not to use lambda:

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.

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:

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

Setting phpMyAdmin to point to an Amazon RDS instance

Isn’t it annoying how you can’t ssh in to Amazon RDS instances?  Don’t you wish there was a way you could use trusty old phpMyAdmin to manage your db?  Here is the easy way to do it:

  1. Find the endpoint link to your RDS server.  It’s found in the instance information section.  It usually looks something like this: xxx.amazonaws.com
  2. Install phpMyAdmin somewhere.  I recommend on a webserver so you can access anywhere.
  3. cd phpMyAdmin
  4. sudo cp -p config.sample.inc.php config.inc.php
  5. Change this line in config.inc.php:

to

 

That’s it!

CloudFormation templates: Old vs. New

I’ve been using Amazon Web Services (AWS) for a while now, and have recently started using their new product called CloudFormation.  CloudFormation is a must-have solution if you find yourself setting up the same web stack frequently.  First you setup a template for your stack, then you let CloudFormation setup your stack for you.  I’ve now used it several times for WordPress + RDS stacks, and it has saved me a tremendous amount of time!

That said, I don’t like the new example template for WordPress + RDS stack.  I find overly-complicated, because it askes for many parameters which are unnecesary and not straight forward.  If you are having trouble with the new template, I highly recommend trying the old one here.

Tip:  The example WordPress+RDS templates disallow micro RDS instances.  This sucks because it bumps you out of the free usage tier.  If you want to stay in the free, just add db.t1.micro to the allowed values of DBclass in the template.  I already did this for my template I posted above.  For small websites, the db.t1.micro works just fine!

Namescheap and DNS settings

I started using Namescheap lately as an alternative to GoDaddy.  Namescheap is awesome for how clean and straight forward it is.  I do pose a warning though:  their documentation for the DNS settings is somewhat misleading and confusing, and their default settings are probably not what you want.

The Options:
URL Redirect (default) – URL Frame Forwarding – A-address –  URL Redirect – 301

My advice:  If you want this domain to have the browser redirect to another domain, use URL Redirect.  You will notice in the address bar that the original domain you entered will change to the redirected domain.  If you are using the domain name as your primary, then use the A – address option.

Hello world!

Welcome to my blog:  powered by WordPress and based on Matthew Buchanan‘s theme “Esquire”.

This blog serves as my memoir of interesting tech issues I encounter.  By documenting my experiences, hopefully I’ll be smarter next time.