Posts filed under 'Tip'

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

6 comments April 8, 2008

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]

3 comments April 4, 2008

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.

Add comment April 4, 2008

Downloading Files from Command Line via FTP

At the command line, enter ftp <serverName>.

If you have a user name and password, enter them. Otherwise, use the default of Anonymous and a blank password.

Use cd, ls, etc. to work your way around the directory structure.

If you want to download a particular file, specify get <fileName>.  If you want to download many files, use mget <filePattern> (for example, mget *.gz). By default it will ask you to confirm every file download. You can disable this by entering prompt and then hitting Enter before doing the mget.

Enter bye to exit.

(I think this works the same on Windows as it does on *nix operating systems.)

Add comment March 31, 2008


Categories

Archives

Top Posts