脚本在两种模式下工作(preVIEW和更新) [英] Script working in two modes (preview and update)

查看:193
本文介绍了脚本在两种模式下工作(preVIEW和更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个脚本,在两种模式工作:'preVIEW 更新
当它运行在'preVIEW 模式下,脚本生成将要做出(像一个diff输出)的变化preVIEW。
当它运行在更新模式,它适用于那些改变。

I'm writing a script which works in two modes: 'preview' and 'update'.
When it runs in 'preview' mode, the script generates a preview of the changes that will be made (something like a diff output).
When it runs in 'update' mode, it applies those changes.



的preVIEW输出可以以这种方式被概括



The preview output could be generalized in this way:

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. 

正如你已经可以想像,在动作模式该preVIEW会做这样的事情

As you already can imagine, the 'action' mode for this preview will do something like

- Create item 231234 with his information
- Update description for item 211012
- Update description and name for item 218998
- Remove item 212099



到现在为止,我已经构建脚本按照这样的逻辑:
(注:这个伪code的刚需为这个问题的自我解释线,并且显然是实际code真的不一样)



Until now, I've been building the script following this logic:
(note: this pseudo code has just the self-explaining lines needed for this question, and is obviously really different from actual code)

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

 ....


这个脚本通常采用大约在300〜3000 元素列表的关心,测试约 80-120 的条件。
该脚本预计需要他有点长的时间来执行的(例如,是确定该脚本在运行'preVIEW 模式会3mins较大名单)的。


This script will usually takes care of lists of circa 300 to 3000 elements, testing about 80-120 conditions.
The script is expected to take his somewhat long time to execute (for example, is ok that script running in 'preview' mode takes 3mins for larger lists).


但现在我想知道,如果它不会是的的(*)根据下面的逻辑来构建脚本:


But now I'm wondering if it wouldn't be "better" (*) to build the script under the following logic:

[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

哪个版本应该是preferred,在何种情况下,为什么?

Which version should be preferred, under which circumstances and why?



编辑:仅仅因为是清晰的,这是两个选择的简历



Just for being clearer, this is the resume of the two options

一个。 一个脚本,运行两次:
我有两次运行一个脚本。
    它检查很多的条件,并为每个那些,     这取决于其运行模式,它会增加一些字符串到preVIEW输出,或将执行命令。
    (code被写入只有一次,但吨的条件下执行两次;     该脚本首先在名为preVIEW模式,然后在更新模式。)

a. "single script, runs twice":
I have a single script which runs twice.
It checks a lot of conditions, and for each of those, depending on which mode it is running, it will add some string to the preview output, or it will execute a command.
(code is written only once, but the ton of conditions is executed twice; the script is first called in 'preview' mode and then in 'update' mode.)

乙。 两种不同的脚本:
在previous脚本的所有条件仅在preVIEW'脚本将被检查。
    对于每一个条件,它会增加一些字符串到preVIEW输出,和一个命令一个command_list。
    更新脚本只是在command_list,仅此而已执行的每一个命令。
    (的previous'一'脚本测试code被写入只有一次,它总是会产生preVIEW和command_list)

b. "two different scripts":
all the conditions of the previous script will be checked only in the 'preview' script.
For each condition, it will add some string to the preview output, and a command to a command_list.
The 'update' script will simply execute every command in that command_list, nothing more.
(the testing code of previous 'a' script is written only once, and it will always generate preview and command_list)



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



__
(*) better for performance, duration, etc ...

推荐答案

你的建议和@米哈尔的想法也许是一个组合。做一次通过的逻辑,与虚拟输出目的地,如果你不打算做了preVIEW。

Perhaps a combination of your proposal and @michal's idea. Do one pass through the logic, with a dummy output destination if you are not going to do a preview.

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

这篇关于脚本在两种模式下工作(preVIEW和更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆