Sunday, January 19, 2014

Steps I took for moving my Wordpress site to be hosted on Earthlink

My neighborhood's home owner's association is using Earthlink hosting.  Most of the cheap shared hosting services have some eccentricities that you may run into when trying to do anything outside the standard "1 click installer" type of actions.  Earthlink seems to be especially behind the times.
Some clues:
  • During my fiddling with user database accounts, I managed to get the web panel into a state where I was locked from doing anything to my phpmyadmin installation.
However,  both times I've used their chat support tool I've received fast and helpful replies, so the barrier for switching hosting services stayed just high enough to keep me with Earthlink (for now).

Getting to the point: here's what I did to get the latest version of Wordpress (with updated plugins and a database full of pages) migrated from a server I was running locally.

1. Use the Earthlink wordpress installer

This creates the wp-config.php and puts it into /private.  It also creates the database, which is not a big deal but it's nice.  It also probably fixes up htaccess and some other settings, which you can't edit.

You can't stop here though because you can't update your Wordpress installation, and running with an old version of Wordpress is bad for security and breaks many of the plugins that make Wordpress awesome.

2. Clean up your database dump file

You used mysqldump to get the data from your old database, right?  That's good, but you need to change the urls in the database dump file.  I was able to do this quickly using find/replace on the dump sql file.

Also, because the commands in the dump files include "lock" and "unlock" statements and Earthlink doesn't give you those permissions on your database users for some reason, you need to remove all of these commands.  I ended up just running the commands for each table one at a time, removing the "lock" and "unlock" statements for each table before executing.

3. Install phpmyadmin

For some reason I couldn't run any sql commands from the command line.  So In installed phpmyadmin, which worked fine.

One thing to look out for is that you have to associate the phpmyadmin installation with a single database when you add it.  If you delete the user associated with the database through Earthlink's console, the database is also deleted, and this breaks phpmyadmin to a point where you can't re-install, uninstall, or re-associate the phpmyadmin installation with a different database.  At that point, you have to talk to support.

4. Delete all the crap the Wordpress install placed in the /public folder

Yes, you just added it, but this is where your new Wordpress files will go.  Make sure to not delete the /phpmyadmin symlink in that directory.  You need that.

5. Copy all of files for your local Wordpress install into Earthlink's /public folder

Here "local Wordpress install" means all the files from the installation you already had running somewhere else.  FTP works fine here.


6. Copy the wp-config.php files from /private to /public

This file is pretty important.  You could fiddle around with the wp-load file to get it to fine the wp-config file in /private, but I found it simpler just to move the file.


That should be it!  It felt a little unclean, but it got the job done and I'm now running a modern copy of Wordpress that I can easily update because all of the files are in a folder I have FTP access to.

Wednesday, August 7, 2013

Variable scoping in python and javascript

As I was reviewing a colleague's code a few days ago, I ran across the following:

Having spent the last few weeks reading through Code Complete, defining the variables outside the loop stuck out to me as a problem - a case of pre-mature optimization.  I suggested the following:

Reducing the scope of the variable just felt cleaner, and the discussions I found here and here seemed to back this up.

But when this came up when talking with a friend later in the day, he mentioned that he didn't think javascript even maintained scope inside "for" loops.  Coming from C++, I thought loops in both javascript and python would hold scope.  The following were our test functions, entered on the Chrome Developer Tools command line for javascript and the REPL for Python.

The output from both these tests are the same: x is defined.  This is because both python and javascript lack block scope, something very much present in C++.

In the particular case I was discussing, the choice becomes a big more grey.  There are strong opinions on both sides of the fence.  At this point I agree with the accepted answer on the linked StackOverflow discussion: "For the case where a variable is used temporarily in a section of code, it's better to declare var in that section, so the section stands alone and can be copy-pasted."  I think the use "var" can be as helpful for people as compilers, and it makes the intent of a block of code more clear.  Using "var" on a variable in a code block says to me that this variable is intended for use in this code block, and may not have any meaning outside of it.

Thursday, July 25, 2013

A Nose Specific Django Settings File

The Python Nose test framework has a several advantages over Django's default test framework.  I personally wanted to use Nose because of the xunit plugin which can output results in xunit format.  I needed this output format so that test results could be interpreted by Bamboo, the continuous integration solution we're using at my work.

For using Nose with Django, the django-nose package is an obvious choice.  However, it requires setting several settings values in your settings.py file that were only used by Nose.  Also, I like being able to run my tests using Django's default test runner.  I'm familiar with the output format, with the syntax for specifying how to run a single test, etc. and I didn't want to limit myself to running only Nose tests.

The approach I took was to create a new settings file for running nose tests.  This would contain just the settings needed for configuring django-nose, and import the main settings file to fill in the rest.  This is kind of the opposite of the usual pattern of importing a "local_settings.py" file at the bottom of your main "settings.py" to specify scenario specific settings (e.g. development vs production).

The comments in the script specify how to use it.  I hope it's helpful.

Wednesday, July 10, 2013

Duck Typing in Python

It has happened before, and it was happening again.  I had a weird problem, coded up an "interesting" solution, and then tried to find out why I felt strange about it.

In this case, I was writing a test function in a Django project to loop over a set of Tastypie api endpoints and make sure each endpoint responded without any errors.  To allow this test to grow with the file describing the api endpoints and to follow basic DRY principles I decided to import that file, find all the classes of a given type find the attribute I needed to build up the url of the corresponding api endpoint, and use the test client to render that url and check for errors.

My first attempt looked like this:


It may work, but it's not pretty. I'm explicitly checking whether each object I'm looking for is of the correct class, then I'm checking for an attribute, and after that I'm still having to wrap everything in an Exception to handle the case where the first argument to "issubclass" is not, in fact, a class.

So, with the smell of bad code wafting through the air, I went off in search of better solutions.  In this search I started in the place where all good programmer start: StackOverflow.

I started with this question about checking if a class is a subclass of another class.  At first I dismissed the comment about "python is not java" as a preachy comments on dynamic languages, but that got me thinking: I've heard the same people advocating duck typing and at the same time saying not to use exceptions to control flow.  I hadn't really put these 2 together solidly before, but now that I have this problem to work on, I see that these are quite related, and you can't have it both ways.  Either you type check, or you handle the unexpected with exceptions.

Coming from a HPC world, I instinctively avoided exceptions because they are usually though of as slow.  But after reading this very well phrased question on the topic of asking permission vs asking forgiveness (i.e. type checking vs. exception handling), my mind was starting to change.  In python the performance penalty is not so large that Exceptions need only be used for "exeptional" circumstances. After all, iterators are controlled with the StopIteration Exception, and that's part of the core language.

My mind was made after reading a third great question and response explicitly addressing duck typing.  The top answer recommends using Exceptions whenever possible as long as nothing really strange is going to happen if you try an operation on the wrong type of object.  Most of the time it's better to just document your functions well, let your users decide if they want to use it, and raise and Exception up to them if something went wrong.

So after all this, I went back and tried to put back together my simple function using what I learned.  I think the second take is more readable and probably just as fast as the original (the running of the test client for each url is definitely the bottleneck here anyway).  The substitution of the getmembers function of the Inspect module helps too.

If you were running into a similar problem with smelly code or preachy internet pythonistas, I hope this helps clear things up.

P.S. - The code snippets were embedded by adding the following snippet directly in the body of the post using the HTML editor feature of Blogger:

<script src="https://gist.github.com/[username]/[gistID]/[gistVersion].js"></script>

Tuesday, January 3, 2012

Photography: Layers in GIMP

The goal here was simple: to highlight a certain portion of a photograph with color while the rest stays black and white.  For this, I chose a picture of a butterfly.  In retrospect this is not the best choice because the butterfly is too dark to stand out well against a black and white background, but oh well.  It still worked.
The original photo.

To start, open the image you want to edit in GIMP using File -> Open.  Now go to Windows -> Dockable Dialogues -> Layers, or simply use the Ctrl-L command.  A window will pop up that shows your current image with the default name, probably "Background".  You can click in the eye icon to show or hide the layer.

Now you want to create a duplicate copy of your layer in black and white.  Left click on your layer to select, then right click and select the "Duplicate Layer" option.  A new layer should appear in the "Layers" window.  Click on the eye icon next to your original layer ("background") to hide it so you know you are looking at your new layer.

Next, use Picassa to make your new layer black and white.  An easy way to do this is to click to select your new layer in the "Layers" window, then select Colors -> Desaturate.  The default settings work, but feel free to play around with the settings to see what works.  Most changes can be undone with a simple "Ctrl-Z" if you make a mistake.

Now you have 2 layers: a color and a black and white copy of the same image.  Now you need to use something called a layer mask to hide the portion of the black and white image that you want to be in color.  So right click on the B&W layer in the Layers window and select "Add Layer Mask".  Keep the default setting for "white (full opacity)".

Now go to the Toolbox window (it should have been up the whole time, but if not, go to Windows -> Toolbox to see it) and select the paintbrush tool.  Use the menu in the toolbox to select a brush size and type (you probably want to start out with the big circle) and start painting your layer starting at the center of the area that you want to be in color.  If you have hidden the original image correctly (i.e. the eye icon is only shown next to the B&W layer), you should see a checkered pattern showing through your image as you paint.  Keep painting until all of the portion of the image that you want in color is gone.  Make thc brush size smaller and zoom in to get around the edges.

Now you're almost done.  Drag the B&W layer so it's above the original in the dialog box.  Make sure the "Mode" of each layer (visible in the Layers window) is "Normal".  Click the eye icon next to both to make them visible.  Viola!  Your color should be peeking through your B&W, giving the effect of color highlighting.
The new image using 3 layers.  The B&W is in "normal" mode and the other two are color layers with different qualities in "Overlay" mode to give a mixing feel.

Now, go to File -> Save As to save your image.  You can select whatever file type you want.  Say ok if GIMP complains about JPEG not understanding layers - the default action by GIMP gives you what you want.  The final product should be something like what is shown above.

In the image shown above, I got a little fancy and make the layers transparent by changing the "Mode" to "Overlay" for all the layers except the B&W, and I played with contrast color settings and mixed that with a layer where I applied an edge finding filter to make the image pop up.  SO obviously there is a lot more you can do, but you'll just have to play around and figure it out. :)

Let me know if this helped!

Photography: Before and After for Basic Editing in GIMP and Pisacca

After talking to my wife, I learned that she really liked the color settings on here friends new Canon DSLR.  We weren't sure if our Canon SD1000 would be able to produce colors like this, but I figured it was worth looking into.

I have messed around with Picassa a few times to do some basic editing, and I have also used some of the functions in GIMP to get a little fancy with my editing. Some example of what I found we could do are shown below.


Set 1:
The first image looks o.k., but a little dull.  By playing around with some settings in GIMP (mainly color saturation and contrast), I was able to create bit more appealing version in the second photo.  In the new photo, the water looks bluer, the plants look greener, and everything just pops a little bit more.










 
















Set 2:
Again, the first image is quite nice, but the colors just look a lot nicer when saturation is boosted.



































Set 3:
The angles looked nice when I took this picture, but the horizon is uneven and the colors just clash a bit too much.  Using the softglow filter in GIMP and messing with hue, I was able to make something a bit more interesting.





























GIMP is certainly capable of much more than I have done here, but it's still neat that with just a few minutes of messing around you can turn decent photos into really nice photos.

Photography: Getting the most out of a point-and-shoot

Similar to the last post about fishing, this article will be an "information dump" with all of the interesting things I have found about photography while researching it in the last two days.

My wife and I were looking at a DSLR, and after thinking about it, I was simply not convinced that this camera was a great choice.  I had read many articles that talked about the wonderful photos that could be taken with point and shoots, so I knew there was untapped potential in our Canon SD1000 7.1MP Digital Elph.  Further, I knew the cost and size of the DSLR would make it annoying to tote around, and we would always be scared of it breaking.
Our current point-and-shoot.
Thus, I went in search of information on how to get the most out of our little point and shoot.

The first set of three articles are general information on why point-and-shoots are great, and techniques for getting the most of of them.
I like to take a lot of photos in macro setting (up close pictures with a wide aperture for a narrow focal plane resulting in a blurry background).  This article explains how to do it better.  The article also serves as a good into to aperture effects.

Night shooting is something we also wanted to try.  In particular, we want to shoot a nice night cityscape of Atlanta.  These articles have some great tips on how to get very good photos at night, including portraits, stills, and scenery shots.

Since we want to blow up our cityscape photo, I did a little research into what resolution camera we need to get a nice blown-up photo.  It turns out that matters very little.
And lastly, as inspiration, an article on cameras and why they don't matter very much.  This article contains many examples of beautiful photos taken with cheap or seemingly "obsolete" cameras.
And although cameras don't matter so much, to get my wife really going on photography here are some I picked out as great options.  It looks like the travel-zoom category of cameras is the place to look, since the DSLRs and even the micro 4/3 class are a bit big to carry around.