【问题标题】:trimming the output of difflib.Differ修剪 difflib.Differ 的输出
【发布时间】:2018-07-10 18:35:13
【问题描述】:

我想为assertEqual 不匹配提供更多信息性消息,为此我使用difflib.Differ。但是,当文件很大时,它会返回太多信息。

我如何实现像 diffs windowing 这样只显示几行上下文的东西?

@@ -155,6 +155,8 @@ except (ImportError,) as e:
     raise
+tst
+
 class KeepTextFilter(object):
     def __init__(self, regexes = [], f_notify=None):

这是我的测试代码:

def get_data():

    left, right = [],[]
    for num in range(0,10):
        lmark = rmark = " "
        if not (num % 5):
            lmark, rmark = "L", "R"
        left.append("%02d%s" % (num, lmark))
        right.append("%02d%s" % (num,rmark))

    return left, right

from difflib import Differ

differ = Differ()
left, right = get_data()
print(left)
print(right)    

output = "\n".join(differ.compare(left, right))
print(output)

这就是输出的样子(给定更大的列表,它会输出所有内容)

['00L', '01 ', '02 ', '03 ', '04 ', '05L', '06 ', '07 ', '08 ', '09 ']
['00R', '01 ', '02 ', '03 ', '04 ', '05R', '06 ', '07 ', '08 ', '09 ']
- 00L
+ 00R
  01 
  02 
  03 
  04 
- 05L
+ 05R
  06 
  07 
  08 
  09 

我怎样才能拥有一个1 的窗口。即

- 00L
+ 00R
  01 
  04 
- 05L
+ 05R
  06 

我正在考虑用deque(maxlen=1) 破解一些东西,但我想我会问 - 这似乎是一个明显的要求。我查看了 Differ()compare 签名的构造函数,但都没有这个选项。

【问题讨论】:

    标签: python unit-testing compare


    【解决方案1】:

    这是我想出来的

    class DiffFormatter(difflib.Differ):
    
        def _window(self, lines, window=None):
            try:
    
                if not window:
                    return lines
                if not isinstance(window, int):
                    raise TypeError("window has to be an int")
    
                #remember, at most, `window` # of lines
                dq = deque(maxlen=window)
                cntr = 0
    
                res = []
    
                for line in lines:
    
                    if line[0] in ("+","-"):
                        #reset cntr
                        cntr = window
                        while True:
                            #add `before` context lines
                            try:
                                #try if res.extend(dq) works
                                res.append(dq.popleft())
                            except (IndexError,) as e:
                                break
                        res.append(line)
                    elif cntr > 0:
                        #`after` context
                        #cntr, while > 0 adds line to res
                        cntr -= 1
                        res.append(line)
                    else:
                        #this line won't be used, unless a later line
                        #requires it in context.
                        dq.append(line)
                return res
            except (Exception,) as e:
                raise
    
        def format(self, exp, got, window=None):
            try:
                exp_ = exp.splitlines()
                got_ = got.splitlines()
                lines = self.compare(exp_, got_)
    
                if window:
                    lines2 = self._window(lines, window)
                else:
                    lines2 = list(lines)
    
                msg = "\n".join(lines2)
                msg = msg.strip()
                if msg and msg[1] != " ":
                    msg = "  %s" % (msg) 
                return msg 
    
            except (Exception,) as e:
                raise
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 1970-01-01
      • 2014-07-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2015-02-03
      相关资源
      最近更新 更多