I am working on translating some code I have written into a parallel process to be distributed on a computer cluster at my home university. In order to prepare to write my script for the cluster, I began by reading one of the sample snippets of Python code provided by the cluster:
#! /usr/bin/python
# This script replicates a given test with varied parameters
# creating unique submit scripts and executing the submission to the CRC SGE queue
# Imports
import os
import shutil
import string
basedir=os.getcwd()
origTestFol='wwd'
templFol='template'
origTestDir= basedir + '/' + origTestFol
templFolDir= basedir + '/' + templFol
steps=[0,1,2,3,4,5,6,7,8,9]
primes=[2,3,5,7,11,13,17,19,23,29,31]
trials=[6,7,8,9,10]
for step in steps:
newTestDir= origTestDir + '_nm_' + str(step)
if not os.path.exists(newTestDir):
os.mkdir(newTestDir)
os.chdir(newTestDir)
for trial in trials:
newTestSubDir= newTestDir + '/' + str(trial)
if not os.path.exists(newTestSubDir):
shutil.copytree(templFolDir,newTestSubDir)
os.chdir(newTestSubDir)
os.system('sed -i \'s/seedvalue/' + str(primes[trial]) + '/g\' wwd.nm.conf')
os.system('sed -i \'s/stepval/' + str(step) + '/g\' qsubScript.sh')
os.system('qsub qsubScript.sh')
os.chdir(basedir)
I can follow the code up to the last four lines [e.g. up to "os.system('sed -i ...)"] but then have a hard time following the code. Is there any chance others could help me understand what these last four lines are doing. Is there a way to describe the lies in pseudocode? So far as I can tell, the first sed line attempts to replace "seedvalue" with the value of primes[trial], but I'm not sure what the seedvalue is. I'm also not sure how to understand the "stepval" in the subsequent line. Any light others can shed on these questions will be most appreciated.
You could do a little search on sed: http://en.wikipedia.org/wiki/Sed#In-place_editing
In short:
sed -i 's/old/new/g' file
replaces all the occurrence ofold
withnew
infile
. The-i
flag tells it do it in-line, modifying the file itself.In your code,
seedvalue
andstepval
are nothing but two words in the text fileswwd.nm.conf
andqsubScript.sh
. Those commands are replacing those words just as you'd do in your text editor or word processor.