Patch#
- class pyhf.patchset.Patch(spec)[source]#
Bases:
jsonpatch.JsonPatch
A way to store a patch definition as part of a patchset (
PatchSet
).It contains
metadata
about the Patch itself:In addition to the above metadata, the Patch object behaves like the underlying
jsonpatch.JsonPatch
.- __init__(spec)[source]#
Construct a Patch.
- Parameters:
spec (
jsonable
) – The patch JSON specification- Returns:
The Patch instance.
- Return type:
patch (
Patch
)
Attributes
- metadata#
The metadata of the patch
- name#
The name of the patch
- operations = mappingproxy({'remove': <class 'jsonpatch.RemoveOperation'>, 'add': <class 'jsonpatch.AddOperation'>, 'replace': <class 'jsonpatch.ReplaceOperation'>, 'move': <class 'jsonpatch.MoveOperation'>, 'test': <class 'jsonpatch.TestOperation'>, 'copy': <class 'jsonpatch.CopyOperation'>})#
A JSON Patch is a list of Patch Operations.
>>> patch = JsonPatch([ ... {'op': 'add', 'path': '/foo', 'value': 'bar'}, ... {'op': 'add', 'path': '/baz', 'value': [1, 2, 3]}, ... {'op': 'remove', 'path': '/baz/1'}, ... {'op': 'test', 'path': '/baz', 'value': [1, 3]}, ... {'op': 'replace', 'path': '/baz/0', 'value': 42}, ... {'op': 'remove', 'path': '/baz/1'}, ... ]) >>> doc = {} >>> result = patch.apply(doc) >>> expected = {'foo': 'bar', 'baz': [42]} >>> result == expected True
JsonPatch object is iterable, so you can easily access each patch statement in a loop:
>>> lpatch = list(patch) >>> expected = {'op': 'add', 'path': '/foo', 'value': 'bar'} >>> lpatch[0] == expected True >>> lpatch == patch.patch True
Also JsonPatch could be converted directly to
bool
if it contains any operation statements:>>> bool(patch) True >>> bool(JsonPatch([])) False
This behavior is very handy with
make_patch()
to write more readable code:>>> old = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} >>> new = {'baz': 'qux', 'numbers': [1, 4, 7]} >>> patch = make_patch(old, new) >>> if patch: ... # document have changed, do something useful ... patch.apply(old) {...}
- values#
The values of the associated labels for the patch
Methods