Python difflib.Differ() Examples

The following are 30 code examples of difflib.Differ(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module difflib , or try the search function .
Example #1
Source File: color.py    From phpsploit with GNU General Public License v3.0 9 votes vote down vote up
def diff(old, new, display=True):
    """Nice colored diff implementation
    """
    if not isinstance(old, list):
        old = decolorize(str(old)).splitlines()
    if not isinstance(new, list):
        new = decolorize(str(new)).splitlines()

    line_types = {' ': '%Reset', '-': '%Red', '+': '%Green', '?': '%Pink'}

    if display:
        for line in difflib.Differ().compare(old, new):
            if line.startswith('?'):
                continue
            print(colorize(line_types[line[0]], line))

    return old != new 
Example #2
Source File: utils.py    From python-esppy with Apache License 2.0 7 votes vote down vote up
def assertContentsEqual(self, file1, file2):
        if os.path.isfile(file1):
            with open(file1, 'rb') as infile:
                file1 = infile.read().decode('utf-8').split('\n')
        else:
            file1 = file1.split('\n')

        if os.path.isfile(file2):
            with open(file2, 'rb') as infile:
                file2 = infile.read().decode('utf-8').split('\n')
        else:
            file1 = file1.split('\n')

        out = list(difflib.Differ().compare(file1, file2) )
        print(out)
        if out:
            for line in out:
                print(line)
            raise ValueError('Files are not equal') 
Example #3
Source File: intersection.py    From apkutils with MIT License 7 votes vote down vote up
def common(self, one, two):
        """清单内容交集,不一样的地方用*号表示。
        注:只是简单的匹配,可能不如人意。
        Args:
            one (TYPE): 第一个清单
            two (TYPE): 第二个清单

        Returns:
            TYPE: 清单交集
        """
        import difflib
        from difflib import SequenceMatcher as SM
        s = SM(None, one, two)
        r = s.ratio()
        if r == 1.0:
            return one

        d = difflib.Differ()
        sss = ''
        for item in list(d.compare(one, two)):
            if item.startswith(' '):
                sss += item[2:]
            elif not sss.endswith('*'):
                sss += '*'
        return sss 
Example #4
Source File: run.py    From starthinker with Apache License 2.0 6 votes vote down vote up
def deep_compare(actual, expected):

  if type(actual) != type(expected):
    return 'EXPECTED %s BUT ACTUAL %s' % (type(expected), type(actual))

  elif isinstance(expected, (dict, tuple, list)):
    expected_str = json.dumps(expected, indent=2, sort_keys=True)
    actual_str = json.dumps(actual, indent=2, sort_keys=True)

    delta = list(Differ().compare(
      expected_str.splitlines(),
      actual_str.splitlines()
    ))

    if sum(1 for d in delta if d[0] in ['-', '+', '?']):
      return '%s\nEXPECTED *******************************************************\n%s\nACTUAL *******************************************************\n%s' % ('\n'.join(delta), expected_str, actual_str)

  elif actual != expected:
     return 'EXPECTED %s != ACTUAL %s' % (expected, actual)

  return None


# display results of list comparison 
Example #5
Source File: Malicious-Proxy-Scanner.py    From maliciousProxyScanner with GNU General Public License v2.0 6 votes vote down vote up
def printerMalicious(self, results):
        differ = difflib.Differ()
        for result in results:
            proxy = result[1]
            try:
                html = requests.get("http://www.daviddworken.com/", proxies = {'http':'http://'+proxy}, headers = {'User-agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.69 Safari/537.36'})
                htmlNormal = requests.get("http://www.daviddworken.com/")
                htmlHash = hashlib.sha1(html.content).digest()
                htmlNormalHash = hashlib.sha1(htmlNormal.content).digest()
                if(not(htmlHash == htmlNormalHash)):
                    htmlNormalL = htmlNormal.content.splitlines()
                    htmlL = html.content.splitlines()
                    diff = differ.compare(htmlNormalL, htmlL)
                    print(bcolors.WARNING + "[-] Malicious proxy found at " + proxy + bcolors.ENDC)
                    diffOut =  '\n'.join(diff)
                    print(diffOut)
            except:
                pass 
Example #6
Source File: test_examples.py    From pyexperiment with MIT License 6 votes vote down vote up
def check_shell(self, command, expected_stdout, expected_stderr):
        """Assert output of shell command is as expected
        """
        process = subprocess.Popen(command,
                                   universal_newlines=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        process.wait()
        stdout = invariant_output(process.stdout.read())
        stderr = invariant_output(process.stderr.read())
        stdout_diff = [dl for dl in (difflib.Differ().compare(
            stdout.splitlines(),
            expected_stdout.splitlines())) if not dl[0] == ' ']
        stderr_diff = [dl for dl in (difflib.Differ().compare(
            stderr.splitlines(),
            expected_stderr.splitlines())) if not dl[0] == ' ']
        self.assertEqual(remove_whitespace(str(stdout)),
                         remove_whitespace(str(expected_stdout)),
                         stdout_diff)
        self.assertEqual(remove_whitespace(str(stderr)),
                         remove_whitespace(str(expected_stderr)),
                         stderr_diff) 
Example #7
Source File: mox.py    From personfinder with Apache License 2.0 6 votes vote down vote up
def __init__(self, unexpected_method, expected):
    """Init exception.

    Args:
      # unexpected_method: MockMethod that was called but was not at the head of
      #   the expected_method queue.
      # expected: MockMethod or UnorderedGroup the method should have
      #   been in.
      unexpected_method: MockMethod
      expected: MockMethod or UnorderedGroup
    """

    Error.__init__(self)
    if expected is None:
      self._str = "Unexpected method call %s" % (unexpected_method,)
    else:
      differ = difflib.Differ()
      diff = differ.compare(str(unexpected_method).splitlines(True),
                            str(expected).splitlines(True))
      self._str = ("Unexpected method call.  unexpected:-  expected:+\n%s"
                   % ("\n".join(diff),)) 
Example #8
Source File: dicomdiff.py    From dicom2nifti with MIT License 6 votes vote down vote up
def dicom_diff(file1, file2):
    """ Shows the fields that differ between two DICOM images.

    Inspired by https://code.google.com/p/pydicom/source/browse/source/dicom/examples/DicomDiff.py
    """

    datasets = compressed_dicom.read_file(file1), compressed_dicom.read_file(file2)

    rep = []

    for dataset in datasets:
        lines = (str(dataset.file_meta)+"\n"+str(dataset)).split('\n')
        lines = [line + '\n' for line in lines]  # add the newline to the end
        rep.append(lines)

    diff = difflib.Differ()
    for line in diff.compare(rep[0], rep[1]):
        if (line[0] == '+') or (line[0] == '-'):
            sys.stdout.write(line) 
Example #9
Source File: fileintegrity.py    From darkc0de-old-stuff with GNU General Public License v3.0 6 votes vote down vote up
def compute():
    from difflib import Differ
    filename1it = fil1.get()
    filename1it2 = fil2.get()
    filer = open(filename1it, 'rb')
    filer2 = open(filename1it2, 'rb')
    data1 = filer.read()
    data1 = data1.rstrip()
    data2 = filer2.read()
    data2 = data2.rstrip()
    d = Differ()
    result = list(d.compare(data1, data2))
    s = "\n".join(result)
    s = s.rstrip()
    textbox.insert(END, "The two files compared with Difflib are " + filename1it + " and " + filename1it2 + "\n\n")
    textbox.insert(END, s) 
Example #10
Source File: mox.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def __init__(self, unexpected_method, expected):
    """Init exception.

    Args:
      # unexpected_method: MockMethod that was called but was not at the head of
      #   the expected_method queue.
      # expected: MockMethod or UnorderedGroup the method should have
      #   been in.
      unexpected_method: MockMethod
      expected: MockMethod or UnorderedGroup
    """

    Error.__init__(self)
    if expected is None:
      self._str = "Unexpected method call %s" % (unexpected_method,)
    else:
      differ = difflib.Differ()
      diff = differ.compare(str(unexpected_method).splitlines(True),
                            str(expected).splitlines(True))
      self._str = ("Unexpected method call.  unexpected:-  expected:+\n%s"
                   % ("\n".join(diff),)) 
Example #11
Source File: diff.py    From pytest-clarity with MIT License 6 votes vote down vote up
def build_unified_diff(lhs_repr, rhs_repr):
    differ = difflib.Differ()
    lines_lhs, lines_rhs = lhs_repr.splitlines(), rhs_repr.splitlines()
    diff = differ.compare(lines_lhs, lines_rhs)

    output = []
    for line in diff:
        # Differ instructs us how to transform left into right, but we want
        # our colours to indicate how to transform right into left
        if line.startswith("- "):
            output.append(inserted_text(" L " + line[2:]))
        elif line.startswith("+ "):
            output.append(deleted_text(" R " + line[2:]))
        elif line.startswith("? "):
            # We can use this to find the index of change in the
            # line above if required in the future
            pass
        else:
            output.append(non_formatted(line))

    return output 
Example #12
Source File: differ.py    From pySINDy with MIT License 5 votes vote down vote up
def __init__(self, source, proposal):
        proposal = htmlescape(proposal)

        differ = Differ()
        self.diff = list(differ.compare(source.splitlines(1),
                                        proposal.splitlines(1))) 
Example #13
Source File: netbase.py    From deep-prior with GNU General Public License v3.0 5 votes vote down vote up
def load(self, filename):
        """
        Load the parameters for this network from disk.
        :param filename: Load the parameters of this network from a pickle file at the named path. If this name ends in
               ".gz" then the input will automatically be gunzipped; otherwise the input will be treated as a "raw" pickle.
        :return: None
        """

        opener = gzip.open if filename.lower().endswith('.gz') else open
        handle = opener(filename, 'rb')
        saved = cPickle.load(handle)
        handle.close()
        if saved['network'] != self.__str__():
            print "Possibly not matching network configuration!"
            differences = list(difflib.Differ().compare(saved['network'].splitlines(), self.__str__().splitlines()))
            print "Differences are:"
            print "\n".join(differences)
        for layer in self.layers:
            if len(layer.params) != len(saved['{}-values'.format(layer.layerNum)]):
                print "Warning: Layer parameters for layer {} do not match. Trying to fit on shape!".format(layer.layerNum)
                n_assigned = 0
                for p in layer.params:
                    for v in saved['{}-values'.format(layer.layerNum)]:
                        if p.get_value().shape == v.shape:
                            p.set_value(v)
                            n_assigned += 1

                if n_assigned != len(layer.params):
                    raise ImportError("Could not load all necessary variables!")
                else:
                    print "Found fitting parameters!"
            else:
                prms = layer.params
                for p, v in zip(prms, saved['{}-values'.format(layer.layerNum)]):
                    if p.get_value().shape == v.shape:
                        p.set_value(v)
                    else:
                        print "WARNING: Skipping parameter for {}! Shape {} does not fit {}.".format(p.name, p.get_value().shape, v.shape)
        print 'Loaded model parameters from {}'.format(filename) 
Example #14
Source File: test_difflib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_added_tab_hint(self):
        # Check fix for bug #1488943
        diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
        self.assertEqual("- \tI am a buggy", diff[0])
        self.assertEqual("?            --\n", diff[1])
        self.assertEqual("+ \t\tI am a bug", diff[2])
        self.assertEqual("? +\n", diff[3]) 
Example #15
Source File: code.py    From lpdec with GNU General Public License v3.0 5 votes vote down vote up
def compareCode(args):
    """Compare two codes by means of their parity-check matrix. Different matrices result in a
    diff-like output."""
    other = BinaryLinearBlockCode(parityCheckMatrix=args.other)
    if np.all(args.code.parityCheckMatrix == other.parityCheckMatrix):
        print('codes have the same parity-check matrix')
    else:
        print('codes do not have the same parity-check matrix')
        import difflib
        d = difflib.Differ()
        result = d.compare(matrices.formatMatrix(args.code.parityCheckMatrix),
                           matrices.formatMatrix(other.parityCheckMatrix))
        print('\n'.join(result)) 
Example #16
Source File: blackdiff.py    From scanpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def black_diff(src: Path, differ=Differ()) -> Tuple[int, int]:
    src_contents = src.read_text()
    dst_contents = black.format_str(src_contents, mode=mode)
    if src_contents == dst_contents:
        return 0, 0

    counts = Counter(
        line[0]
        for line in differ.compare(src_contents.splitlines(), dst_contents.splitlines())
    )
    return counts['+'], counts['-'] 
Example #17
Source File: delta.py    From ksconf with Apache License 2.0 5 votes vote down vote up
def show_text_diff(stream, a, b):
    _show_diff_header(stream, (a, b), "--text")
    differ = difflib.Differ()
    lines_a = open(a, "r", encoding=default_encoding).readlines()
    lines_b = open(b, "r", encoding=default_encoding).readlines()
    with TermColor(stream) as tc:
        for d in differ.compare(lines_a, lines_b):
            # Someday add "?" highlighting.  Trick is this should change color mid-line on the
            # previous (one or two) lines.  (Google and see if somebody else solved this one already)
            # https://stackoverflow.com/questions/774316/python-difflib-highlighting-differences-inline
            tc.color(_diff_color_mapping.get(d[0], 0))
            stream.write(d)
            tc.reset() 
Example #18
Source File: test_difflib.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_added_tab_hint(self):
        # Check fix for bug #1488943
        diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
        self.assertEqual("- \tI am a buggy", diff[0])
        self.assertEqual("?            --\n", diff[1])
        self.assertEqual("+ \t\tI am a bug", diff[2])
        self.assertEqual("? +\n", diff[3]) 
Example #19
Source File: pycore.py    From blackmamba with MIT License 5 votes vote down vote up
def _set_diffs(self):
        differ = difflib.Differ()
        self.lines = []
        lineno = 0
        for line in differ.compare(self.old.splitlines(True),
                                   self.new.splitlines(True)):
            if line.startswith(' '):
                lineno += 1
            elif line.startswith('-'):
                lineno += 1
                self.lines.append(lineno) 
Example #20
Source File: testutil.py    From runway with Apache License 2.0 5 votes vote down vote up
def diff(first, second):
    """Human readable differ."""
    return '\n'.join(
        list(
            difflib.Differ().compare(
                first.splitlines(),
                second.splitlines()
            )
        )
    ) 
Example #21
Source File: string_Utils.py    From warriorframework with Apache License 2.0 5 votes vote down vote up
def text_compare(text1, text2, output_file):
    """
        Compares two strings and if they match returns True
        else writes the difference to the output_file.
    """
    if text1 == text2:
        return True
    diff = list(difflib.Differ().compare(text1.split(), text2.split()))
    te = open(output_file, 'w')
    for line in diff:
        te.write(line+"\n")
    te.close()
    return False 
Example #22
Source File: test_difflib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_added_tab_hint(self):
        # Check fix for bug #1488943
        diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
        self.assertEqual("- \tI am a buggy", diff[0])
        self.assertEqual("?            --\n", diff[1])
        self.assertEqual("+ \t\tI am a bug", diff[2])
        self.assertEqual("? +\n", diff[3]) 
Example #23
Source File: fortios.py    From pyfg with Apache License 2.0 5 votes vote down vote up
def compare_config(self, other=None, text=False):
        """
        Compares running config with another config. This other config can be either the *running*
        config or a :class:`~pyFG.forticonfig.FortiConfig`. The result of the comparison will be how to reach\
        the state represented in the target config (either the *candidate* or *other*) from the *running*\
        config.

        Args:
            * **other** (:class:`~pyFG.forticonfig.FortiConfig`) -- This parameter, if specified, will be used for the\
                comparison. If it is not specified the candidate config will be used.
            * **text** (bool):
                * If ``True`` this method will return a text diff showing how to get from the running config to\
                    the target config.
                * If ``False`` this method will return all the exact commands that needs to be run on the running\
                    config to reach the target config.

        Returns:
            See the explanation of the *text* arg in the section Args.

        """
        if other is None:
            other = self.candidate_config

        if not text:
            return self.running_config.compare_config(other)
        else:
            diff = Differ()
            result = diff.compare(
                self.running_config.to_text().splitlines(),
                other.to_text().splitlines()
            )
            return '\n'.join(result) 
Example #24
Source File: test_difflib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_added_tab_hint(self):
        # Check fix for bug #1488943
        diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
        self.assertEqual("- \tI am a buggy", diff[0])
        self.assertEqual("?            --\n", diff[1])
        self.assertEqual("+ \t\tI am a bug", diff[2])
        self.assertEqual("? +\n", diff[3]) 
Example #25
Source File: test_emit.py    From quark with Apache License 2.0 5 votes vote down vote up
def run_tests(base, dirs, command, env=None):
    failed_expectations = []
    for name in dirs:
        if has_main(name):
            try:
                cmd = command(name)
                cwd = os.path.join(base, name)
                print "cd %s && %s" % (cwd, " ".join(cmd))
                actual = subprocess.check_output(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                actual = e.output
                print(actual)
            expected = get_expected(name)
            if expected != actual:
                open(get_out(name) + ".cmp", "w").write(actual)
                failed_expectations.append(name)
                if expected is None:
                    print("FAILURE: Expected output not found for %r." % name)
                else:
                    d = difflib.Differ()
                    delta = list(d.compare(filter_builtin(expected),
                                           filter_builtin(actual)))
                    print("FAILURE: Expected and actual output dont match for '%s':\n%s" % (
                        name, "".join(delta)))
    print(failed_expectations)
    assert not failed_expectations 
Example #26
Source File: testutil.py    From stacker with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def diff(a, b):
    """A human readable differ."""
    return '\n'.join(
        list(
            difflib.Differ().compare(
                a.splitlines(),
                b.splitlines()
            )
        )
    ) 
Example #27
Source File: test_difflib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_added_tab_hint(self):
        # Check fix for bug #1488943
        diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
        self.assertEqual("- \tI am a buggy", diff[0])
        self.assertEqual("?            --\n", diff[1])
        self.assertEqual("+ \t\tI am a bug", diff[2])
        self.assertEqual("? +\n", diff[3]) 
Example #28
Source File: fuctup_comments.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def compute():
    #open and read files compare using Lib Diff output to Tk
    textbox.insert(END, "reading files please be patient... This may take a moment\n\n")
    from difflib import Differ
    filename1it = fil1.get()
    filename1it2 = fil2.get()
    filer = open(filename1it, 'rb')
    filer2 = open(filename1it2, 'rb')
    
    data1 = filer.read()
    data2 = filer2.read()
    d = Differ()
    result = list(d.compare(data1, data2))
    textbox.insert(END, "The two files compared with Difflib are " + filename1it + " and " + filename1it2 + "\n\n")
    textbox.insert(END, result) 
Example #29
Source File: diff.py    From ward with MIT License 5 votes vote down vote up
def raw_unified_diff(lhs_repr: str, rhs_repr: str) -> Generator[str, None, None]:
    differ = difflib.Differ()
    lines_lhs = lhs_repr.splitlines()
    lines_rhs = rhs_repr.splitlines()
    return differ.compare(lines_lhs, lines_rhs) 
Example #30
Source File: conf.py    From faucet with Apache License 2.0 5 votes vote down vote up
def conf_diff(self, other):
        """Return text diff between two Confs."""
        differ = difflib.Differ()
        return '\n'.join(differ.compare(
            self.to_conf().splitlines(), other.to_conf().splitlines()))