Archive for April, 2008

Converting a String to a Boolean in Python

Let’s say you have a string value that you want to convert to a boolean, but you’re not sure the format it will be in. Some languages have built-in functions for doing this, but to my knowledge Python doesn’t. Here’s a way to do it (though it’s not comprehensive). (Thanks to the commenter who helped me see a simpler way to do this.)

def parseBoolString(theString):
  return theString[0].upper()==’T’

parseBoolString(“true”)

True

parseBoolString(“false”)

False

April 8, 2008 at 1:11 am 20 comments

Simple Method to Search a Python List

Let’s say you have a list of objects of type Individual and that list is called individuals.

The Individual type contains an ID, name, and email address.

Let’s say you have an ID and want to get the corresponding Individual object from the list. How would you go about doing that?

match = [ind for ind in individuals if ind.id == theID]

April 4, 2008 at 3:19 pm 3 comments

Increasing Swap Space in Linux

I had trouble with an application that was using gigabytes of memory in Linux and running out, so I had to figure out how to increase the amount of memory without buying more hardware. A relatively easy solution for this is to create a new swap file. Of course, there are drawbacks to doing this, but if you are in need a quick solution to increase the memory available to an application, this might work for you, too.

This article explains how to do this on Red Hat. I am not sure, but I think it’s similar on other flavors of Linux.

April 4, 2008 at 12:41 am Leave a comment