Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_change_set_order(self):
first = set(["changeA", "changeC", "changeB"])
second = set(["changeB", "changeC", "changeA"])
diffed = list(diff(first, second))
# There should be zero reported diffs
assert len(diffed) == 0
def test_expand_addition(self):
first = {}
second = {'foo': 'bar', 'apple': 'banana'}
diffed = list(diff(first, second, expand=True))
res = [('add', '', [('foo', 'bar')]),
('add', '', [('apple', 'banana')])]
assert len(diffed) == 2
for patch in res:
assert patch in diffed
def test_ignore_with_ignorecase(self):
class IgnoreCase(set):
def __contains__(self, key):
return set.__contains__(self, str(key).lower())
assert list(diff({'a': 1, 'b': 2}, {'A': 3, 'b': 4},
ignore=IgnoreCase('a'))) == [('change', 'b', (2, 4))]
def test_nodes(self):
first = {'a': {'b': {'c': 'd'}}}
second = {'a': {'b': {'c': 'd', 'e': 'f'}}}
diffed = next(diff(first, second))
assert ('add', 'a.b', [('e', 'f')]) == diffed
def jsondiff(j1, j2, filterMode=None, withoutKey=None, filterKey=None):
diffs = diff(j1, j2)
tmp = []
for mode, selector, changes in diffs:
# parse selector to list
if isinstance(selector, basestring):
selector = selector.split('.')
# filter
if not filterMode or mode == filterMode:
if not withoutKey or withoutKey not in selector:
if not filterKey or filterKey in selector:
tmp.append((mode, selector, changes))
return tmp
def repair_record_metadata(uuid):
"""Repair the record's metadata using a reference revision."""
rec = Record.get_record(uuid)
good_revision = recent_non_corrupted_revision(rec)
if '_internal' in good_revision:
rec['_internal'] = good_revision['_internal']
files_diff = list(diff(rec['_files'], good_revision['_files']))
if files_diff_safe(files_diff):
rec['_files'] = good_revision['_files']
rec.commit()
db.session.commit()
RecordIndexer().bulk_index([str(rec.id), ])
def get_embed_dict_differences(embed1: dict, embed2: dict) -> list:
"""Finds any differences between 2 embed dicts, returning the names of the keys /
fields that are different. Does not find additions or deletions, just changes.
Args:
embed1: A dict of a discord Embed object.
embed2: A dict of a discord Embed object.
Returns:
A List of strings that are the names of the changed keys / fields.
"""
changes = []
for difference in list(diff(embed1, embed2)):
# The first index is the type of diff. We are looking for changes.
if difference[0] == "change":
if difference[1] == "image.url":
# Ignore image url changes.
continue
# The second index ([1]) is the key, or in the case of fields, it is a list
# like: ['fields', 0, 'value'].
# Here we check it is a fields value that has changed, otherwise ignore it.
if (
isinstance(difference[1], list)
and difference[1][0] == "fields"
and difference[1][2] == "value"
):
# diff[1][1] is the fields index in the embed dict.
def extract_record_events(recid, record, old_record=None):
"""Extract record events from a record."""
events = {}
# Exit early if there are no changes
if old_record:
record_changes = diff(record, old_record, ignore=IGNORED_RECORD_KEYS)
if not list(record_changes):
return events
event_type = 'object_{}'.format('updated' if old_record else 'created')
events = {
event_type: [
{
'object_publication_date': record.get('publication_date'),
'object_provider': {'name': 'Zenodo'},
'object': format_source_object(record),
'metadata': {
'title': record.get('title'),
'creators': record.get('creators'),
},
'metadata_schema': 'Zenodo',
'metadata_schema_url':
def check_rules(self) -> YieldFlake8Error:
"""Check missing sections and missing key/value pairs in setup.cfg."""
setup_cfg = ConfigParser()
with self.file_path.open() as handle:
setup_cfg.read_file(handle)
actual_sections = set(setup_cfg.sections())
missing = self.get_missing_output(actual_sections)
if missing:
yield self.flake8_error(1, " has some missing sections. Use this:", missing)
for section in self.expected_sections - self.missing_sections:
expected_dict = self.file_dict[section]
actual_dict = dict(setup_cfg[section])
# TODO: add a class Ini(BaseFormat) and move this dictdiffer code there
for diff_type, key, values in dictdiffer.diff(actual_dict, expected_dict):
if diff_type == dictdiffer.CHANGE:
yield from self.compare_different_keys(section, key, values[0], values[1])
elif diff_type == dictdiffer.ADD:
yield from self.show_missing_keys(section, key, values)
def character_diff(self, old, new):
"""
Diff two character dicts; return a human-readable representation
:param old: old (cache) character dict
:type old: dict
:param new: new (battlenet) character dict
:type new: dict
:rtype: string
"""
d = diff(old, new)
s = ''
for x in list(d):
if x[0] == 'change':
s += 'change {item} from {a} to {b}\n'.format(typ=x[0],
item=x[1],
a=x[2][0],
b=x[2][1])
elif x[0] == 'remove':
s += 'remove {a} {b}\n'.format(a=x[1], b=x[2])
else:
s += 'add {a} {b}\n'.format(a=x[1], b=x[2])
s = s.strip()
return s