patch_function

function of dascore.utils.patch source

patch_function(
    required_dims: tuple[tuple[str, …], Callable, None] = None,
    required_attrs: dict[dict[str, Any], str, collections.abc.Sequence[str], None] = None,
    history: Literal[‘full’, ‘method_name’, None] = full,
)

Decorator to mark a function as a patch method.

Parameters

Parameter Description
required_dims A tuple of dimensions which must be found in the Patch.
required_attrs A dict of attributes which must be found in the Patch and whose
values must be equal to those provided.
history Specifies how to track history on Patch.
Full - Records function name and str version of input arguments.
method_name - Only records method name. Useful if args are long.
None - Function call is not recorded in history attribute.

Examples

import dascore as dc
@dc.patch_function(required_dims=('time', 'distance'))
def do_something(patch):
    ...   # raises a PatchCoordsError if patch doesn't have time,
    #  distance
import dascore as dc
@dc.patch_function(required_attrs={'data_type': 'DAS'})
def do_another_thing(patch):
    ...  # raise PatchAttributeError if patch doesn't have attribute
    # called "data_type" or its values is not equal to "DAS".
Note

The original function can still be accessed with the .func attribute. This may be useful for avoiding calling the patch_func machinery multiple times from within another patch function.