|
15 | 15 |
|
16 | 16 |
|
17 | 17 | def expects(*args_units, return_value=None, **kwargs_units):
|
| 18 | + """ |
| 19 | + Decorator which ensures the inputs and outputs of the decorated |
| 20 | + function are expressed in the expected units. |
| 21 | +
|
| 22 | + Arguments to the decorated function are checked for the specified |
| 23 | + units, converting to those units if necessary, and then stripped |
| 24 | + of their units before being passed into the undecorated |
| 25 | + function. Therefore the undecorated function should expect |
| 26 | + unquantified DataArrays, Datasets, or numpy-like arrays, but with |
| 27 | + the values expressed in specific units. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + func : callable |
| 32 | + Function to decorate, which accepts zero or more |
| 33 | + xarray.DataArrays or numpy-like arrays as inputs, and may |
| 34 | + optionally return one or more xarray.DataArrays or numpy-like |
| 35 | + arrays. |
| 36 | + *args_units : unit-like or mapping of hashable to unit-like, optional |
| 37 | + Units to expect for each positional argument given to func. |
| 38 | +
|
| 39 | + The decorator will first check that arguments passed to the |
| 40 | + decorated function possess these specific units (or will |
| 41 | + attempt to convert the argument to these units), then will |
| 42 | + strip the units before passing the magnitude to the wrapped |
| 43 | + function. |
| 44 | +
|
| 45 | + A value of None indicates not to check that argument for units |
| 46 | + (suitable for flags and other non-data arguments). |
| 47 | + return_value : unit-like or list of unit-like or mapping of hashable to unit-like \ |
| 48 | + or list of mapping of hashable to unit-like, optional |
| 49 | + The expected units of the returned value(s), either as a |
| 50 | + single unit or as a list of units. The decorator will attach |
| 51 | + these units to the variables returned from the function. |
| 52 | +
|
| 53 | + A value of None indicates not to attach any units to that |
| 54 | + return value (suitable for flags and other non-data results). |
| 55 | + **kwargs_units : mapping of hashable to unit-like, optional |
| 56 | + Unit to expect for each keyword argument given to func. |
| 57 | +
|
| 58 | + The decorator will first check that arguments passed to the decorated |
| 59 | + function possess these specific units (or will attempt to convert the |
| 60 | + argument to these units), then will strip the units before passing the |
| 61 | + magnitude to the wrapped function. |
| 62 | +
|
| 63 | + A value of None indicates not to check that argument for units (suitable |
| 64 | + for flags and other non-data arguments). |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + return_values : Any |
| 69 | + Return values of the wrapped function, either a single value or a tuple |
| 70 | + of values. These will be given units according to return_units. |
| 71 | +
|
| 72 | + Raises |
| 73 | + ------ |
| 74 | + TypeError |
| 75 | + If any of the units are not a valid type |
| 76 | + ValueError |
| 77 | + If the number of arguments or return values does not match the number of |
| 78 | + units specified. Also thrown if any parameter does not have a unit |
| 79 | + specified. |
| 80 | +
|
| 81 | +
|
| 82 | + Examples |
| 83 | + -------- |
| 84 | +
|
| 85 | + Decorating a function which takes one quantified input, but |
| 86 | + returns a non-data value (in this case a boolean). |
| 87 | +
|
| 88 | + >>> @expects("deg C") |
| 89 | + ... def above_freezing(temp): |
| 90 | + ... return temp > 0 |
| 91 | + ... |
| 92 | +
|
| 93 | + Decorating a function which allows any dimensions for the array, but also |
| 94 | + accepts an optional `weights` keyword argument, which must be dimensionless. |
| 95 | +
|
| 96 | + >>> @expects(None, weights="dimensionless") |
| 97 | + ... def mean(da, weights=None): |
| 98 | + ... if weights: |
| 99 | + ... return da.weighted(weights=weights).mean() |
| 100 | + ... else: |
| 101 | + ... return da.mean() |
| 102 | + ... |
| 103 | + """ |
| 104 | + |
18 | 105 | def outer(func):
|
19 | 106 | signature = inspect.signature(func)
|
20 | 107 |
|
|
0 commit comments