Bases: object
Create an object for storing a comparison expression.
A comparison expression comprises a relation (such as ‘less than’) and a value (any object capable of being related to with the standard comparison operators). These are stored in the relation and value attributes respectively.
The comparison is evaluated for an arbitrary object with the evaluate() method.
Valid relations are:
Relation | Description |
---|---|
lt | Strictly less than |
le | Less than or equal |
gt | Strictly greater than |
ge | Greater than or equal |
eq | Equals (within a tolerance) |
ne | Not equal to (within a tolerance) |
inside | Inside a given range (range bounds included) |
outside | Outside a given range (range bounds excluded) |
See also
As a convenience, for each relation in the above list there is an identically named function which returns the appropriate Comparison object.
Parameters: | **kwargs – A keyword argument to the call is the relation of the comparison (any one of the relations listed above). The keyword’s argument is the object to test the comparison against. If the keyword argument is “inside” or “outside” then the value should be a two-element list or tuple. |
---|
Attribute | (type) Description |
---|---|
relation | (str) A string definging the comparison operator. One of relations given above. |
value | (object) The object with which to compare. |
Method | Description |
---|---|
dump() | Return a string containing a full description of the object. |
evaluate() | Evaluate the comparison for a given variable. |
Examples
>>> c = cf.Comparison(le = 5)
>>> c.evaluate(4)
True
>>> c.evaluate(5)
True
>>> c = lt(5)
>>> c.evaluate(4)
True
>>> c = cf.Comparison(inside = (1,2))
>>> a = numpy.arange(4)
>>> c.evaluate(a)
array([False, True, True, False], dtype=bool)
>>> (a >= 1) & (a <= 2)
array([False, True, True, False], dtype=bool)
Evaluate the comparison for a given object.
Parameters: | x (object) – An object. |
---|---|
Returns: | Typically a boolean or array of booleans. More generally, the output from the appropriate relation operators (==, !=, <, >, <=, >=) for the given variable, x. |