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.

Saturday, October 29, 2011

Fishing Tips

In was a bit frustrated when I went fishing over the summer and had no luck at all fishing.  As in not a single bite.  So I did a ton of research and tried to figure out what I did wrong.  Below is most of what I found.

Gear
10 Things every tackle box needs and Let's outfit a tacklebox
Weights, lures, hooks, pliers, etc.

Spinning reels vs baitcast reels
Baitcast gives a little more control and is necessary for very heavy-duty applications, spin cast works better for very light lures.
Tip for clearing backlash















How often to replace line
1-2 times per year depending on use.

Hook buyer guide

Types of fish
Bass
Tips for catching bass

Fishing line for bass
Choosing the right lures
Topwater tips

Crappie
Location, presentation, behavior
Spring crappie tips
John on crappie fishing

Bream
Soft plastics for bream
Poppers for bream

Catfish
How to make catfish soap
Soap bait demo
How to make a catfish jug

Carp
Prepping carp for cooking
Doughball recipies
NPR doughball tips

Weakfish
How to catch weakfish in the bay

Bluefish
How to catch bluefish
Guide to bluefish

Stripers
Live bait rigs
Bunker head rig

Rigs
How to tie double fishing line
How to tie a surgeon's knot
Sabiki rig for surf casting
Live bait rigs

Types of lures
Lurenet
A website that sells lures, but also has good explanations about what each design is, what it is useful for, etc.
Quick overview of lure types
Top water lures
Soft plastics with KVD
Topwater lures for saltwater

Jerk bait
How to fish using a jerk bait

Spinner bait
How to work a spinner bait

Spinnerbait with KVD
Spinner bait tactics

Spoons
Spoons are good for finding fish and fishing quickly.  Best in deep water.
Jigging spoons on YouTube
Basic tips
Jigging spoons in salt water
Jigging spoons tips

Jig and Pig
5 tips

Finesse jig tips
Bass edge jig tips
Basics
Jigs with KVD
Jigging for bluefish and stripers with bucktails
Bass professor jig and pork
Bass professor bucktail jig
Jig and pig made simple

Buzzbait
Tips
Bass professor buzz baits

Plastic worms
Tips for bass
Tips for plastic grubs or worms
4 minute tip video
Carolina vs Texas rigs
Carolina rig made simple
Texas, Caolina, and floating rigs

Swim bait
Basic promotional video tips

Plastic frogs
Tips

Rattle
Rattletrap tips

Casting Technique
Skip cast
Easy cast for surf casting
Texas long distance surf casting
Off the beach surf cast

Retrieval technique
Basics to lure retreives
Walk the dog

Platforms
Kayak
3 golden rules
Georgia Kayak Fishing

Bay
Bay fishing tips

Met Atlanta
What fish live in streams in metropolitain Atlanta

Environment
Bass professor - environment

Bass profesor - Big bass feed midday
Bass professor - night fishing
Bass professor - Shallow water for big bass
Bass professor - fishing a new lake
Coldwater lures

Color selection
Bass professor tips

This will be updated and cleaned up later.