我在写两种模式的脚本:'preview'
和'update'
。
当它在'preview'
模式下运行时,脚本会生成将进行的更改的预览(类似于diff输出)。
当它在'update'
模式下运行时,它将应用这些更改。在两种模式下工作的脚本(预览和更新)
预览输出可以这样概括:
Item 231234 is new to the db. It will be added to the db with the following data:
Name:
Description:
etc...
Item 211012 already exists in the database, but some changes have been made:
Different description:
Old description: "Blah blah blah"
New description: "Improved blah blah blah"
Item 218998 already exists in the database, but some changes have been made:
Different name:
Old name: "I am 218998"
New name: "John"
Different description:
Old description: "Blah blah blah"
New description: "Improved blah blah blah"
Item 212099 doesn't exists anymore, it will be removed from the database.
正如你已经可以想象,'action'
模式可供预览会做这样的事情
- Create item 231234 with his information
- Update description for item 211012
- Update description and name for item 218998
- Remove item 212099
到目前为止,我一直在按照这个逻辑构建脚本:
(注意:这个伪代码中有刚需对这一问题的自我解释线,并且显然是从实际的代码真的不同)
if condition 1:
if mode is 'preview': add message1 to preview
if mode is 'update': execute command1
for element in list1:
if mode is 'preview': add something about element to preview
if mode is 'update': execute some command involving element
for element in list2:
if condition 2:
if mode is 'preview': add message2 about this element to preview
if mode is 'update': execute command2 involving element
if condition 3:
if mode is 'preview': add message3 about this element to preview
if mode is 'update': execute command3 involving element
....
这个脚本通常会照顾circa 300 to 3000
元素的列表,测试约80-120
条件。
脚本预计需要很长时间才能执行(例如,在'preview'
模式下运行的脚本可能需要3分钟才能执行较大的列表)。
但现在我想知道,如果它不会是“更好”(*),以构建以下逻辑下的脚本:
[preview_script]
if condition 1:
add message1 to preview
add command1 to command_list
for element in list1:
add something about element to preview
add some command involving element to command_list
[update_script]
for command in command_list:
execute command
哪个版本应该是首选,其下情况和原因?
编辑:只是为了更清澈,这是两个选项
一个简历。 “单个脚本,运行两次”:
我有一个脚本运行两次。
它检查很多条件,并且对于其中的每一个, 取决于它运行的是哪种模式,它会在预览输出中添加一些字符串,或者它将执行命令。
(代码仅被写入一次,但是执行两次的条件吨; 脚本在“预览”模式称为第一,然后在“更新”模式)。
湾“两种不同的脚本”:
仅在'预览'脚本中检查前一个脚本的所有条件。
对于每个条件,它会向预览输出添加一些字符串,并向command_list添加一条命令。
'update'脚本将只执行该command_list中的每个命令,仅此而已。
(以前的“一”脚本的测试代码编写只有一次,它总是会产生预览和command_list)
__
(*)的表现,持续时间,等更好...
其实,如果我想保持它与语言无关,真正的代码是在Python中。这对于使用exec从command_list执行命令来说是完美的。使用Python,可以很容易地在预览脚本中生成命令列表并在更新脚本中执行它们。但我觉得也是一个脚本运行两次版本有他的优点... – dolma33 2012-02-28 22:22:25