2012-02-27 63 views
0

我在写两种模式的脚本:'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)



__
(*)的表现,持续时间,等更好...

回答

0

也许最重要的考虑因素是你不要想要维护两个必须保持同步的独立脚本。

是否可以更改每个命令(或者在每个命令周围编写一个shell),以便它可以在“预览”或“执行”模式下运行?然后,您可以运行一个脚本,并将参数传递给每个命令,告诉它运行哪种模式。

作为示例,在bash中,您有时可以将变量设置为“echo”或不变,具体取决于在模式上。然后可以写命令等

$ECHO command args 

这将任一回声命令(如果$ECHO是“回波”)或执行(如果$ECHO为空)。当然这是非常简单的,但你也许可以应用类似的技术。

0

这个问题被标记为语言不可知论,但有些反应可能会在某些语言中使用语法技巧,而在其他语言中则可能不会。就个人而言,我会推荐一个系统,根据模式在其中定义对项目的操作,而“脚本本身”调用这些操作并且完全不依赖模式。例如:

[preview script] 
define function handleMessage(args) to print "handle message:"+args 
define function handleCommand(args) to print "handle command: "+args 

[update script] 
define function handleMessage(args) to actually handle the message (send it somewhere etc.) 
define function handleCommand(args) to actually handle the command (execute it etc.) 

[common part] 
handleMessage(message1) 
for command in command_list: 
    handleCOmmand(command) 

这样您就不会复制您的命令流并且代码更易于使用。大多数语言都会使用虚拟函数覆盖来实现这种机制,其成本对于您引用的通话计数而言可以忽略不计。

+0

其实,如果我想保持它与语言无关,真正的代码是在Python中。这对于使用exec从command_list执行命令来说是完美的。使用Python,可以很容易地在预览脚本中生成命令列表并在更新脚本中执行它们。但我觉得也是一个脚本运行两次版本有他的优点... – dolma33 2012-02-28 22:22:25

0

也许是你的建议和@ michal的想法的结合。如果你不打算做预览,是否有人通过逻辑,有一个虚拟输出目的地。

if (isPreview) 
    dest = ValidMessageSink 
else 
    dest = \dev\null 

[always] 
if condition 1: 
add message1 to dest 
add command1 to command_list 

for element in list1: 
    add something about element to dest 
    add some command involving element to command_list 


[update_script] 
if (isUpdate) 
    for command in command_list: 
    execute command 
+0

嗯.. update_script是为了跳过所有德条件测试。这个想法是:而不是执行相同的代码两次(包括所有条件等),如果预览模式保存输出,如果模式更新则执行命令,通过条件一次,保存预览输出和command_list。这样,当运行更新脚本时,它不会测试其他任何内容,只需执行命令即可。 – dolma33 2012-02-28 20:12:06

+1

然后我会认为你的第二个选择是好的 - 只要你可以做一些事情来确保保存的“命令列表”的完整性。您可能想要防止意外运行陈旧预览的结果 - 或者两次运行同一个预览。 – AShelly 2012-02-28 20:16:11

+0

我正在考虑创建一个预览对象,该对象存储有关预览(属性,时间戳,...)和command_list的所有univoque数据。 (此预览对象也包含在单脚本运行的两次版本中,以确保更新与预览中发布的更新相同:例如,在用户正在查看预览时数据库已更改,而不会根据该预览执行操作。此版本的预览对象在预览生成时包含数据库的快照。) – dolma33 2012-02-28 22:16:43

相关问题