0

我正在努力将我编写的一些代码翻译成一个并行进程,以便分布在我家乡大学的计算机集群上。为了准备为集群编写脚本,我首先阅读了集群提供的 Python 代码示例片段之一:

#! /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)

我可以跟踪代码到最后四行 [例如,最多到“os.system('sed -i ...)”],但是很难跟踪代码。有没有其他人可以帮助我理解最后四行在做什么。有没有办法用伪代码来描述谎言?据我所知,第一行 sed 试图用 primes[trial] 的值替换“种子值”,但我不确定种子值是什么。我也不确定如何理解下一行中的“stepval”。任何其他人可以对这些问题提出的任何启发都将不胜感激。

4

2 回答 2

1

您可以在 sed 上进行一些搜索:http ://en.wikipedia.org/wiki/Sed#In-place_editing

简而言之:用insed -i 's/old/new/g' file替换所有出现的。该标志告诉它在线执行,修改文件本身。oldnewfile-i

在您的代码中,seedvalueandstepval只是文本文件中的两个单词wwd.nm.confqsubScript.sh. 就像您在文本编辑器或文字处理器中所做的那样,这些命令正在替换这些单词。

于 2013-09-01T03:55:55.240 回答
1

seedvalue是一个字符串,所以它的值就是它本身,与stepval.

第一sed行将用 value 替换(在所有行上,就是这样,它代表“global”)中的所有seedvalue字符串。这样想:wwd.nm.conf/gstr(primes[trial])

stepvalue在文件中找到文本的任何位置wwd.nm.conf,放置值str(primes[trial])(在 Python 中计算的任何值)。

第二个sed调用将执行类似的操作,但使用stepvaland 的结果step将替换文件中的文本qsubScript.sh

于 2013-09-01T03:56:02.593 回答