Util¶
Utility functions for property-based test assertions, etc.
Functions:
| Name | Description |
|---|---|
any_nan_nat_in_awkward_array |
|
any_nan_in_awkward_array |
|
any_nat_in_awkward_array |
|
iter_contents |
Iterate over all contents in an Awkward Array layout. |
iter_leaf_contents |
Iterate over all leaf contents in an Awkward Array layout. |
iter_numpy_arrays |
Iterate over all NumPy arrays in an Awkward Array layout. |
leaf_size |
Count total leaf elements in an Awkward Array layout. |
content_size |
Count total scalars stored in an Awkward Array layout. |
any_nan_nat_in_numpy_array |
|
any_nan_in_numpy_array |
|
any_nat_in_numpy_array |
|
simple_dtypes_in |
Return simple dtypes contained in a (compound) dtype |
simple_dtype_kinds_in |
Return character codes of simple dtypes contained in a (compound) dtype |
n_scalars_in |
Return the number of scalar values contained in a value of dtype |
safe_compare |
The |
safe_max |
The largest item in |
safe_min |
The smallest item in |
CountdownDrawer |
Create a draw function with a shared element budget. |
any_nan_nat_in_awkward_array ¶
True if Awkward Array contains any NaN or NaT values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
any_nan_in_awkward_array ¶
True if Awkward Array contains any NaN values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
any_nat_in_awkward_array ¶
True if Awkward Array contains any NaT values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
iter_contents ¶
iter_contents(
a: Array | Content,
/,
*,
string_as_leaf: bool = True,
bytestring_as_leaf: bool = True,
) -> Iterator[Content]
Iterate over all contents in an Awkward Array layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array or Content. |
required |
string_as_leaf
|
bool
|
If |
True
|
bytestring_as_leaf
|
bool
|
If |
True
|
Yields:
| Type | Description |
|---|---|
Content
|
Each content node in the layout. |
iter_leaf_contents ¶
iter_leaf_contents(
a: Array | Content,
/,
*,
string_as_leaf: bool = True,
bytestring_as_leaf: bool = True,
) -> Iterator[LeafContent]
Iterate over all leaf contents in an Awkward Array layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array or Content. |
required |
string_as_leaf
|
bool
|
If |
True
|
bytestring_as_leaf
|
bool
|
If |
True
|
Yields:
| Type | Description |
|---|---|
LeafContent
|
Each leaf content in the layout. |
iter_numpy_arrays ¶
iter_numpy_arrays(
a: Array | Content,
/,
*,
exclude_string: bool = True,
exclude_bytestring: bool = True,
) -> Iterator[np.ndarray]
Iterate over all NumPy arrays in an Awkward Array layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array or Content. |
required |
exclude_string
|
bool
|
If |
True
|
exclude_bytestring
|
bool
|
If |
True
|
Yields:
| Type | Description |
|---|---|
ndarray
|
Each underlying NumPy array in the layout. |
Examples:
leaf_size ¶
Count total leaf elements in an Awkward Array layout.
Each NumpyArray element counts as one. Each string and bytestring
(not character or byte) counts as one. EmptyArray counts as zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array or Content. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of leaf elements. |
Examples:
content_size ¶
Count total scalars stored in an Awkward Array layout.
Counts data elements, offset/index buffer elements, and metadata values
(RegularArray.size, RecordArray field names).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array | Content
|
An Awkward Array or Content. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of scalars stored in the content tree. |
Examples:
A flat array has content_size equal to its length:
A variable-length list array counts offsets (n+1) plus child data:
A string array counts offsets (n+1) plus UTF-8 bytes:
any_nan_nat_in_numpy_array ¶
True if NumPy array contains any NaN or NaT values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
ndarray
|
A NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
any_nan_in_numpy_array ¶
True if NumPy array contains any NaN values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
ndarray
|
A NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
any_nat_in_numpy_array ¶
True if NumPy array contains any NaT values, else False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
ndarray
|
A NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Examples:
simple_dtypes_in ¶
Return simple dtypes contained in a (compound) dtype d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dtype
|
A NumPy dtype. It can be a sub-array or structured dtype as well as a simple dtype. |
required |
Returns:
| Type | Description |
|---|---|
set of np.dtype
|
Simple dtypes contained in |
Examples:
simple_dtype_kinds_in ¶
Return character codes of simple dtypes contained in a (compound) dtype d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dtype
|
A NumPy dtype. It can be a sub-array or structured dtype as well as a simple dtype. |
required |
Returns:
| Type | Description |
|---|---|
set of str
|
Character codes of simple dtypes contained in |
Examples:
n_scalars_in ¶
Return the number of scalar values contained in a value of dtype d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dtype
|
A NumPy dtype. It can be a sub-array or structured dtype as well as a simple dtype. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of scalar values contained in a value of dtype |
Examples:
safe_compare ¶
The value itself if it is not None. Otherwise, an object
that returns True for all inequality comparisons.
This function helps you concisely write assertions that compare
values that may be None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T | None
|
A value or |
required |
Examples:
Suppose you have min_ and max_ that may be None
and val that should be in the range [min_, max_]:
Without this function, you need to check if min_ and max_
are None.
This function lets you write the same assertion in one line:
safe_max ¶
The largest item in vals that is not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vals
|
Iterable[T]
|
An iterable of values. |
required |
default
|
Optional[T]
|
The value to return if |
None
|
Examples:
It returns None if vals is empty or all items in vals are None.
If default is given, it returns default instead of None.
safe_min ¶
The smallest item in vals that is not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vals
|
Iterable[T]
|
An iterable of values. |
required |
default
|
Optional[T]
|
The value to return if |
None
|
Examples:
It returns None if vals is empty or all items in vals are None.
If default is given, it returns default instead of None.
CountdownDrawer ¶
CountdownDrawer(
draw: DrawFn,
st_: _StWithMinMaxSize[_T],
min_size_each: int = 0,
max_size_each: int | None = None,
min_size_total: int = 0,
max_size_total: int = 10,
max_draws: int = 100,
) -> Callable[[], _T | None]
Create a draw function with a shared element budget.
Each call draws from st_ and adds the length of the result
to a running total. Returns None once the budget is exhausted,
too small to satisfy min_size_each, or the draw limit is reached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
draw
|
DrawFn
|
The Hypothesis draw function. |
required |
st_
|
_StWithMinMaxSize[_T]
|
A callable that accepts |
required |
min_size_each
|
int
|
Minimum number of elements in each draw. |
0
|
max_size_each
|
int | None
|
Maximum number of elements in each draw. If |
None
|
min_size_total
|
int
|
Minimum total elements across all draws. |
0
|
max_size_total
|
int
|
Total element budget shared across all draws. |
10
|
max_draws
|
int
|
Maximum number of non-None draws. |
100
|