How Many Files in a Linux Directory

The following command will tell you how many files matching the *.csv pattern are in the current directory:

ls -1R | grep .*.csv | wc -l


July 4, 2008 at 6:14 am 1 comment

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’





April 8, 2008 at 1:11 am 24 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 == 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

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.)

March 31, 2008 at 10:15 pm Leave a comment

Upgrading MySQL on Red Hat Linux Despite Dependency Conflicts

Recently I needed to upgrade MySQL from version 4.1 to 5.0 on Red Hat. I was having some dependency conflicts that were vexing me. After searching around the Web I figured out how to do it. Because I’m a Linux novice, I’m sure there are more details I don’t understand, so if anyone wants to provide insight, that would be great.

Here are the basic steps I performed:

  1. Downloaded the .rpm files from the MySQL site (server, client, shared libraries, shared compatibility libraries) for my O/S version to a single folder on the server.
  2. Tried the basic install (from the directory with the .rpm files): rpm -ivh MySQL-*
  3. That gave me errors about dependency conflicts with previously installed MySQL packages. I used this command to find out which packages were installed: rpm -qa | grep mysql
  4. I went through one by one trying to uninstall them (rpm -e <package name>). However, I ran into problems because there were some duplicates. For the duplicates I ran the following command: rpm -e –nodeps –allmatches <package name>.
  5. Then I reran the install command from step 2, and it worked!

This page has some helpful tips on starting up the MySQL service and setting the root password, etc.

March 31, 2008 at 9:12 pm Leave a comment

Sorting Dictionaries in Python

Newer versions of Python have a built-in function called sorted that can help you sort dictionaries. Below is the basic functionality.

Sort by key:


Sort by value:

class SortedDictionary:
  def __init__(self, dictToSort):
    self.keys = sorted(dictToSort.iterkeys())
    self.values = [dictToSort[key] for key in self.keys]
    self._lastIndex = -1

  def __iter__(self):
    return self

  def next(self):
    if self._lastIndex < (len(self.keys) - 1):
      self._lastIndex += 1
      return (self.keys[self._lastIndex], self.values[self._lastIndex])
      raise StopIteration

x = {}
x['abc'] = 1
x['aaa'] = 2

y = SortedDictionary(x)
print y.keys
print y.values

for z in y:
  print z

March 28, 2008 at 10:36 pm Leave a comment

Older Posts Newer Posts