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

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.

Add comment March 31, 2008

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:

sorted(x.items())

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])
    else:
      raise StopIteration

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

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

for z in y:
  print z

Add comment March 28, 2008

Copying from One Directory to Another in Linux

cp FileName.txt DirectoryName/

1 comment March 27, 2008

How to Install tar.gz Files in Linux

cd Download_Dir/
tar -xvfz file.tar.gz
cd file_dir/
./configure
make
make install

Add comment March 25, 2008

Append a List to a List in Python

(Note: Please see my latest posts at my new blog!)

An easy way to do this is with the extend function:

x = [1,2,3]

x.extend([4,5])

[1,2,3,4,5]

1 comment March 19, 2008

Simple Method to Calculate Median in Python

(Note: Please see my latest posts at my new blog!)

def getMedian(numericValues):
  theValues = sorted(numericValues)

  if len(theValues) % 2 == 1:
    return theValues[(len(theValues)+1)/2-1]
  else:
    lower = theValues[len(theValues)/2-1]
    upper = theValues[len(theValues)/2]

    return (float(lower + upper)) / 2  

def validate(valueShouldBe, valueIs):
  print “Value Should Be: %.6f, Value Is: %.6f, Correct: %s” % (valueShouldBe, valueIs, valueShouldBe==valueIs)  

validate(2.5, getMedian([0,1,2,3,4,5]))
validate(2, getMedian([0,1,2,3,4]))
validate(2, getMedian([3,1,2]))
validate(3, getMedian([3,2,3]))
validate(1.234, getMedian([1.234, 3.678, -2.467]))
validate(1.345, getMedian([1.234, 3.678, 1.456, -2.467]))

6 comments March 17, 2008

Filtering Data in Python (Example of Functional Programming Approach)

(Note: Please see my latest posts at my new blog!)

Let’s say you are doing a cancer study and have a list of patients of various ages in a tab-delimited file. You want to limit the study to patients who are 60 years or older. One way you could do this is use a for loop and process the data one row at a time and remove any patients below your threshold. Another way is to insert the data into a SQL database and use a WHERE clause to filter the data and then extract it back out. (That’s pretty desperate, but I know it happens!)

One simple way to do this in Python is to use the filter function. Let’s say you pull the data from the file into a series of tuples.

file = open("Patients.csv", 'r')
patients = [line.rstrip().split('\t') for line in file]

Now suppose age is the 3rd column in the data. You need to create a small function to determine whether a tuple meets the criteria:

def f(x): return int(x[2]) >= 60

Then you use the filter function and apply that function to the data.

matches = filter(f, patients)

This is a contrived example, but I hope it illustrates a beginning of how you might use functional programming and that it gives you a flavor for how this can be a powerful approach.

Add comment March 7, 2008

Remove / Delete Directory and Its Contents in Linux

(Note: Please see my latest posts at my new blog!)

rm -rf <DirectoryName>

This command will remove a directory and its contents without prompting you to make sure you want to delete each file in the directory. Substitute <DirectoryName> with the actual directory name.

5 comments February 20, 2008

Next Posts Previous Posts


Categories

Archives

Top Posts