本文整理汇总了Python中jsonpatch.JsonPatch方法的典型用法代码示例。如果您正苦于以下问题:Python jsonpatch.JsonPatch方法的具体用法?Python jsonpatch.JsonPatch怎么用?Python jsonpatch.JsonPatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonpatch
的用法示例。
在下文中一共展示了jsonpatch.JsonPatch方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _apply_patches
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def _apply_patches(self, resources):
for _, resource in resources.iteritems():
if self.namespace:
if 'namespace' in resource['value']['metadata']:
op = 'replace'
else:
op = 'add'
resource['patch'].append({
"op": op,
"path": "/metadata/namespace",
"value": self.namespace})
if len(resource['patch']):
patch = jsonpatch.JsonPatch(resource['patch'])
result = patch.apply(resource['value'])
resource['value'] = result
return resources
示例2: _apply_patches
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def _apply_patches(self, resources):
for _, resource in resources.iteritems():
if self.namespace:
if 'namespace' in resource['value']['metadata']:
op = 'replace'
else:
op = 'add'
resource['patch'].append({
"op": op,
"path": "/metadata/namespace",
"value": self.namespace
})
if len(resource['patch']):
patch = jsonpatch.JsonPatch(resource['patch'])
result = patch.apply(resource['value'])
resource['value'] = result
return resources
示例3: model
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def model(self, **config_kwargs):
"""
Create a model object with/without patches applied.
See :func:`pyhf.workspace.Workspace.get_measurement` and :class:`pyhf.pdf.Model` for possible keyword arguments.
Args:
patches: A list of JSON patches to apply to the model specification
config_kwargs: Possible keyword arguments for the measurement and model configuration
Returns:
~pyhf.pdf.Model: A model object adhering to the schema model.json
"""
poi_name = config_kwargs.pop('poi_name', None)
measurement_name = config_kwargs.pop('measurement_name', None)
measurement_index = config_kwargs.pop('measurement_index', None)
measurement = self.get_measurement(
poi_name=poi_name,
measurement_name=measurement_name,
measurement_index=measurement_index,
)
log.debug(
'model being created for measurement {0:s}'.format(measurement['name'])
)
patches = config_kwargs.pop('patches', [])
modelspec = {
'channels': self['channels'],
'parameters': measurement['config']['parameters'],
}
for patch in patches:
modelspec = jsonpatch.JsonPatch(patch).apply(modelspec)
return Model(modelspec, poi_name=measurement['config']['poi'], **config_kwargs)
示例4: json2xml
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def json2xml(workspace, output_dir, specroot, dataroot, resultprefix, patch):
"""Convert pyhf JSON back to XML + ROOT files."""
try:
import uproot
assert uproot
except ImportError:
log.error(
"json2xml requires uproot, please install pyhf using the "
"xmlio extra: python -m pip install pyhf[xmlio]"
)
from .. import writexml
os.makedirs(output_dir, exist_ok=True)
with click.open_file(workspace, 'r') as specstream:
spec = json.load(specstream)
for pfile in patch:
patch = json.loads(click.open_file(pfile, 'r').read())
spec = jsonpatch.JsonPatch(patch).apply(spec)
os.makedirs(Path(output_dir).joinpath(specroot), exist_ok=True)
os.makedirs(Path(output_dir).joinpath(dataroot), exist_ok=True)
with click.open_file(
Path(output_dir).joinpath(f'{resultprefix}.xml'), 'w'
) as outstream:
outstream.write(
writexml.writexml(
spec,
Path(output_dir).joinpath(specroot),
Path(output_dir).joinpath(dataroot),
resultprefix,
).decode('utf-8')
)
示例5: __eq__
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def __eq__(self, other):
""" Equality for subclass with new attributes """
if not isinstance(other, Patch):
return False
return (
jsonpatch.JsonPatch.__eq__(self, other) and self.metadata == other.metadata
)
示例6: apply_jsonpatch
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def apply_jsonpatch(doc, patch):
for p in patch:
if p['op'] == 'add' and p['path'].count('/') == 1:
if p['path'].lstrip('/') not in doc:
msg = _('Adding a new attribute (%s) to the root of '
' the resource is not allowed')
raise wsme.exc.ClientSideError(msg % p['path'])
return jsonpatch.apply_patch(doc, jsonpatch.JsonPatch(patch))
示例7: patch
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def patch(operations):
if jsonpatch:
json_patch = jsonpatch.JsonPatch(operations)
def patcher(doc):
return json_patch.apply(doc)
else:
def patcher(doc):
# If jsonpatch could not be imported, then `@chaos_utils.patch()`
# will be disabled, and will silently return values unmodified,
# without applying the JSON patch operations.
return doc
def inner(patched_function):
def patched_inner(*args, **kwargs):
return_value = patched_function(*args, **kwargs)
not_json = False
if not isinstance(return_value, six.text_type):
return_value = json.dumps(return_value)
not_json = True
return_value = patcher(return_value)
if not_json:
return_value = json.loads(return_value)
return return_value
return patched_inner
return inner
示例8: default
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [as 别名]
def default(self, obj):
"""Set defaults
:param obj: json object.
:type obj: str.
"""
if isinstance(obj, Dictable):
return obj.to_dict()
elif isinstance(obj, jsonpatch.JsonPatch):
return obj.patch
return super(JSONEncoder, self).default(obj)
示例9: diff
# 需要导入模块: import jsonpatch [as 别名]
# 或者: from jsonpatch import JsonPatch [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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~