General decorators for the landlab library.
use_file_name_or_kwds (func) |
Decorate a method so that it takes a file name or keywords. |
use_field_name_or_array (at_element) |
Decorate a function so that it accepts a field name or array. |
make_return_array_immutable (func) |
Decorate a function so that its return array is read-only. |
deprecated (use, version) |
Mark a function as deprecated. |
deprecated
(use, version)[source]¶Mark a function as deprecated.
Parameters: | use : str
version : str
|
---|---|
Returns: | func
|
make_return_array_immutable
(func)[source]¶Decorate a function so that its return array is read-only.
Parameters: | func : function
|
---|---|
Returns: | func
|
read_only_array
(func)[source]¶Decorate a function so that its return array is read-only.
Parameters: | func : function
|
---|---|
Returns: | func
|
use_field_name_or_array
(at_element)[source]¶Bases: object
Decorate a function so that it accepts a field name or array.
Parameters: | func : function
at_element : str
|
---|---|
Returns: | func
|
Examples
>>> from landlab import RasterModelGrid
>>> grid = RasterModelGrid((4, 5), spacing=(1, 2))
>>> def my_func(grid, vals):
... return grid.area_of_cell * vals
>>> my_func(grid, np.arange(grid.number_of_cells))
array([ 0., 2., 4., 6., 8., 10.])
Decorate the function so that the second argument can be array-like or the name of a field contained withing the grid. The decorator takes a single argument that is the name (as a str) of the grid element that the values are defined on (“node”, “cell”, etc.).
>>> from landlab.utils.decorators import use_field_name_or_array
>>> @use_field_name_or_array('cell')
... def my_func(grid, vals):
... return grid.area_of_cell * vals
The array of values now can be list or anything that can be converted to a numpy array.
>>> my_func(grid, [0, 1, 2, 3, 4, 5])
array([ 0., 2., 4., 6., 8., 10.])
The array of values doesn’t have to be flat.
>>> vals = np.array([[0, 1, 2], [3, 4, 5]])
>>> my_func(grid, vals)
array([ 0., 2., 4., 6., 8., 10.])
The array of values can be a field name.
>>> _ = grid.add_field('cell', 'elevation', [0, 1, 2, 3, 4, 5])
>>> my_func(grid, 'elevation')
array([ 0., 2., 4., 6., 8., 10.])
use_file_name_or_kwds
(func)[source]¶Decorate a method so that it takes a file name or keywords.
Parameters: | func : A function
|
---|---|
Returns: | function
|
Examples
>>> from landlab import RasterModelGrid
>>> from landlab.utils.decorators import use_file_name_or_kwds
>>> class MyClass(object):
... @use_file_name_or_kwds
... def __init__(self, grid, kw=0.):
... self.kw = kw
>>> grid = RasterModelGrid((4, 5))
>>> foo = MyClass(grid)
>>> foo.kw
0.0
>>> foo = MyClass(grid, "kw: 1945")
>>> foo.kw
1945
>>> foo = MyClass(grid, "kw: 1945", kw=1973)
>>> foo.kw
1973
>>> mpd = """
... kw: kw value
... 1e6
... """
>>> foo = MyClass(grid, mpd)
>>> foo.kw
1000000.0