Python difflib 模块,ndiff() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用difflib.ndiff()

项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
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))
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
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))
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
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))
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
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))
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _compare_eq_iterable(left, right, verbose=False):
    if not verbose:
        return [u('Use -v to get the full diff')]
    # dynamic import to speedup pytest
    import difflib

    try:
        left_formatting = pprint.pformat(left).splitlines()
        right_formatting = pprint.pformat(right).splitlines()
        explanation = [u('Full diff:')]
    except Exception:
        # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
        # sorted() on a list would raise. See issue #718.
        # As a workaround, the full diff is generated by using the repr() string of each item of each container.
        left_formatting = sorted(repr(x) for x in left)
        right_formatting = sorted(repr(x) for x in right)
        explanation = [u('Full diff (fallback to calling repr on each item):')]
    explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting))
    return explanation
项目:devsecops-example-helloworld    作者:boozallen    | 项目源码 | 文件源码
def assertMultiLineEqual(self, first, second, msg=None):
        """Assert that two multi-line strings are equal."""
        self.assertIsInstance(first, six.string_types, (
                'First argument is not a string'))
        self.assertIsInstance(second, six.string_types, (
                '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' % _common_shorten_repr(first, second)
            diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
            standardMsg = self._truncateMessage(standardMsg, diff)
            self.fail(self._formatMessage(msg, standardMsg))
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def write_json_diff_to_file(self, json_object1, json_object2, output_file):
        """
            Compares two json objects and if they does not match writes the
            difference into the output file
        """
        result = self.compare_json_objects(json_object1, json_object2,
                                           write_diff_to_console=False)
        status = True
        if not result:
            status = False
            sorted_json1 = self.sort_json_object(json_object1)
            sorted_json2 = self.sort_json_object(json_object2)
            json_obj1 = json.dumps(
                sorted_json1, indent=4, separators=(',', ':'),
                encoding="utf-8")
            json_obj2 = json.dumps(
                sorted_json2, indent=4, separators=(',', ':'),
                encoding="utf-8")
            te = open(output_file, 'w')
            diff = ("\n".join(
                difflib.ndiff(json_obj1.splitlines(), json_obj2.splitlines())))
            te.write(diff)
            te.close()
        return status
项目:style50    作者:cs50    | 项目源码 | 文件源码
def __init__(self, code):
        self.original = code

        comments = self.count_comments(code)

        try:
            # Avoid warning about comments if we don't knowhow to count them.
            self.comment_ratio = 1. if comments is None else comments / self.count_lines(code)
        except ZeroDivisionError:
            raise Error("file is empty")

        self.styled = self.style(code)

        # Count number of differences between styled and unstyled code (average of added and removed lines).
        self.diffs = sum(d[0] == "+" or d[0] == "-"
                         for d in difflib.ndiff(code.splitlines(True), self.styled.splitlines(True))) / 2

        self.lines = self.count_lines(self.styled)
        try:
            self.score = 1 - self.diffs / self.lines
        except ZeroDivisionError:
            raise Error("file is empty")
项目:data    作者:pytube    | 项目源码 | 文件源码
def check_render_rest(data_root, verbose=False):
    category_paths, video_paths = get_json_files(data_root)

    file_paths = category_paths + video_paths

    error_by_path = {}
    for file_path in file_paths:
        with open(file_path, encoding='UTF-8') as fp:
            serialized_blob = fp.read()
            re_serialized_blob = json.dumps(
                json.loads(serialized_blob),
                **JSON_FORMAT_KWARGS
            )
            if serialized_blob.strip() != re_serialized_blob.strip():
                error_by_path[file_path] = (serialized_blob, re_serialized_blob)

    if error_by_path:
        for path, blobs in error_by_path.items():
            print('Incorrect serialization order in {}'.format(path), flush=True)
            blobs = tuple(blob.splitlines(keepends=True) for blob in blobs)
            if verbose:
                print(''.join(difflib.ndiff(*blobs)), end="")
        sys.exit(1)
项目:transmutagen    作者:ergs    | 项目源码 | 文件源码
def _parse_coeffs(args):
    file = args.file
    check_existing = args.check_existing

    _coeffs = parse_crv_coeffs(file=file)

    if check_existing:
        for n in sorted(coeffs):
            for v in 'pq':
                print('Checking against', n, v)
                if _coeffs[n][v] == coeffs[n][v]:
                    print(n, v, "matches")
                else:
                    print(n, v, "doesn't match")
                    print(v, 'diff:')
                    print('\n'.join(difflib.ndiff(_coeffs[n][v], coeffs[n][v])))
    else:
        pprint.pprint(_coeffs, width=20)
项目:tst    作者:daltonserey    | 项目源码 | 文件源码
def tests_differ(activity, checkout):

    delta = {}
    diffs = [d for d in difflib.ndiff(checkout, activity) if d[0] != '?']
    index = 0
    for i in xrange(len(diffs)):
        line = diffs[i]
        if line[0] == ' ':
            index += 1
        elif line[0] == '+':
            previous = diffs[i-1] if (i > 0) else None
            delta.setdefault('tests', []).append(('+', line[2:], index))
            index += 1
        elif line[0] == '-':
            delta.setdefault('tests', []).append(('-', line[2:], index))
            # index is not changed, because this line was removed
        elif line[0] == '?':
            # this line is added by ndiff for information only
            pass

    return delta.get('tests', [])
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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(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))
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def do_disassembly_test(self, func, expected):
        s = io.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)))
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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))
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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)))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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)))
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
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))
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def _compare_eq_iterable(left, right, verbose=False):
    if not verbose:
        return [u('Use -v to get the full diff')]
    # dynamic import to speedup pytest
    import difflib

    try:
        left_formatting = pprint.pformat(left).splitlines()
        right_formatting = pprint.pformat(right).splitlines()
        explanation = [u('Full diff:')]
    except Exception:
        # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
        # sorted() on a list would raise. See issue #718.
        # As a workaround, the full diff is generated by using the repr() string of each item of each container.
        left_formatting = sorted(repr(x) for x in left)
        right_formatting = sorted(repr(x) for x in right)
        explanation = [u('Full diff (fallback to calling repr on each item):')]
    explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting))
    return explanation
项目:protoc-gen-lua-bin    作者:u0u0    | 项目源码 | 文件源码
def assertMultiLineEqual(self, first, second, msg=None):
    """Assert that two multi-line strings are equal."""
    assert isinstance(first, types.StringTypes), (
        'First argument is not a string: %r' % (first,))
    assert isinstance(second, types.StringTypes), (
        'Second argument is not a string: %r' % (second,))

    if first == second:
      return
    if msg:
      failure_message = [msg, ':\n']
    else:
      failure_message = ['\n']
    for line in difflib.ndiff(first.splitlines(True), second.splitlines(True)):
      failure_message.append(line)
      if not line.endswith('\n'):
        failure_message.append('\n')
    raise self.failureException(''.join(failure_message))
项目:protoc-gen-lua-bin    作者:u0u0    | 项目源码 | 文件源码
def assertMultiLineEqual(self, first, second, msg=None):
    """Assert that two multi-line strings are equal."""
    assert isinstance(first, types.StringTypes), (
        'First argument is not a string: %r' % (first,))
    assert isinstance(second, types.StringTypes), (
        'Second argument is not a string: %r' % (second,))

    if first == second:
      return
    if msg:
      failure_message = [msg, ':\n']
    else:
      failure_message = ['\n']
    for line in difflib.ndiff(first.splitlines(True), second.splitlines(True)):
      failure_message.append(line)
      if not line.endswith('\n'):
        failure_message.append('\n')
    raise self.failureException(''.join(failure_message))
项目:codenn    作者:sriniiyer    | 项目源码 | 文件源码
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()
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
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))
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
def _compare_eq_iterable(left, right, verbose=False):
    if not verbose:
        return [u('Use -v to get the full diff')]
    # dynamic import to speedup pytest
    import difflib

    try:
        left_formatting = pprint.pformat(left).splitlines()
        right_formatting = pprint.pformat(right).splitlines()
        explanation = [u('Full diff:')]
    except Exception:
        # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
        # sorted() on a list would raise. See issue #718.
        # As a workaround, the full diff is generated by using the repr() string of each item of each container.
        left_formatting = sorted(repr(x) for x in left)
        right_formatting = sorted(repr(x) for x in right)
        explanation = [u('Full diff (fallback to calling repr on each item):')]
    explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting))
    return explanation
项目:godot-python    作者:touilleMan    | 项目源码 | 文件源码
def _compare_eq_iterable(left, right, verbose=False):
    if not verbose:
        return [u('Use -v to get the full diff')]
    # dynamic import to speedup pytest
    import difflib

    try:
        left_formatting = pprint.pformat(left).splitlines()
        right_formatting = pprint.pformat(right).splitlines()
        explanation = [u('Full diff:')]
    except Exception:
        # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
        # sorted() on a list would raise. See issue #718.
        # As a workaround, the full diff is generated by using the repr() string of each item of each container.
        left_formatting = sorted(repr(x) for x in left)
        right_formatting = sorted(repr(x) for x in right)
        explanation = [u('Full diff (fallback to calling repr on each item):')]
    explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting))
    return explanation
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
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))
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
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))
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
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))
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
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)))
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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))
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
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)))
项目:liberator    作者:libscie    | 项目源码 | 文件源码
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))
项目:liberator    作者:libscie    | 项目源码 | 文件源码
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))
项目:djanoDoc    作者:JustinChavez    | 项目源码 | 文件源码
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))
项目:djanoDoc    作者:JustinChavez    | 项目源码 | 文件源码
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))
项目:webmon    作者:KarolBedkowski    | 项目源码 | 文件源码
def compare(self, old: str, old_date: str, new: str, new_date: str,
                ctx: common.Context, meta: dict) \
            -> ty.Tuple[bool, ty.Optional[str], ty.Optional[dict]]:
        # pylint: disable=invalid-sequence-index
        old = old.replace(common.RECORD_SEPARATOR, '\n\n')
        new = new.replace(common.RECORD_SEPARATOR, '\n\n')
        old_lines = old.split('\n')
        res = list(difflib.ndiff(old_lines, new.split('\n')))

        changed_lines = sum(1 for line in res if line and line[0] != ' ')
        if not _check_changes(ctx, changed_lines, len(old_lines),
                              self.conf.get("changes_threshold"),
                              self.conf.get("min_changed")):
            return False, None, None

        return True, "\n".join(res), self.opts
项目:empyrion-python-api    作者:huhlig    | 项目源码 | 文件源码
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))
项目:fame_modules    作者:certsocietegenerale    | 项目源码 | 文件源码
def add_to_results(self, block, last=None):
        b = {
            '_id': block['_id'],
            'created': block['created'],
            'action': block['action'],
            'type': block['type'],
            'botnet': block['botnet'],
            'target': block['target'],
            'monitor': block['monitor'],
            'content': block['content']
        }

        if block['action'] == ACTION_UPDATE:
            b['diff'] = ''.join(ndiff(last['content'].splitlines(1), block['content'].splitlines(1)))

        self.results.append(b)
项目:ci_edit    作者:google    | 项目源码 | 文件源码
def applyDocumentUpdate(self, data):
    diff = difflib.ndiff(self.lines, self.doDataToLines(data))
    ndiff = []
    counter = 0
    for i in diff:
      if i[0] != ' ':
        if counter:
          ndiff.append(counter)
          counter = 0
        if i[0] in ['+', '-']:
          ndiff.append(i)
      else:
        counter += 1
    if counter:
      ndiff.append(counter)
    if len(ndiff) == 1 and type(ndiff[0]) is type(0):
      # Nothing was changed. The only entry is a 'skip these lines'
      self.setMessage('No matches found')
      return
    ndiff = tuple(ndiff)
    if 0:
      for i in ndiff:
        app.log.info(i)
    self.redoAddChange(('ld', ndiff))
    self.redo()
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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))
项目:python-xdis    作者:rocky    | 项目源码 | 文件源码
def do_disassembly_test(self, func, expected):
            s = 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)))
项目:python-xdis    作者:rocky    | 项目源码 | 文件源码
def do_disassembly(self, func, expected):

        co = func.__code__
        bytecode = Bytecode(co, opc)
        got = bytecode.dis()

        # 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)))
项目:GSM-scanner    作者:yosriayed    | 项目源码 | 文件源码
def _compare_eq_iterable(left, right, verbose=False):
    if not verbose:
        return [u('Use -v to get the full diff')]
    # dynamic import to speedup pytest
    import difflib

    try:
        left_formatting = pprint.pformat(left).splitlines()
        right_formatting = pprint.pformat(right).splitlines()
        explanation = [u('Full diff:')]
    except Exception:
        # hack: PrettyPrinter.pformat() in python 2 fails when formatting items that can't be sorted(), ie, calling
        # sorted() on a list would raise. See issue #718.
        # As a workaround, the full diff is generated by using the repr() string of each item of each container.
        left_formatting = sorted(repr(x) for x in left)
        right_formatting = sorted(repr(x) for x in right)
        explanation = [u('Full diff (fallback to calling repr on each item):')]
    explanation.extend(line.strip() for line in difflib.ndiff(left_formatting, right_formatting))
    return explanation
项目:anaconda-project    作者:Anaconda-Platform    | 项目源码 | 文件源码
def _pretty_diff(old_list, new_list, indent):
    diff = list(difflib.ndiff(old_list, new_list))

    # the diff has - lines, + lines, and ? lines
    # the ? lines have the ^ pointing to changed character,
    # which is just noise for these short version strings.
    # the diff also has lines with just whitespace at the
    # front, which are context.

    # remove context lines
    diff = filter(lambda x: x[0] != ' ', diff)
    # remove ? lines
    diff = filter(lambda x: x[0] != '?', diff)

    def indent_more(s):
        if s.startswith("+ "):
            return "+ " + indent + s[2:]
        elif s.startswith("- "):
            return "- " + indent + s[2:]
        else:
            return s  # pragma: no cover # should not be any other kind of lines

    diff = map(indent_more, diff)

    return list(diff)
项目:django-next-train    作者:bitpixdigital    | 项目源码 | 文件源码
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))
项目:django-next-train    作者:bitpixdigital    | 项目源码 | 文件源码
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))
项目:dcos-rabbitmq    作者:sheepkiller    | 项目源码 | 文件源码
def _apply_templating_to_file(self, filename, orig_content):
        rabbitmq_mapping = self._get_rabbitmq_mapping_for_content(orig_content)
        new_content = orig_content
        for rabbitmq_key, rabbitmq_val in rabbitmq_mapping.items():
            new_content = new_content.replace('{{%s}}' % rabbitmq_key, rabbitmq_val)
        if orig_content == new_content:
            logger.info('')
            logger.info('No templating detected in {}, leaving file as-is'.format(filename))
            return orig_content
        logger.info('')
        logger.info('Applied templating changes to {}:'.format(filename))
        logger.info('Template params used:')
        rabbitmq_keys = list(rabbitmq_mapping.keys())
        rabbitmq_keys.sort()
        for key in rabbitmq_keys:
            logger.info('  {{%s}} => %s' % (key, rabbitmq_mapping[key]))
        logger.info('Resulting diff:')
        logger.info('\n'.join(difflib.ndiff(orig_content.split('\n'), new_content.split('\n'))))
        return new_content
项目:django-wechat-api    作者:crazy-canux    | 项目源码 | 文件源码
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))
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
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))
项目:Flask_Blog    作者:sugarguo    | 项目源码 | 文件源码
def ndiffAssertEqual(self, first, second):
        """Like failUnlessEqual except use ndiff for readable output."""
        if first <> second:
            sfirst = str(first)
            ssecond = str(second)
            diff = difflib.ndiff(sfirst.splitlines(), ssecond.splitlines())
            fp = StringIO()
            print >> fp, NL, NL.join(diff)
            raise self.failureException, fp.getvalue()