2012-02-27 29 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

相關問題