我在寫兩種模式的腳本:'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