2013-05-05 34 views
1

我剛完成了一段很長的代碼的重構。文件內容是否完全存在於庫文件中

我的重構包括在許多文件夾中的許多文件中將源代碼分解爲許多函數。

現在我已經完成了,我想確保原始代碼中沒有行不在我創建的新文件中的一行中。

我需要一個僞代碼是這樣的:

for line in sourceCode: 
    if length(grep line refacoredLib)==0: 
     print line + " does not exist in refactored code" 

我首先想到的是寫一個python \ bash的實現,有沒有你認識的更優雅的解決方案嗎? 謝謝!

+1

「單線」?可能不會。 「優雅的單線」絕對不是。爲什麼你只希望它只有一行?這是一個非常毫無意義的要求。 – 2013-05-05 12:14:43

+0

謝謝,確實不需要它在一條線上。 – 2013-05-05 12:20:32

+5

不是一個正確的答案,但重構沒有自動迴歸測試並不一定是一個好主意。 – 2013-05-05 12:34:17

回答

1

那麼,你可以在Python中比較幾行代碼,但那不會是一行代碼。

source_files = ['source1.py', 'source2.py'] 
new_files = ['new1.py', 'new2.py'] 

old_lines, new_lines = set(), set() 
for source in source_files: 
    with open(source) as sf: 
     old_lines.update(sf) 
for new in new_files: 
    with open(new) as nf: 
     new_lines.update(nf) 
for line in old_lines - new_lines: 
    print line + " does not exist in refactored code" 
+0

'old_lines - new_lines'會更有用嗎? – 2013-05-05 13:22:42

+0

@LennartRegebro它會,謝謝。編輯。 – 2013-05-05 13:26:51

6

或者,如果你不想重新發明輪子:

cat newfiles/* | sort > /tmp/new 
cat oldfile.py | sort > /tmp/old 
comm -23 /tmp/old /tmp/new 

不Python的,我知道,但仍。

相關問題