Functions¶
validate |
Check if a data type is well-formed. |
resolve |
Retrieve the properties for a given data type. |
update |
Update a data type with different values. |
get_data |
Retrieve the NumPy data from an array view generated by Nani. |
get_element_view |
Retrieve the element view from an array view generated by Nani. |
-
nani.
validate
(data_type)[source]¶ Check if a data type is well-formed.
Parameters: data_type (nani data type) – Data type. Returns: True
if the data type is well-formed.Return type: bool Raises: TypeError or ValueError – The data type isn’t well-formed.
-
nani.
resolve
(data_type, name=None, listify_default=False)[source]¶ Retrieve the properties for a given data type.
This is the main routine where most of the work is done. It converts Nani’s data types into properties that can be used to define a new NumPy array and to wrap it into a view object.
Use
validate()
to check if the input data type is well-formed.Parameters: - data_type (nani data type) – Type of the array elements.
- name (str) – Name for the view to be generated for the array.
- listify_default (bool) –
True
to output the default values with lists in place of tuples. This might cause the output to be incompatible with array creation routines such asnumpy.array
but it should still work for element assignment.
Returns: The properties to use to initalize a NumPy array around the data type.
Return type: Examples
Create a NumPy array where each element represents a color:
>>> import numpy >>> import nani >>> color_type = nani.Array( ... element_type=nani.Number(type=numpy.uint8, default=255), ... shape=3, ... view=None) >>> dtype, default, view = nani.resolve(color_type, name='Color') >>> a = numpy.array([default] * element_count, dtype=dtype) >>> v = view(a) >>> type(v) <class 'nani.Color'> >>> for color in v: ... color [255, 255, 255] [255, 255, 255]
-
nani.
update
(data_type, **kwargs)[source]¶ Update a data type with different values.
The operation is not made in place, instead a copy is returned.
Parameters: - data_type (nani data type) – Data type.
- kwargs – Keyword arguments to update.
Returns: The updated version of the data type.
Return type: nani data type
Examples
Update the shape of an array data type and the default value of its elements:
>>> import nani >>> data_type = nani.Array( ... element_type=nani.Number(), ... shape=2) >>> new_data_type = nani.update( ... data_type, ... element_type=nani.update(data_type.element_type, default=123), ... shape=3)
-
nani.
get_data
(view)[source]¶ Retrieve the NumPy data from an array view generated by Nani.
Parameters: view (nani.AtomicArrayView or nani.CompositeArrayView) – Array view. Returns: The NumPy array, None
otherwise.Return type: type Examples
>>> import numpy >>> import nani >>> data_type = nani.Number(type=numpy.int32) >>> dtype, _, view = nani.resolve(data_type) >>> a = numpy.a_range(10, dtype=dtype) >>> v = view(a) >>> nani.get_data(v) is a True
-
nani.
get_element_view
(view)[source]¶ Retrieve the element view from an array view generated by Nani.
Parameters: view (nani.AtomicArrayView or nani.CompositeArrayView) – Array view. Returns: The element view, None
otherwise.Return type: type Examples
>>> import numpy >>> import nani >>> vector2_type = nani.Array( ... element_type=nani.Number(), ... shape=2, ... name='Vector2') >>> dtype, default, view = nani.resolve(vector2_type, name='Positions') >>> a = numpy.zeros(3, dtype=dtype) >>> v = view(a) >>> type(v) <class 'nani.Positions'> >>> nani.get_element_view(v) <class 'nani.Vector2'>