Compare the two notebooks, and raise with a meaningful message
    that explains the differences, if any
497217
def compare_notebooks(notebook_expected,
                      notebook_actual,
                      fmt=None,
                      allow_expected_differences=True,
                      raise_on_first_difference=True,
                      compare_outputs=False):
    """"""
    fmt = long_form_one_format(fmt)
    format_name = fmt.get('format_name')

    # Expected differences
    allow_filtered_cell_metadata = allow_expected_differences
    allow_missing_code_cell_metadata = allow_expected_differences and format_name == 'sphinx'
    allow_missing_markdown_cell_metadata = allow_expected_differences and format_name in ['sphinx', 'spin']
    allow_removed_final_blank_line = allow_expected_differences

    cell_metadata_filter = notebook_actual.get('jupytext', {}).get('cell_metadata_filter')

    if format_name == 'sphinx' and notebook_actual.cells and notebook_actual.cells[0].source == '%matplotlib inline':
        notebook_actual.cells = notebook_actual.cells[1:]

    # Compare cells type and content
    test_cell_iter = iter(notebook_actual.cells)
    modified_cells = set()
    modified_cell_metadata = set()
    for i, ref_cell in enumerate(notebook_expected.cells, 1):
        try:
            test_cell = next(test_cell_iter)
        except StopIteration:
            if raise_on_first_difference:
                raise NotebookDifference('No cell corresponding to {} cell #{}:\n{}'
                                         .format(ref_cell.cell_type, i, ref_cell.source))
            modified_cells.update(range(i, len(notebook_expected.cells) + 1))
            break

        ref_lines = [line for line in ref_cell.source.splitlines() if not _BLANK_LINE.match(line)]
        test_lines = []

        # 1. test cell type
        if ref_cell.cell_type != test_cell.cell_type:
            if raise_on_first_difference:
                raise NotebookDifference("Unexpected cell type '{}' for {} cell #{}:\n{}"
                                         .format(test_cell.cell_type, ref_cell.cell_type, i, ref_cell.source))
            modified_cells.add(i)

        # 2. test cell metadata
        if (ref_cell.cell_type == 'code' and not allow_missing_code_cell_metadata) or \
                (ref_cell.cell_type != 'code' and not allow_missing_markdown_cell_metadata):

            if allow_filtered_cell_metadata:
                ref_cell.metadata = {key: ref_cell.metadata[key] for key in ref_cell.metadata
                                     if key not in _IGNORE_CELL_METADATA}
                test_cell.metadata = {key: test_cell.metadata[key] for key in test_cell.metadata
                                      if key not in _IGNORE_CELL_METADATA}

            if ref_cell.metadata != test_cell.metadata:
                if raise_on_first_difference:
                    try:
                        compare(ref_cell.metadata, test_cell.metadata)
                    except AssertionError as error:
                        raise NotebookDifference("Metadata differ on {} cell #{}: {}\nCell content:\n{}"
                                                 .format(test_cell.cell_type, i, str(error), ref_cell.source))
                else:
                    modified_cell_metadata.update(set(test_cell.metadata).difference(ref_cell.metadata))
                    modified_cell_metadata.update(set(ref_cell.metadata).difference(test_cell.metadata))
                    for key in set(ref_cell.metadata).intersection(test_cell.metadata):
                        if ref_cell.metadata[key] != test_cell.metadata[key]:
                            modified_cell_metadata.add(key)

        test_lines.extend([line for line in test_cell.source.splitlines() if not _BLANK_LINE.match(line)])

        # 3. test cell content
        if ref_lines != test_lines:
            if raise_on_first_difference:
                try:
                    compare('\n'.join(ref_lines), '\n'.join(test_lines))
                except AssertionError as error:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
            else:
                modified_cells.add(i)

        # 3. bis test entire cell content
        if not same_content(ref_cell.source, test_cell.source, allow_removed_final_blank_line):
            try:
                compare(ref_cell.source, test_cell.source)
            except AssertionError as error:
                if raise_on_first_difference:
                    raise NotebookDifference("Cell content differ on {} cell #{}: {}"
                                             .format(test_cell.cell_type, i, str(error)))
                modified_cells.add(i)

        if not compare_outputs:
            continue

        if ref_cell.cell_type != 'code':
            continue

        ref_cell = filtered_cell(ref_cell,
                                 preserve_outputs=compare_outputs,
                                 cell_metadata_filter=cell_metadata_filter)
        test_cell = filtered_cell(test_cell,
                                  preserve_outputs=compare_outputs,
                                  cell_metadata_filter=cell_metadata_filter)

        try:
            compare(ref_cell, test_cell)
        except AssertionError as error:
            if raise_on_first_difference:
                raise NotebookDifference("Cell outputs differ on {} cell #{}: {}"
                                         .format(test_cell['cell_type'], i, str(error)))
            modified_cells.add(i)

    # More cells in the actual notebook?
    remaining_cell_count = 0
    while True:
        try:
            test_cell = next(test_cell_iter)
            if raise_on_first_difference:
                raise NotebookDifference('Additional {} cell: {}'.format(test_cell.cell_type, test_cell.source))
            remaining_cell_count += 1
        except StopIteration:
            break

    if remaining_cell_count and not raise_on_first_difference:
        modified_cells.update(
            range(len(notebook_expected.cells) + 1, len(notebook_expected.cells) + 1 + remaining_cell_count))

    # Compare notebook metadata
    modified_metadata = False
    try:
        compare(filtered_notebook_metadata(notebook_expected),
                filtered_notebook_metadata(notebook_actual))
    except AssertionError as error:
        if raise_on_first_difference:
            raise NotebookDifference("Notebook metadata differ: {}".format(str(error)))
        modified_metadata = True

    error = []
    if modified_cells:
        error.append('Cells {} differ ({}/{})'.format(','.join([str(i) for i in modified_cells]),
                                                      len(modified_cells), len(notebook_expected.cells)))
    if modified_cell_metadata:
        error.append("Cell metadata '{}' differ".format("', '".join([str(i) for i in modified_cell_metadata])))
    if modified_metadata:
        error.append('Notebook metadata differ')

    if error:
        raise NotebookDifference(' | '.join(error))
523689
def compare_two_documents(kls, doc1, doc2):
        """"""
        first = doc1
        if isinstance(doc1, string_types):
            try:
                first = json.loads(doc1)
            except (ValueError, TypeError) as error:
                log.warning("Failed to convert doc into a json object\terror=%s", error)
                yield error.args[0]
                return

        second = doc2
        if isinstance(doc2, string_types):
            try:
                second = json.loads(doc2)
            except (ValueError, TypeError) as error:
                log.warning("Failed to convert doc into a json object\terror=%s", error)
                yield error.args[0]
                return

        # Ordering the principals because the ordering amazon gives me hates me
        def sort_statement(statement):
            for principal in (statement.get("Principal", None), statement.get("NotPrincipal", None)):
                if principal:
                    for principal_type in ("AWS", "Federated", "Service"):
                        if principal_type in principal and type(principal[principal_type]) is list:
                            principal[principal_type] = sorted(principal[principal_type])
        def sort_key(statement, key):
            if key in statement and type(statement[key]) is list:
                statement[key] = sorted(statement[key])
        for document in (first, second):
            if "Statement" in document:
                if type(document["Statement"]) is dict:
                    sort_statement(document["Statement"])
                    sort_key(document["Statement"], "Action")
                    sort_key(document["Statement"], "NotAction")
                    sort_key(document["Statement"], "Resource")
                    sort_key(document["Statement"], "NotResource")
                else:
                    for statement in document["Statement"]:
                        sort_statement(statement)
                        sort_key(statement, "Action")
                        sort_key(statement, "NotAction")
                        sort_key(statement, "Resource")
                        sort_key(statement, "NotResource")

        difference = diff(first, second, fromfile="current", tofile="new").stringify()
        if difference:
            lines = difference.split('\n')
            if not first or not second or first != second:
                for line in lines:
                    yield line
1066829
def main():
    
    print("Generating research notes...")
    if os.path.exists(fname):
       os.remove(fname)
    append_rst('================================================\n')
    append_rst('Comparison of Information Aggregation Techniques\n')
    append_rst('================================================\n\n')
    append_rst('.. contents::\n\n')
       
    # import header
    append_rst(open('res_core_data_HEADER.rst', 'r').read())
    append_rst(res_core_data_mthd1.get_method())
    append_rst(res_core_data_mthd2.get_method())
    
    # call programs
    append_rst('Results\n')
    append_rst('=====================================\n')
    for dat in data_files:
        append_rst('\nData File : ' + dat + '\n---------------------------------------\n\n')
        res_core_data_mthd1.get_results(fname, dat)
        res_core_data_mthd2.get_results(fname, dat)
    
    
    # import footer
    append_rst(open('res_core_data_FOOTER.rst', 'r').read())    
    print("Done!")
950870
function compare(start, end, max) {
    var diff = end.line - start.line
    var lines = Math.abs(diff) - max
    var reason

    if (lines > 0) {
      reason =
        'Remove ' +
        lines +
        ' ' +
        plural('line', lines) +
        ' ' +
        (diff > 0 ? 'before' : 'after') +
        ' node'

      file.message(reason, end)
    }
  }
483751
def _compute_diff(existing, expected):

    '''
    
    '''

    diff = {}

    for key in ['location', 'contact', 'chassis_id']:
        if existing.get(key) != expected.get(key):
            _create_diff(diff,
                         _valid_str,
                         key,
                         existing.get(key),
                         expected.get(key))

    for key in ['community']:  # for the moment only onen
        if existing.get(key) != expected.get(key):
            _create_diff(diff,
                         _valid_dict,
                         key,
                         existing.get(key),
                         expected.get(key))

    return diff
1271485
def compare(self, other, components=[]):
        
        lg.debug("Comparing publish %s (%s) and %s (%s)" % (self.name, self.storage or "local", other.name, other.storage or "local"))

        diff, equal = ({}, {})

        for component, snapshots in self.components.items():
            if component not in list(other.components.keys()):
                # Component is missing in other
                diff[component] = snapshots
                continue

            equal_snapshots = list(set(snapshots).intersection(other.components[component]))
            if equal_snapshots:
                lg.debug("Equal snapshots for %s: %s" % (component, equal_snapshots))
                equal[component] = equal_snapshots

            diff_snapshots = list(set(snapshots).difference(other.components[component]))
            if diff_snapshots:
                lg.debug("Different snapshots for %s: %s" % (component, diff_snapshots))
                diff[component] = diff_snapshots

        return (diff, equal)
678378
def _disks_equal(disk1, disk2):
    '''
    
    '''
    target1 = disk1.find('target')
    target2 = disk2.find('target')
    source1 = ElementTree.tostring(disk1.find('source')) if disk1.find('source') is not None else None
    source2 = ElementTree.tostring(disk2.find('source')) if disk2.find('source') is not None else None

    return source1 == source2 and \
        target1 is not None and target2 is not None and \
        target1.get('bus') == target2.get('bus') and \
        disk1.get('device', 'disk') == disk2.get('device', 'disk') and \
        target1.get('dev') == target2.get('dev')
400522
def construct_difference(self, parent, excluded, name=None, fp=None):
        """"""
        assert parent in self.locations, "Can't find location {}".format(parent)
        for location in excluded:
            assert location in self.locations, "Can't find location {}".format(location)
        included = set(self.data[parent]).difference(
            reduce(set.union, [set(self.data[loc]) for loc in excluded])
        )
        geom = _union(included)
        if fp:
            self.write_geoms_to_file(fp, [geom], [name] if name else None)
            return fp
        else:
            return geom
1146128
def compare_disks(disk0, disk1, configuration):
    """"""
    with DiskComparator(disk0, disk1) as comparator:
        results = comparator.compare(
            size=configuration.get('get_file_size', False),
            identify=configuration.get('identify_files', False),
            concurrent=configuration.get('use_concurrency', False))

        if configuration.get('extract_files', False):
            extract = results['created_files'] + results['modified_files']
            files = comparator.extract(1, extract,
                                       path=configuration['results_folder'])

            results.update(files)

        if configuration.get('compare_registries', False):
            results['registry'] = comparator.compare_registry(
                concurrent=configuration.get('use_concurrency', False))

    return results
978806
def compare(ver1, ver2):
    ''''''
    if ver1 == ver2:
        return 0
    ver1 = _cut(ver1)
    ver2 = _cut(ver2)
    # magic multiplier
    m = 1
    # if the first one is shorter, replace them
    if len(ver1) < len(ver2):
        ver1, ver2 = ver2, ver1
        # and reverse magic multiplier
        m = -1
    # compare all items that both have
    for i, part in enumerate(ver2):
        if ver1[i] > ver2[i]:
            return m * 1
        if ver1[i] < ver2[i]:
            return m * -1
    # if the first "extra" item is not negative, it's bigger
    if ver1[len(ver2)] >= 0:
        return m * 1
    else:
        return m * -1
1110267
function notEqual(actual, expected, message) {
    if (actual != expected) {
      this.pass(message);
    }
    else {
      this.fail({
        actual: actual,
        expected: expected,
        message: message,
        operator: "!=",
      });
    }
  }
817603
def compare_version(version1, version2):
    
    def normalize(v):
        return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]
    return (normalize(version1) > normalize(version2))-(normalize(version1) < normalize(version2))
169664
function compare(v1, v2) {
  // Omit the starting `v`.
  const rawv1 = omitv(v1)
  const rawv2 = omitv(v2)
  let v1data, v2data
  try {
    v1data = numberedVersionData(rawv1)
    v2data = numberedVersionData(rawv2)
  } catch (e) {
    return asciiVersionCompare(rawv1, rawv2)
  }

  // Compare the numbered part (eg, 1.0.0 < 2.0.0).
  const numbersCompare = listCompare(v1data.numbers, v2data.numbers)
  if (numbersCompare !== 0) {
    return numbersCompare
  }

  // Compare the modifiers (eg, alpha < beta).
  if (v1data.modifier < v2data.modifier) {
    return -1
  } else if (v1data.modifier > v2data.modifier) {
    return 1
  }

  // Compare the modifier counts (eg, alpha1 < alpha3).
  if (v1data.modifierCount < v2data.modifierCount) {
    return -1
  } else if (v1data.modifierCount > v2data.modifierCount) {
    return 1
  }

  return 0
}
611347
def comparison_callback(sender, instance, **kwargs):
    """"""
    if validate_instance(instance) and settings.AUTOMATED_LOGGING['to_database']:
        try:
            old = sender.objects.get(pk=instance.pk)
        except Exception:
            return None

        try:
            mdl = ContentType.objects.get_for_model(instance)
            cur, ins = old.__dict__, instance.__dict__
            old, new = {}, {}
            for k in cur.keys():
                # _ fields are not real model fields, only state or cache fields
                # getting filtered
                if re.match('(_)(.*?)', k):
                    continue

                changed = False
                if k in ins.keys():
                    if cur[k] != ins[k]:
                        changed = True
                        new[k] = ModelObject()
                        new[k].value = str(ins[k])
                        new[k].save()

                        try:
                            new[k].type = ContentType.objects.get_for_model(ins[k])
                        except Exception:
                            logger = logging.getLogger(__name__)
                            logger.debug('Could not dermin the content type of the field')

                        new[k].field = Field.objects.get_or_create(name=k, model=mdl)[0]
                        new[k].save()
                else:
                    changed = True

                if changed:
                    old[k] = ModelObject()
                    old[k].value = str(cur[k])
                    old[k].save()

                    try:
                        old[k].type = ContentType.objects.get_for_model(cur[k])
                    except Exception:
                        logger = logging.getLogger(__name__)
                        logger.debug('Could not dermin the content type of the field')

                    old[k].field = Field.objects.get_or_create(name=k, model=mdl)[0]
                    old[k].save()

            if old or new:
                changelog = ModelChangelog()
                changelog.save()

                changelog.modification = ModelModification()
                changelog.modification.save()
                changelog.modification.previously.add(*old.values())
                changelog.modification.currently.add(*new.values())

                changelog.information = ModelObject()
                changelog.information.save()
                changelog.information.value = repr(instance)
                changelog.information.type = ContentType.objects.get_for_model(instance)
                changelog.information.save()
                changelog.save()

                instance.al_chl = changelog

                return instance

        except Exception as e:
            print(e)
            logger = logging.getLogger(__name__)
            logger.warning('automated_logging recorded an exception that should not have happended')
1316605
def compare_name_component(list1, list2, settings, use_ratio=False):
    
    if not list1[0] or not list2[0]:
        not_required = not settings['required']
        return not_required * 100 if use_ratio else not_required

    if len(list1) != len(list2):
        return False

    compare_func = _ratio_compare if use_ratio else _normal_compare
    return compare_func(list1, list2, settings)
12301
def __changed_bytes(self, other):
        
        if self._page_size != other._page_size:
            raise SimMemoryError("SimPagedMemory page sizes differ. This is asking for disaster.")

        our_pages = set(self._pages.keys())
        their_pages = set(other._pages.keys())
        their_additions = their_pages - our_pages
        our_additions = our_pages - their_pages
        common_pages = our_pages & their_pages

        candidates = set()
        for p in their_additions:
            candidates.update(other._pages[p].keys())
        for p in our_additions:
            candidates.update(self._pages[p].keys())

        for p in common_pages:
            our_page = self._pages[p]
            their_page = other._pages[p]

            if our_page is their_page:
                continue

            our_keys = set(our_page.keys())
            their_keys = set(their_page.keys())
            changes = (our_keys - their_keys) | (their_keys - our_keys) | {
                i for i in (our_keys & their_keys) if our_page.load_mo(self.state, i) is not their_page.load_mo(self.state, i)
            }
            candidates.update(changes)

        #both_changed = our_changes & their_changes
        #ours_changed_only = our_changes - both_changed
        #theirs_changed_only = their_changes - both_changed
        #both_deleted = their_deletions & our_deletions
        #ours_deleted_only = our_deletions - both_deleted
        #theirs_deleted_only = their_deletions - both_deleted

        differences = set()
        for c in candidates:
            if c not in self and c in other:
                differences.add(c)
            elif c in self and c not in other:
                differences.add(c)
            else:
                if type(self[c]) is not SimMemoryObject:
                    self[c] = SimMemoryObject(self.state.solver.BVV(ord(self[c]), self.byte_width), c, byte_width=self.byte_width)
                if type(other[c]) is not SimMemoryObject:
                    other[c] = SimMemoryObject(self.state.solver.BVV(ord(other[c]), self.byte_width), c, byte_width=self.byte_width)
                if c in self and self[c] != other[c]:
                    # Try to see if the bytes are equal
                    self_byte = self[c].bytes_at(c, 1)
                    other_byte = other[c].bytes_at(c, 1)
                    if self_byte is not other_byte:
                        #l.debug("%s: offset %x, two different bytes %s %s from %s %s", self.id, c,
                        #        self_byte, other_byte,
                        #        self[c].object.model, other[c].object.model)
                        differences.add(c)
                else:
                    # this means the byte is in neither memory
                    pass

        return differences
779971
def diff_texts(a, b, filename):
    """"""
    a = a.splitlines()
    b = b.splitlines()
    return difflib.unified_diff(a, b, filename, filename,
                                "(original)", "(refactored)",
                                lineterm="")