Python dictdiffer.diff() Examples
The following are 16
code examples of dictdiffer.diff().
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
dictdiffer
, or try the search function
.
Example #1
Source File: base_test_migration.py From GelReportModels with Apache License 2.0 | 6 votes |
def diff_round_tripped(self, original, round_tripped, ignore_fields=[]): differ = False for diff_type, field_path, values in list( dictdiffer.diff(round_tripped.toJsonDict(), original.toJsonDict())): if type(field_path).__name__ in ['unicode', 'str']: field_path = [field_path] if self.is_field_ignored(field_path, ignore_fields): continue expected = values[1] if len(values) > 1 else None observed = values[0] if observed in self._empty_values and expected in self._empty_values: continue if self.is_hashable(expected) and self._equal_values.get(expected, "not the same") == observed: continue if expected == observed: continue logging.error("{}: {} expected '{}' found '{}'".format(diff_type, ".".join( list(map(str, field_path))), expected, observed)) differ = True return differ
Example #2
Source File: utils.py From opencraft with GNU Affero General Public License v3.0 | 6 votes |
def build_instance_config_diff(instance_config: 'BetaTestApplication'): """ Builds an configuration diff for the provided instance configuration. """ instance = instance_config.instance if not instance: instance = object() original_config = {} new_config = {} for attr in ('instance_name', 'privacy_policy_url', 'public_contact_email'): original_config[attr] = getattr(instance, attr, None) new_config[attr] = getattr(instance_config, attr, None) original_config['theme_config'] = getattr(instance, 'theme_config', None) or {} new_config['theme_config'] = instance_config.draft_theme_config or {} original_config['static_content_overrides'] = getattr(instance, 'static_content_overrides', None) or {} new_config['static_content_overrides'] = instance_config.draft_static_content_overrides or {} return list(diff(original_config, new_config))
Example #3
Source File: api_views.py From esdc-ce with Apache License 2.0 | 6 votes |
def _diff_dicts(active, current, ignore=('changed',)): res = {} for t, key, value in diff(active, current, ignore=ignore): if t not in res: res[t] = {} if t == 'change': if isinstance(key, list): # list value has changed key = key[0] value = (active[key], current[key]) res[t][key] = value else: # add, remove if key: # list value has changed if 'change' not in res: res['change'] = {} res['change'][key] = (active[key], current[key]) else: for k, v in value: res[t][k] = v return res
Example #4
Source File: api_views.py From esdc-ce with Apache License 2.0 | 6 votes |
def _diff_lists(active, current, ignore=()): res = {} for t, key, value in diff(active, current, ignore=ignore): if t not in res: res[t] = {} if t == 'change': list_id, attr = key list_id += 1 if list_id not in res[t]: res[t][list_id] = {} res[t][list_id][attr] = value else: # add, remove for list_id, full_value in value: res[t][list_id + 1] = full_value return res
Example #5
Source File: notifications.py From SpaceXLaunchBot with MIT License | 5 votes |
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. changes.append(embed1["fields"][difference[1][1]]["name"]) else: changes.append(difference[1]) return changes
Example #6
Source File: protocol.py From GelReportModels with Apache License 2.0 | 5 votes |
def equals(self, instance): """ Method to compare entities :return: """ if not isinstance(instance, ProtocolElement): logging.error("Comparing instance of type {} with instance of type {}".format(type(self), type(instance))) return False differences = list(dictdiffer.diff(self.toJsonDict(), instance.toJsonDict())) if differences is None or differences == []: return True return differences
Example #7
Source File: helpers.py From gordo with GNU Affero General Public License v3.0 | 5 votes |
def patch_dict(original_dict: dict, patch_dictionary: dict) -> dict: """Patches a dict with another. Patching means that any path defines in the patch is either added (if it does not exist), or replaces the existing value (if it exists). Nothing is removed from the original dict, only added/replaced. Parameters ---------- original_dict : dict Base dictionary which will get paths added/changed patch_dictionary: dict Dictionary which will be overlaid on top of original_dict Examples -------- >>> patch_dict({"highKey":{"lowkey1":1, "lowkey2":2}}, {"highKey":{"lowkey1":10}}) {'highKey': {'lowkey1': 10, 'lowkey2': 2}} >>> patch_dict({"highKey":{"lowkey1":1, "lowkey2":2}}, {"highKey":{"lowkey3":3}}) {'highKey': {'lowkey1': 1, 'lowkey2': 2, 'lowkey3': 3}} >>> patch_dict({"highKey":{"lowkey1":1, "lowkey2":2}}, {"highKey2":4}) {'highKey': {'lowkey1': 1, 'lowkey2': 2}, 'highKey2': 4} Returns ------- dict A new dictionary which is the result of overlaying `patch_dictionary` on top of `original_dict` """ diff = dictdiffer.diff(original_dict, patch_dictionary) adds_and_mods = [(f, d, s) for (f, d, s) in diff if f != "remove"] return dictdiffer.patch(adds_and_mods, original_dict)
Example #8
Source File: template_data.py From python-viptela with GNU General Public License v3.0 | 5 votes |
def import_feature_template_list(self, feature_template_list, check_mode=False, update=False): """Import a list of feature templates from list to vManage. Object Names are converted to IDs. Args: feature_template_list (list): List of feature templates check_mode (bool): Only check to see if changes would be made update (bool): Update the template if it exists Returns: result (list): Returns the diffs of the updates. """ # Process the feature templates feature_template_updates = [] feature_template_dict = self.feature_templates.get_feature_template_dict(factory_default=True, remove_key=False) for feature_template in feature_template_list: if 'templateId' in feature_template: feature_template.pop('templateId') if feature_template['templateName'] in feature_template_dict: existing_template = feature_template_dict[feature_template['templateName']] feature_template['templateId'] = existing_template['templateId'] diff = list( dictdiffer.diff(existing_template['templateDefinition'], feature_template['templateDefinition'])) if len(diff): feature_template_updates.append({'name': feature_template['templateName'], 'diff': diff}) if not check_mode and update: self.feature_templates.update_feature_template(feature_template) else: diff = list(dictdiffer.diff({}, feature_template['templateDefinition'])) feature_template_updates.append({'name': feature_template['templateName'], 'diff': diff}) if not check_mode: self.feature_templates.add_feature_template(feature_template) return feature_template_updates
Example #9
Source File: policy_data.py From python-viptela with GNU General Public License v3.0 | 5 votes |
def import_local_policy_list(self, local_policy_list, update=False, push=False, check_mode=False, force=False): """Import Local Policies into vManage. Object names are converted to IDs. Returns: response (dict): A list of all policy lists currently in vManage. """ local_policy_dict = self.local_policy.get_local_policy_dict(remove_key=False) diff = [] local_policy_updates = [] for local_policy in local_policy_list: payload = {'policyName': local_policy['policyName']} payload['policyDescription'] = local_policy['policyDescription'] payload['policyType'] = local_policy['policyType'] payload['policyDefinition'] = local_policy['policyDefinition'] if payload['policyName'] in local_policy_dict: # A policy by that name already exists existing_policy = self.convert_policy_to_name(local_policy_dict[payload['policyName']]) diff_ignore = set([ 'lastUpdated', 'policyVersion', 'createdOn', 'references', 'isPolicyActivated', '@rid', 'policyId', 'createdBy', 'lastUpdatedBy', 'lastUpdatedOn', 'mastersAttached', 'policyDefinitionEdit', 'devicesAttached' ]) diff = list(dictdiffer.diff(existing_policy, payload, ignore=diff_ignore)) if diff: print(diff) local_policy_updates.append({'name': local_policy['policyName'], 'diff': diff}) if 'policyDefinition' in payload: self.convert_definition_name_to_id(payload['policyDefinition']) if not check_mode and update: self.local_policy.update_local_policy(payload, existing_policy['policyId']) else: diff = list(dictdiffer.diff({}, payload['policyDefinition'])) local_policy_updates.append({'name': local_policy['policyName'], 'diff': diff}) if 'policyDefinition' in payload: # Convert list and definition names to template IDs self.convert_definition_name_to_id(payload['policyDefinition']) if not check_mode: self.local_policy.add_local_policy(payload) return local_policy_updates
Example #10
Source File: config.py From openrasp-iast with Apache License 2.0 | 5 votes |
def _check_format(self): """ 检查配置格式, 使用默认配置代替丢失/错误配置 """ with open(self._main_path + "/config.default.yaml", "r") as f: origin_config = yaml.load(f, Loader=yaml.FullLoader) keys = [] diff = dictdiffer.diff(origin_config, self.config_dict) for item in diff: if item[0] == "remove": for diff_item in item[2]: key = diff_item[0] keys.append(key) print("[!] Missing config item: {}, use default.".format(key)) elif item[0] == "change" and not isinstance(item[2][0], type(item[2][1])): # 配置只有一层,只取[0]即可 key = item[1][0] keys.append(key) print("[!] Config item {} type error, expect {}, found {}, use default value.".format( item[1], type(item[2][0]), type(item[2][1]))) elif item[0] == "add": for diff_item in item[2]: key = diff_item[0] print("[!] Unknow config item {}, ignore.".format(key)) continue else: continue for key in keys: self.config_dict[key] = origin_config[key]
Example #11
Source File: api_views.py From esdc-ce with Apache License 2.0 | 5 votes |
def is_diff(self, data): return self.request.method == 'GET' and self.request.query_params.get('diff', False)
Example #12
Source File: api_views.py From esdc-ce with Apache License 2.0 | 5 votes |
def diff(self): if self._diff is None: self._diff = self.is_diff(self.data) return self._diff
Example #13
Source File: template_data.py From python-viptela with GNU General Public License v3.0 | 4 votes |
def import_device_template_list(self, device_template_list, check_mode=False, update=False): """Import a list of device templates from list to vManage. Object Names are converted to IDs. Args: device_template_list (list): List of device templates check_mode (bool): Only check to see if changes would be made update (bool): Update the template if it exists Returns: result (list): Returns the diffs of the updates. """ device_template_updates = [] device_template_dict = self.device_templates.get_device_template_dict() diff = [] for device_template in device_template_list: if 'policyId' in device_template: device_template.pop('policyId') if 'securityPolicyId' in device_template: device_template.pop('securityPolicyId') if device_template['templateName'] in device_template_dict: existing_template = self.convert_device_template_to_name( device_template_dict[device_template['templateName']]) device_template['templateId'] = existing_template['templateId'] # Just check the things that we care about changing. diff_ignore = set([ 'templateId', 'policyId', 'connectionPreferenceRequired', 'connectionPreference', 'templateName', 'attached_devices', 'input', 'securityPolicyId' ]) diff = list(dictdiffer.diff(existing_template, device_template, ignore=diff_ignore)) if len(diff): device_template_updates.append({'name': device_template['templateName'], 'diff': diff}) if not check_mode and update: if not check_mode: converted_device_template = self.convert_device_template_to_id(device_template) self.device_templates.update_device_template(converted_device_template) else: if 'generalTemplates' in device_template: diff = list(dictdiffer.diff({}, device_template['generalTemplates'])) elif 'templateConfiguration' in device_template: diff = list(dictdiffer.diff({}, device_template['templateConfiguration'])) else: raise Exception("Template {0} is of unknown type".format(device_template['templateName'])) device_template_updates.append({'name': device_template['templateName'], 'diff': diff}) if not check_mode: converted_device_template = self.convert_device_template_to_id(device_template) self.device_templates.add_device_template(converted_device_template) return device_template_updates
Example #14
Source File: policy_data.py From python-viptela with GNU General Public License v3.0 | 4 votes |
def import_policy_list_list(self, policy_list_list, push=False, update=False, check_mode=False, force=False): """Import a list of policyies lists into vManage. Object Names are translated to IDs. Args: policy_list_list: A list of polcies push (bool): Whether to push a change out update (bool): Whether to update when the list exists check_mode (bool): Report what updates would happen, but don't update Returns: result (dict): All data associated with a response. """ # Policy Lists diff = [] policy_list_updates = [] #pylint: disable=too-many-nested-blocks for policy_list in policy_list_list: policy_list_dict = self.policy_lists.get_policy_list_dict(policy_list['type'], remove_key=False, cache=False) if policy_list['name'] in policy_list_dict: existing_list = policy_list_dict[policy_list['name']] diff_ignore = set( ['listId', 'references', 'lastUpdated', 'activatedId', 'policyId', 'listId', 'isActivatedByVsmart']) diff = list(dictdiffer.diff(existing_list, policy_list, ignore=diff_ignore)) if diff: policy_list_updates.append({'name': policy_list['name'], 'diff': diff}) policy_list['listId'] = policy_list_dict[policy_list['name']]['listId'] # If description is not specified, try to get it from the existing information if not policy_list['description']: policy_list['description'] = policy_list_dict[policy_list['name']]['description'] if not check_mode and update: response = self.policy_lists.update_policy_list(policy_list) if response['json']: # Updating the policy list returns a `processId` that locks the list and 'masterTemplatesAffected' # that lists the templates affected by the change. if 'error' in response['json']: raise Exception(response['json']['error']['message']) elif 'processId' in response['json']: if push: vmanage_device_templates = DeviceTemplates(self.session, self.host) # If told to push out the change, we need to reattach each template affected by the change for template_id in response['json']['masterTemplatesAffected']: vmanage_device_templates.reattach_device_template(template_id) else: raise Exception("Did not get a process id when updating policy list") else: diff = list(dictdiffer.diff({}, policy_list)) policy_list_updates.append({'name': policy_list['name'], 'diff': diff}) if not check_mode: self.policy_lists.add_policy_list(policy_list) return policy_list_updates
Example #15
Source File: policy_data.py From python-viptela with GNU General Public License v3.0 | 4 votes |
def import_policy_definition_list(self, policy_definition_list, update=False, push=False, check_mode=False, force=False): """Import Policy Definitions into vManage. Object names are converted to IDs. Returns: response (dict): A list of all policy lists currently in vManage. """ policy_definition_updates = [] for definition in policy_definition_list: policy_definition_dict = self.policy_definitions.get_policy_definition_dict(definition['type'], remove_key=False) diff = [] payload = { "name": definition['name'], "description": definition['description'], "type": definition['type'], } if 'defaultAction' in definition: payload.update({'defaultAction': definition['defaultAction']}) if 'sequences' in definition: payload.update({'sequences': definition['sequences']}) if 'definition' in definition: payload.update({'definition': definition['definition']}) if definition['name'] in policy_definition_dict: existing_definition = self.convert_policy_definition_to_name(policy_definition_dict[definition['name']]) # Just check the things that we care about changing. diff_ignore = set([ 'lastUpdated', 'definitionId', 'referenceCount', 'references', 'owner', 'isActivatedByVsmart', 'infoTag', 'activatedId' ]) diff = list(dictdiffer.diff(existing_definition, payload, ignore=diff_ignore)) if diff: converted_definition = self.convert_policy_definition_to_id(definition) policy_definition_updates.append({'name': converted_definition['name'], 'diff': diff}) if not check_mode and update: self.policy_definitions.update_policy_definition( converted_definition, policy_definition_dict[converted_definition['name']]['definitionId']) policy_definition_updates.append({'name': converted_definition['name'], 'diff': diff}) else: # Policy definition does not exist diff = list(dictdiffer.diff({}, payload)) policy_definition_updates.append({'name': definition['name'], 'diff': diff}) converted_definition = self.convert_policy_definition_to_id(definition) if not check_mode: self.policy_definitions.add_policy_definition(converted_definition) return policy_definition_updates
Example #16
Source File: policy_data.py From python-viptela with GNU General Public License v3.0 | 4 votes |
def import_security_policy_list(self, security_policy_list, update=False, push=False, check_mode=False, force=False): """Import Security Policies into vManage. Object names are converted to IDs. Returns: response (dict): A list of all policy lists currently in vManage. """ security_policy_dict = self.security_policy.get_security_policy_dict(remove_key=False) diff = [] security_policy_updates = [] for security_policy in security_policy_list: payload = {'policyName': security_policy['policyName']} payload['policyDescription'] = security_policy['policyDescription'] payload['policyType'] = security_policy['policyType'] payload['policyDefinition'] = security_policy['policyDefinition'] if payload['policyName'] in security_policy_dict: # A policy by that name already exists existing_policy = self.convert_policy_to_name(security_policy_dict[payload['policyName']]) diff_ignore = set([ 'lastUpdated', 'policyVersion', 'createdOn', 'references', 'isPolicyActivated', '@rid', 'policyId', 'createdBy', 'lastUpdatedBy', 'lastUpdatedOn', 'policyDefinitionEdit', 'mastersAttached', 'devicesAttached', 'supportedDevices', 'virtualApplicationTemplates', 'policyUseCase' ]) diff = list(dictdiffer.diff(existing_policy, payload, ignore=diff_ignore)) if diff: security_policy_updates.append({'name': security_policy['policyName'], 'diff': diff}) # Convert list and definition names to template IDs converted_payload = self.convert_policy_to_id(payload) if not check_mode and update: self.security_policy.update_security_policy(converted_payload, existing_policy['policyId']) else: diff = list(dictdiffer.diff({}, payload['policyDefinition'])) security_policy_updates.append({'name': security_policy['policyName'], 'diff': diff}) if not check_mode: # Convert list and definition names to template IDs converted_payload = self.convert_policy_to_id(payload) self.security_policy.add_security_policy(converted_payload) return security_policy_updates