Python Difflib - 如何获得不同的序列,如 PHP 不同的 Class 格式
Python Difflib - How to get diff sequneces like PHP Diff Class format
我正在阅读 python difflib 文档。根据 difflib.differ 输出是:
代码含义
'-' 序列 1 独有的行
'+' 序列 2 独有的行
' ' 两个序列共有的行
'? ' 两个输入序列中都不存在行
我也在 Whosebug 上阅读这个问题
Python Difflib - How to Get SDiff Sequences with "Change" Op but not be able to add a comment on the Sнаđошƒаӽ的回答。
我不太了解 Perl 的 Sdiff 是什么,但我需要调整此功能:
def sdiffer(s1, s2):
differ = difflib.Differ()
diffs = list(differ.compare(s1, s2))
i = 0
sdiffs = []
length = len(diffs)
while i < length:
line = diffs[i][2:]
if diffs[i].startswith(' '):
sdiffs.append(('u', line))
elif diffs[i].startswith('+ '):
sdiffs.append(('+', line))
elif diffs[i].startswith('- '):
if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously
sdiffs.append(('c', line))
i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '):
sdiffs.append(('c', line))
i += 2
else:
sdiffs.append(('-', line))
i += 1
return sdiffs
上面的函数我试过了,它 return UNCHANGE、ADDED 和 DELETED 的值。 DELETED 更复杂,有 4 种不同情况,即:
情况一:插入一些字符修改的行
- The good bad
+ The good the bad
? ++++
情况2:删除部分字符修改行
- The good the bad
? ----
+ The good bad
案例3:修改行删除插入and/or替换部分字符:
- The good the bad and ugly
? ^^ ----
+ The g00d bad and the ugly
? ^^ ++++
情况4:该行被删除
- The good the bad and the ugly
+ Our ratio is less than 0.75!
我不知道如何在这段代码中进行调整
elif diffs[i].startswith('- '):
if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously
sdiffs.append(('c', line))
i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '):
sdiffs.append(('c', line))
i += 2
else:
sdiffs.append(('-', line))
跳过'?'线。我只想在没有插入新行时附加(-),如果调用插入的新行则附加(+)。
我想我已经完成了我想要的 PHP 差异输出。
def sdiffer(s1, s2):
differ = difflib.Differ()
diffs = list(differ.compare(s1, s2))
i = 0
sdiffs = []
length = len(diffs)
sequence = 0
while i < length:
line = diffs[i][2:]
if diffs[i].startswith(' '):
sequence +=1
sdiffs.append((sequence,'u', line))
elif diffs[i].startswith('+ '):
sequence +=1
sdiffs.append((sequence,'+', line))
elif diffs[i].startswith('- '):
sequence +=1
sdiffs.append((sequence,'-',diffs[i][2:]))
if i+1 < length and diffs[i+1].startswith('? '):
if diffs[i+3].startswith('?') and i+3 < length : # case 3
sequence +=1
sdiffs.append((sequence,'+',diffs[i+2][2:]))
i+=3
elif diffs[i+2].startswith('?') and i+2 < length: # case 2
sequence +=1
sdiffs.append((sequence,'+',diffs[i+2][2:]))
i+=2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '): # case 1
sequence +=1
sdiffs.append((sequence,'+', diffs[i+1][2:]))
i += 2
else: # the line is deleted and inserted new line # case 4
sequence +=1
sdiffs.append((sequence,'+', diffs[i+1][2:]))
i+=1
i += 1
return sdiffs
我正在阅读 python difflib 文档。根据 difflib.differ 输出是:
代码含义 '-' 序列 1 独有的行 '+' 序列 2 独有的行 ' ' 两个序列共有的行 '? ' 两个输入序列中都不存在行
我也在 Whosebug 上阅读这个问题 Python Difflib - How to Get SDiff Sequences with "Change" Op but not be able to add a comment on the Sнаđошƒаӽ的回答。
我不太了解 Perl 的 Sdiff 是什么,但我需要调整此功能:
def sdiffer(s1, s2):
differ = difflib.Differ()
diffs = list(differ.compare(s1, s2))
i = 0
sdiffs = []
length = len(diffs)
while i < length:
line = diffs[i][2:]
if diffs[i].startswith(' '):
sdiffs.append(('u', line))
elif diffs[i].startswith('+ '):
sdiffs.append(('+', line))
elif diffs[i].startswith('- '):
if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously
sdiffs.append(('c', line))
i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '):
sdiffs.append(('c', line))
i += 2
else:
sdiffs.append(('-', line))
i += 1
return sdiffs
上面的函数我试过了,它 return UNCHANGE、ADDED 和 DELETED 的值。 DELETED 更复杂,有 4 种不同情况,即:
情况一:插入一些字符修改的行
- The good bad
+ The good the bad
? ++++
情况2:删除部分字符修改行
- The good the bad
? ----
+ The good bad
案例3:修改行删除插入and/or替换部分字符:
- The good the bad and ugly
? ^^ ----
+ The g00d bad and the ugly
? ^^ ++++
情况4:该行被删除
- The good the bad and the ugly
+ Our ratio is less than 0.75!
我不知道如何在这段代码中进行调整
elif diffs[i].startswith('- '):
if i+1 < length and diffs[i+1].startswith('? '): # then diffs[i+2] starts with ('+ '), obviously
sdiffs.append(('c', line))
i += 3 if i + 3 < length and diffs[i + 3].startswith('? ') else 2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '):
sdiffs.append(('c', line))
i += 2
else:
sdiffs.append(('-', line))
跳过'?'线。我只想在没有插入新行时附加(-),如果调用插入的新行则附加(+)。
我想我已经完成了我想要的 PHP 差异输出。
def sdiffer(s1, s2):
differ = difflib.Differ()
diffs = list(differ.compare(s1, s2))
i = 0
sdiffs = []
length = len(diffs)
sequence = 0
while i < length:
line = diffs[i][2:]
if diffs[i].startswith(' '):
sequence +=1
sdiffs.append((sequence,'u', line))
elif diffs[i].startswith('+ '):
sequence +=1
sdiffs.append((sequence,'+', line))
elif diffs[i].startswith('- '):
sequence +=1
sdiffs.append((sequence,'-',diffs[i][2:]))
if i+1 < length and diffs[i+1].startswith('? '):
if diffs[i+3].startswith('?') and i+3 < length : # case 3
sequence +=1
sdiffs.append((sequence,'+',diffs[i+2][2:]))
i+=3
elif diffs[i+2].startswith('?') and i+2 < length: # case 2
sequence +=1
sdiffs.append((sequence,'+',diffs[i+2][2:]))
i+=2
elif diffs[i+1].startswith('+ ') and i+2<length and diffs[i+2].startswith('? '): # case 1
sequence +=1
sdiffs.append((sequence,'+', diffs[i+1][2:]))
i += 2
else: # the line is deleted and inserted new line # case 4
sequence +=1
sdiffs.append((sequence,'+', diffs[i+1][2:]))
i+=1
i += 1
return sdiffs