cf.Comparison

class cf.Comparison(operator, value, units=None, regex=False)[source]

Bases: object

A cmparison operation.

The comparison operation is an operator with a right hand side operand. For example, “strictly less than 3” or “is a member of the set (1, 3, 4)”. Such a comparison operation may be evaluated for an arbitrary left hand side operand with the evaluate method, the result being dependent on the left hand side operand’s type.

The right hand side operand is stored in the value attribute and the operator is stored in the operator attribute and may be any one of the following:

operator Description Constructor function
'lt' Is strictly less than cf.lt
'le' Is less than or equal to cf.le
'gt' Is strictly greater than cf.gt
'ge' Is greater than or equal to cf.ge
'eq' Is equal to cf.eq
'ne' Is not equal to cf.ne
'wi' Is within a given range (range bounds included) cf.wi
'wo' Is without a given range (range bounds excluded) cf.wo
'set' Is a member of a collection of one or more objects. If the left hand side is an iterable which also supports broadcasting then each element is tested for membership independently. cf.set

As a convenience, for each operator in the above list there is an identically named constructor function which returns the appropriate cf.Comparison object. For example, c = cf.Comparison('lt', 5) is equivalent to c = cf.lt(5)

“Equal” and “not equal” comparisons may use the == and != operators respectively, instead of the evaluate method.

Examples

>>> c = cf.Comparison('le', 5)
>>> c.evaluate(4)
True
>>> 5 == c
True
>>> 5 != c
False
>>> 6 != c
True
>>> c = lt(5)
>>> c.evaluate(4)
True
>>> c = cf.Comparison('wi', (1,2))
>>> a = numpy.arange(4)
>>> print c.evaluate(a)
[False True True False]
>>> print a != c
[True False False True]
>>> c = cf.set((1,2))
>>> 2 == c
True
>>> 3 == c
False
>>> print numpy.arange(4) == c
[False True True False]
>>> cf.Comparison('eq', 2).evaluate(2)
True
>>> 2 == cf.Comparison('eq', 2)
True
>>> print cf.Comparison('eq', 2).evaluate(numpy.array([0, 1, 2]))
[False, False, True]
>>> print cf.Comparison('eq', 2) == numpy.array([0, 1, 2])
[False, False, True]
>>> print cf.Comparison('le', 1.5).evaluate(numpy.array([0, 1, 2]))
[True, True, False]
>>> print cf.Comparison('eq', 2) != 2
False
>>> print numpy.array([0, 1, 2]) != cf.Comparison('eq', 2)
[True, True, False]

Initialization

Parameters :
operator : str

The comparison operator.

value : object

The right hand side of the comparison operation.

units : str or cf.Units, optional

If applicable, the units of value. By default, the same units as the left hand side of the comparison operation are assumed.

regex : str, optional

Comparison attributes

Comparison.operator
Comparison.value

Comparison methods

copy Return a deep copy.
dump Return a string containing a full description of the instance.
evaluate Evaluate the comparison operation for a given object.

Table Of Contents

Previous topic

cf.Units.conform

This Page