python – 用于HTML字符串的漂亮打印assertEqual()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 用于HTML字符串的漂亮打印assertEqual(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1694字,纯文字阅读大概需要3分钟。
内容图文
![python – 用于HTML字符串的漂亮打印assertEqual()](/upload/InfoBanner/zyjiaocheng/720/77960f93e017400a8269032761b01c7e.jpg)
我想比较python unittest中包含html的两个字符串.
有没有一种方法可以在人类友好(差异化)版本中输出结果?
解决方法:
几年前我提交了一个补丁来做这件事.该补丁被拒绝,但您仍然可以在python bug list上查看.
我怀疑你是不是想破解你的unittest.py来应用补丁(如果它在所有这些时间后仍然可以工作),但这里的功能是将两个字符串减少到一个可管理的大小,同时仍保持至少部分不同的东西.只要您不想要完全不同,这可能就是您想要的:
def shortdiff(x,y):
'''shortdiff(x,y)
Compare strings x and y and display differences.
If the strings are too long, shorten them to fit
in one line, while still keeping at least some difference.
'''
import difflib
LINELEN = 79
def limit(s):
if len(s) > LINELEN:
return s[:LINELEN-3] + '...'
return s
def firstdiff(s, t):
span = 1000
for pos in range(0, max(len(s), len(t)), span):
if s[pos:pos+span] != t[pos:pos+span]:
for index in range(pos, pos+span):
if s[index:index+1] != t[index:index+1]:
return index
left = LINELEN/4
index = firstdiff(x, y)
if index > left + 7:
x = x[:left] + '...' + x[index-4:index+LINELEN]
y = y[:left] + '...' + y[index-4:index+LINELEN]
else:
x, y = x[:LINELEN+1], y[:LINELEN+1]
left = 0
cruncher = difflib.SequenceMatcher(None)
xtags = ytags = ""
cruncher.set_seqs(x, y)
editchars = { 'replace': ('^', '^'),
'delete': ('-', ''),
'insert': ('', '+'),
'equal': (' ',' ') }
for tag, xi1, xi2, yj1, yj2 in cruncher.get_opcodes():
lx, ly = xi2 - xi1, yj2 - yj1
edits = editchars[tag]
xtags += edits[0] * lx
ytags += edits[1] * ly
# Include ellipsis in edits line.
if left:
xtags = xtags[:left] + '...' + xtags[left+3:]
ytags = ytags[:left] + '...' + ytags[left+3:]
diffs = [ x, xtags, y, ytags ]
if max([len(s) for s in diffs]) < LINELEN:
return '\n'.join(diffs)
diffs = [ limit(s) for s in diffs ]
return '\n'.join(diffs)
内容总结
以上是互联网集市为您收集整理的python – 用于HTML字符串的漂亮打印assertEqual()全部内容,希望文章能够帮你解决python – 用于HTML字符串的漂亮打印assertEqual()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。