当前位置: 首页>>代码示例>>Python>>正文


Python jsonpatch.make_patch方法代码示例

本文整理汇总了Python中jsonpatch.make_patch方法的典型用法代码示例。如果您正苦于以下问题:Python jsonpatch.make_patch方法的具体用法?Python jsonpatch.make_patch怎么用?Python jsonpatch.make_patch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jsonpatch的用法示例。


在下文中一共展示了jsonpatch.make_patch方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: prepare_patch

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def prepare_patch(changes, orig, patch, basepath=''):
    if isinstance(patch, dict):
        for i in patch:
            if i in orig:
                prepare_patch(changes, orig[i], patch[i], '{}/{}'.format(basepath, i))
            else:
                changes.append({'op': 'add', 'path': '{}/{}'.format(basepath, i), 'value': patch[i]})
    elif isinstance(patch, list):
        if len(patch) < len(orig):
            for i in reversed(range(len(patch), len(orig))):
                changes.append({'op': 'remove', 'path': '{}/{}'.format(basepath, i)})
        for i, j in enumerate(patch):
            if len(orig) > i:
                prepare_patch(changes, orig[i], patch[i], '{}/{}'.format(basepath, i))
            else:
                changes.append({'op': 'add', 'path': '{}/{}'.format(basepath, i), 'value': j})
    else:
        for x in make_patch(orig, patch).patch:
            x['path'] = '{}{}'.format(basepath, x['path'])
            changes.append(x) 
开发者ID:openprocurement,项目名称:openprocurement.api,代码行数:22,代码来源:utils.py

示例2: get_revision_changes

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def get_revision_changes(dst, src):
    return make_patch(dst, src).patch 
开发者ID:openprocurement,项目名称:openprocurement.api,代码行数:4,代码来源:utils.py

示例3: _prepare_request_body

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def _prepare_request_body(self, patch, prepend_key):
        if patch:
            if not self._store_unknown_attrs_as_properties:
                # Default case
                new = self._body.attributes
                original_body = self._original_body
            else:
                new = self._unpack_properties_to_resource_root(
                    self._body.attributes)
                original_body = self._unpack_properties_to_resource_root(
                    self._original_body)

            # NOTE(gtema) sort result, since we might need validate it in tests
            body = sorted(
                list(jsonpatch.make_patch(
                    original_body,
                    new).patch),
                key=operator.itemgetter('path')
            )
        else:
            if not self._store_unknown_attrs_as_properties:
                # Default case
                body = self._body.dirty
            else:
                body = self._unpack_properties_to_resource_root(
                    self._body.dirty)

            if prepend_key and self.resource_key is not None:
                body = {self.resource_key: body}
        return body 
开发者ID:openstack,项目名称:openstacksdk,代码行数:32,代码来源:resource.py

示例4: document_needs_updating

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def document_needs_updating(enrollment):
    """
    Get the document from elasticsearch and see if it matches what's in the database

    Args:
        enrollment (ProgramEnrollment): A program enrollment

    Returns:
        bool: True if the document needs to be updated via reindex
    """
    index = get_default_alias(PRIVATE_ENROLLMENT_INDEX_TYPE)

    conn = get_conn()
    try:
        document = conn.get(index=index, doc_type=GLOBAL_DOC_TYPE, id=enrollment.id)
    except NotFoundError:
        return True
    serialized_enrollment = serialize_program_enrolled_user(enrollment)
    del serialized_enrollment['_id']
    source = document['_source']

    if serialized_enrollment != source:
        # Convert OrderedDict to dict
        reserialized_enrollment = json.loads(json.dumps(serialized_enrollment))

        diff = make_patch(source, reserialized_enrollment).patch
        serialized_diff = json.dumps(diff, indent="    ")
        log.info("Difference found for enrollment %s: %s", enrollment, serialized_diff)
        return True
    return False 
开发者ID:mitodl,项目名称:micromasters,代码行数:32,代码来源:api.py

示例5: diff

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def diff(self, hash1, hash2=None, txid=None):
        branch = self._branches[txid]
        rev1 = branch[hash1]
        rev2 = branch[hash2] if hash2 else branch._latest
        if rev1.hash == rev2.hash:
            return JsonPatch([])
        else:
            dict1 = message_to_dict(rev1.data)
            dict2 = message_to_dict(rev2.data)
            return make_patch(dict1, dict2)

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tagging utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
开发者ID:opencord,项目名称:voltha,代码行数:14,代码来源:config_node.py

示例6: diffMsgs

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def diffMsgs(self, msg1, msg2):
        msg1_dict = MessageToDict(msg1)
        msg2_dict = MessageToDict(msg2)
        diff = make_patch(msg1_dict, msg2_dict)
        return dumps(diff.patch, indent=2) 
开发者ID:opencord,项目名称:voltha,代码行数:7,代码来源:flow_helpers.py

示例7: get_version_diff

# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import make_patch [as 别名]
def get_version_diff(from_data, to_data):
    """Calculate the diff (a mangled JSON patch) between from_data and to_data"""

    basic_patch = jsonpatch.make_patch(from_data, to_data)
    result = []
    for operation in sorted(basic_patch, key=lambda o: (o['op'], o['path'])):
        op = operation['op']
        ignore = False
        # We deal with standing_in and party_memberships slightly
        # differently so they can be presented in human-readable form,
        # so match those cases first:
        m = re.search(
            r'(standing_in|party_memberships)(?:/([^/]+))?(?:/(\w+))?',
            operation['path'],
        )
        if op in ('replace', 'remove'):
            operation['previous_value'] = \
                jsonpointer.resolve_pointer(
                    from_data,
                    operation['path'],
                    default=None
                )

        attribute, election, leaf = m.groups() if m else (None, None, None)
        if attribute:
            explain_standing_in_and_party_memberships(operation, attribute, election, leaf)
        if op in ('replace', 'remove'):
            if op == 'replace' and not operation['previous_value']:
                if operation['value']:
                    operation['op'] = 'add'
                else:
                    # Ignore replacing no data with no data:
                    ignore = True
        elif op == 'add':
            # It's important that we don't skip the case where a
            # standing_in value is being set to None, because that's
            # saying 'we *know* they're not standing then'
            if (not operation['value']) and (attribute != 'standing_in'):
                ignore = True
        operation['path'] = re.sub(r'^/', '', operation['path'])
        if not ignore:
            result.append(operation)
        # The operations generated by jsonpatch are incremental, so we
        # need to apply each before going on to parse the next:
        operation['path'] = '/' + operation['path']
        from_data = jsonpatch.apply_patch(from_data, [operation])
    for operation in result:
        operation['path'] = operation['path'].lstrip('/')
    return result 
开发者ID:mysociety,项目名称:yournextrepresentative,代码行数:51,代码来源:diffs.py


注:本文中的jsonpatch.make_patch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。