本文整理汇总了Python中difflib.ndiff方法的典型用法代码示例。如果您正苦于以下问题:Python difflib.ndiff方法的具体用法?Python difflib.ndiff怎么用?Python difflib.ndiff使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类difflib
的用法示例。
在下文中一共展示了difflib.ndiff方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: assertHTMLEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertHTMLEqual(self, html1, html2, msg=None):
"""
Asserts that two HTML snippets are semantically the same.
Whitespace in most cases is ignored, and attribute ordering is not
significant. The passed-in arguments must be valid HTML.
"""
dom1 = assert_and_parse_html(self, html1, msg,
'First argument is not valid HTML:')
dom2 = assert_and_parse_html(self, html2, msg,
'Second argument is not valid HTML:')
if dom1 != dom2:
standardMsg = '%s != %s' % (
safe_repr(dom1, True), safe_repr(dom2, True))
diff = ('\n' + '\n'.join(difflib.ndiff(
six.text_type(dom1).splitlines(),
six.text_type(dom2).splitlines())))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例2: assertMultiLineEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assertIsInstance(first, str, 'First argument is not a string')
self.assertIsInstance(second, str, 'Second argument is not a string')
if first != second:
# don't use difflib if the strings are too long
if (len(first) > self._diffThreshold or
len(second) > self._diffThreshold):
self._baseAssertEqual(first, second, msg)
firstlines = first.splitlines(keepends=True)
secondlines = second.splitlines(keepends=True)
if len(firstlines) == 1 and first.strip('\r\n') == first:
firstlines = [first + '\n']
secondlines = [second + '\n']
standardMsg = '%s != %s' % (safe_repr(first, True),
safe_repr(second, True))
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例3: check_compiles
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def check_compiles(dsl_expr, code):
types = ENV.__types__
expr, functions = to_expr(dsl_expr)
env = fn_types(functions)
env.update(types['__root__'].__field_types__)
expr = check(expr, types, env)
# test eval
lambda_expr = ExpressionCompiler.compile_lambda_expr(expr)
eval(compile(lambda_expr, '<expr>', 'eval'))
# test compile
py_expr = ExpressionCompiler.compile_expr(expr)
first = astor.to_source(py_expr).strip()
second = dedent(code).strip()
if first != second:
msg = ('Compiled code is not equal:\n\n{}'
.format('\n'.join(difflib.ndiff(first.splitlines(),
second.splitlines()))))
raise AssertionError(msg)
示例4: assertMultiLineEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assertIsInstance(first, basestring,
'First argument is not a string')
self.assertIsInstance(second, basestring,
'Second argument is not a string')
if first != second:
firstlines = first.splitlines(True)
secondlines = second.splitlines(True)
if len(firstlines) == 1 and first.strip('\r\n') == first:
firstlines = [first + '\n']
secondlines = [second + '\n']
standardMsg = '%s != %s' % (safe_repr(first, True),
safe_repr(second, True))
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例5: assertMultiLineEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assertIsInstance(first, basestring,
'First argument is not a string')
self.assertIsInstance(second, basestring,
'Second argument is not a string')
if first != second:
# don't use difflib if the strings are too long
if (len(first) > self._diffThreshold or
len(second) > self._diffThreshold):
self._baseAssertEqual(first, second, msg)
firstlines = first.splitlines(True)
secondlines = second.splitlines(True)
if len(firstlines) == 1 and first.strip('\r\n') == first:
firstlines = [first + '\n']
secondlines = [second + '\n']
standardMsg = '%s != %s' % (safe_repr(first, True),
safe_repr(second, True))
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例6: assertHTMLEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertHTMLEqual(self, html1, html2, msg=None):
"""
Assert that two HTML snippets are semantically the same.
Whitespace in most cases is ignored, and attribute ordering is not
significant. The arguments must be valid HTML.
"""
dom1 = assert_and_parse_html(self, html1, msg, 'First argument is not valid HTML:')
dom2 = assert_and_parse_html(self, html2, msg, 'Second argument is not valid HTML:')
if dom1 != dom2:
standardMsg = '%s != %s' % (
safe_repr(dom1, True), safe_repr(dom2, True))
diff = ('\n' + '\n'.join(difflib.ndiff(
str(dom1).splitlines(), str(dom2).splitlines(),
)))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例7: assertXMLEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertXMLEqual(self, xml1, xml2, msg=None):
"""
Assert that two XML snippets are semantically the same.
Whitespace in most cases is ignored and attribute ordering is not
significant. The arguments must be valid XML.
"""
try:
result = compare_xml(xml1, xml2)
except Exception as e:
standardMsg = 'First or second argument is not valid XML\n%s' % e
self.fail(self._formatMessage(msg, standardMsg))
else:
if not result:
standardMsg = '%s != %s' % (safe_repr(xml1, True), safe_repr(xml2, True))
diff = ('\n' + '\n'.join(
difflib.ndiff(xml1.splitlines(), xml2.splitlines())
))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例8: do_disassembly_test
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def do_disassembly_test(self, func, expected):
s = StringIO.StringIO()
save_stdout = sys.stdout
sys.stdout = s
dis.dis(func)
sys.stdout = save_stdout
got = s.getvalue()
# Trim trailing blanks (if any).
lines = got.split('\n')
lines = [line.rstrip() for line in lines]
expected = expected.split("\n")
import difflib
if expected != lines:
self.fail(
"events did not match expectation:\n" +
"\n".join(difflib.ndiff(expected,
lines)))
示例9: assertMultiLineEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
self.assertIsInstance(first, str, 'First argument is not a string')
self.assertIsInstance(second, str, 'Second argument is not a string')
if first != second:
# don't use difflib if the strings are too long
if (len(first) > self._diffThreshold or
len(second) > self._diffThreshold):
self._baseAssertEqual(first, second, msg)
firstlines = first.splitlines(keepends=True)
secondlines = second.splitlines(keepends=True)
if len(firstlines) == 1 and first.strip('\r\n') == first:
firstlines = [first + '\n']
secondlines = [second + '\n']
standardMsg = '%s != %s' % _common_shorten_repr(first, second)
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例10: is_summary_prob
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def is_summary_prob(subtext: str, fulltext: str) -> float:
"""
判断subtext是fulltext的摘要的概率。概率大于0.5可认为是。
"""
sub_sentences = split_sentences(subtext)
full_sentences = split_sentences(fulltext)
if not sub_sentences:
return 1.0 if full_sentences else 0.0
elif not full_sentences:
return 0.0
num_sub = len(sub_sentences)
num_full = len(full_sentences)
if num_sub - num_full >= 0:
return 0.0
max_check = min(num_sub * 2 + 1, num_full)
num_positive = 0
for delta in difflib.ndiff(sub_sentences, full_sentences[:max_check]):
diff_type = delta[0]
if diff_type == ' ' or diff_type == '+':
num_positive += 1
elif diff_type == '-':
num_positive -= 3
return max(0.0, min(1.0, num_positive / max_check))
示例11: ndiffAssertEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def ndiffAssertEqual(self, first, second):
"""Like failUnlessEqual except use ndiff for readable output."""
if first != second:
sfirst = unicode(first)
ssecond = unicode(second)
# Using the built-in .splitlines() method here will cause incorrect
# results when splitting statements that have quoted CR/CR+LF
# characters.
sfirst = sqlparse.utils.split_unquoted_newlines(sfirst)
ssecond = sqlparse.utils.split_unquoted_newlines(ssecond)
diff = difflib.ndiff(sfirst, ssecond)
fp = StringIO()
fp.write(NL)
fp.write(NL.join(diff))
print fp.getvalue()
raise self.failureException, fp.getvalue()
示例12: assertHTMLEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertHTMLEqual(self, html1, html2, msg=None):
"""
Asserts that two HTML snippets are semantically the same.
Whitespace in most cases is ignored, and attribute ordering is not
significant. The passed-in arguments must be valid HTML.
"""
dom1 = assert_and_parse_html(self, html1, msg, 'First argument is not valid HTML:')
dom2 = assert_and_parse_html(self, html2, msg, 'Second argument is not valid HTML:')
if dom1 != dom2:
standardMsg = '%s != %s' % (
safe_repr(dom1, True), safe_repr(dom2, True))
diff = ('\n' + '\n'.join(difflib.ndiff(
six.text_type(dom1).splitlines(),
six.text_type(dom2).splitlines(),
)))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例13: assertXMLEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertXMLEqual(self, xml1, xml2, msg=None):
"""
Asserts that two XML snippets are semantically the same.
Whitespace in most cases is ignored, and attribute ordering is not
significant. The passed-in arguments must be valid XML.
"""
try:
result = compare_xml(xml1, xml2)
except Exception as e:
standardMsg = 'First or second argument is not valid XML\n%s' % e
self.fail(self._formatMessage(msg, standardMsg))
else:
if not result:
standardMsg = '%s != %s' % (safe_repr(xml1, True), safe_repr(xml2, True))
diff = ('\n' + '\n'.join(
difflib.ndiff(
six.text_type(xml1).splitlines(),
six.text_type(xml2).splitlines(),
)
))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))
示例14: prompt_overwrite_json
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def prompt_overwrite_json(original, new, target_path, dumps=json_dumps):
"""
Prompt end user with a diff of original and new json that may
overwrite the file at the target_path. This function only displays
a confirmation prompt and it is up to the caller to implement the
actual functionality. Optionally, a custom json.dumps method can
also be passed in for output generation.
"""
# generate compacted ndiff output.
diff = '\n'.join(l for l in (
line.rstrip() for line in difflib.ndiff(
json_dumps(original).splitlines(),
json_dumps(new).splitlines(),
))
if l[:1] in '?+-' or l[-1:] in '{}' or l[-2:] == '},')
basename_target = basename(target_path)
return prompt(
"Generated '%(basename_target)s' differs with '%(target_path)s'.\n\n"
"The following is a compacted list of changes required:\n"
"%(diff)s\n\n"
"Overwrite '%(target_path)s'?" % locals(),
choices=(
('Yes', True),
('No', False),
),
default_key=1,
)
示例15: assertDictEqual
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import ndiff [as 别名]
def assertDictEqual(self, d1, d2, msg=None):
self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
if d1 != d2:
standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
diff = ('\n' + '\n'.join(difflib.ndiff(
pprint.pformat(d1).splitlines(),
pprint.pformat(d2).splitlines())))
standardMsg = self._truncateMessage(standardMsg, diff)
self.fail(self._formatMessage(msg, standardMsg))