Wrap a number(either python scalar or numpy scalar) as core.Scalar if it is not a scalar. Args: number (Number): number Returns: Scalar: A Scalar that contains the value.
(number)
| 1605 | |
| 1606 | |
| 1607 | def wrap_as_scalar(number): |
| 1608 | """Wrap a number(either python scalar or numpy scalar) as core.Scalar if |
| 1609 | it is not a scalar. |
| 1610 | |
| 1611 | |
| 1612 | Args: |
| 1613 | number (Number): number |
| 1614 | |
| 1615 | Returns: |
| 1616 | Scalar: A Scalar that contains the value. |
| 1617 | """ |
| 1618 | if isinstance(number, core.Scalar): |
| 1619 | return number |
| 1620 | if isinstance(number, (bool, int, float, complex)): |
| 1621 | return core.Scalar(number) |
| 1622 | if isinstance(number, np.number): |
| 1623 | # it is a numpy scalar |
| 1624 | return core.Scalar(number.item()) |
| 1625 | else: |
| 1626 | raise TypeError(f"Cannot wrap {number} as core.Scalar") |
| 1627 | |
| 1628 | |
| 1629 | def wrap_as_scalars(array): |
no test coverage detected