Skip to content

Commit ef1b133

Browse files
committed
I reorganized comparison section based on my own PR feedback inspired by a comment from bkeryan. I like this organization better than what we had before.
Signed-off-by: Kosta Ilic <[email protected]>
1 parent dc7049a commit ef1b133

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

src/nitypes/scalar.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,68 @@ class Scalar(Generic[TScalar_co]):
4949
Comparing Scalar Objects
5050
^^^^^^^^^^^^^^^^^^^^^^^^
5151
52-
You can compare scalar objects of compatible types and with identical units
53-
using the standard comparison operators (``<``, ``<=``, ``>``, ``>=``, ``==``, and ``!=``).
52+
You can compare scalar objects using standard comparison
53+
operators: ``<``, ``<=``, ``>``, ``>=``, ``==``, and ``!=``.
54+
Detailed descriptions of operator behaviors are provided below.
5455
55-
Here are some examples of comparing numeric scalar objects with identical units:
56+
Equality Comparison Operators
57+
-----------------------------
5658
57-
>>> Scalar(5.0, 'V') < Scalar(10.0, 'V')
59+
Equality comparison operators (``==`` and ``!=``) are always supported and behave as follows:
60+
61+
- Comparison of scalar objects with compatible types and identical units results
62+
in ``True`` or ``False`` based on the comparison of scalar object values.
63+
- Comparison of scalar objects with incompatible types (such as numeric and string)
64+
results in inequality.
65+
- Comparison of scalar objects with different units results in inequality.
66+
67+
Examples:
68+
69+
>>> Scalar(5.0, 'V') == Scalar(5.0, 'V') # Numeric scalars with identical values and units
5870
True
59-
>>> Scalar(5.0, 'V') >= Scalar(10.0, 'V')
71+
>>> Scalar(5.0, 'V') == Scalar(12.3, 'V') # Numeric scalars with identical units
6072
False
61-
>>> Scalar(5.0, 'V') == Scalar(5.0, 'V')
73+
>>> Scalar(5.0, 'V') != Scalar(12.3, 'V') # Numeric scalars with identical units
6274
True
75+
>>> Scalar("apple") == Scalar("banana") # String scalars
76+
False
77+
>>> Scalar("apple") == Scalar("Apple") # String scalars - note case sensitivity
78+
False
79+
>>> Scalar(0.5, 'V') == Scalar(500, 'mV') # Numeric scalars with different units
80+
False
81+
>>> Scalar(5.0, 'V') == Scalar("5.0", 'V') # Comparison of a numeric and a string scalar
82+
False
6383
64-
Here are some examples of comparing string scalar objects without units:
84+
Order Comparison Operators
85+
--------------------------
6586
66-
>>> Scalar("apple") < Scalar("banana")
67-
True
68-
>>> Scalar("apple") < Scalar("Banana")
69-
False
87+
Order comparison operators (``<``, ``<=``, ``>``, and ``>=``) behave as follows:
7088
71-
Equality comparison operators (``==`` and ``!=``) are always supported.
89+
- Comparison of scalar objects with compatible types and identical units results
90+
in ``True`` or ``False`` based on the comparison of scalar object values.
91+
- Comparison of scalar objects with incompatible types (such as numeric and string)
92+
is not permitted and will raise a ``TypeError`` exception.
93+
- Comparison of scalar objects with compatible types and different units
94+
is not permitted and will raise a ``ValueError`` exception.
7295
73-
Here are some examples of comparing scalar objects with different units:
96+
Examples:
7497
75-
>>> Scalar(0.5, 'V') == Scalar(500, 'mV')
98+
>>> Scalar(5.0, 'V') < Scalar(10.0, 'V') # Numeric scalars with identical units
99+
True
100+
>>> Scalar(5.0, 'V') >= Scalar(10.0, 'V') # Numeric scalars with identical units
76101
False
77-
>>> Scalar(0.5, 'V') != Scalar(500, 'mV')
102+
>>> Scalar("apple") < Scalar("banana") # String scalars
78103
True
79-
80-
Here is an example of comparing numeric and string scalar objects:
81-
82-
>>> Scalar(5.0, 'V') == Scalar("5.0", 'V')
104+
>>> Scalar("apple") < Scalar("Banana") # String scalars - note case sensitivity
83105
False
84-
85-
Order comparison operators (``<``, ``<=``, ``>``, and ``>=``) raise ``TypeError`` exception
86-
if the combination of types is not supported, such as when comparing
87-
a numeric scalar object and a string scalar object.
88-
89-
Order comparison operators (``<``, ``<=``, ``>``, and ``>=``) raise ``ValueError`` exception
90-
if the units are different.
106+
>>> Scalar(5.0, 'V') < Scalar("5.0", 'V') # Comparison of a numeric and a string scalar
107+
Traceback (most recent call last):
108+
...
109+
TypeError: Comparing Scalar objects of numeric and string types is not permitted.
110+
>>> Scalar(0.5, 'V') < Scalar(500, 'mV') # Numeric scalars with different units
111+
Traceback (most recent call last):
112+
...
113+
ValueError: Comparing Scalar objects with different units is not permitted.
91114
92115
Class Members
93116
^^^^^^^^^^^^^

0 commit comments

Comments
 (0)