cf.Comparison

class cf.Comparison(operator, value, units=None)

Bases: object

Create an object for storing a comparison operation.

The comparison operation is an operator with a right hand side operand, such as ‘less than 3’ and ‘is a member of set(1, 3, 4)’. This comparison operation may be evaluated for an arbitrary left hand side operand, the result being dependent on its type. For example, if the comparison operation is ‘equals 2’ then if this is evaluated for a left hand side of 2 then True is returned and if it is evaluated for a left hand side of numpy.array([0, 1, 2] then numpy.array([False, False, True]) is returned.

The comparison operation is evaluated for an arbitrary left hand side operand with the evaluate method or, equivalently, the == operator. The != operator is also available.

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:

Operation Description
‘lt’ Is strictly less than
‘le’ Is less than or equal to
‘gt’ Is strictly greater than
‘ge’ Is greater than or equal to
‘eq’ Is equal to
‘ne’ Is not equal to
‘wi’ Is within a given range (range bounds included)
‘wo’ Is without a given range (range bounds excluded)
‘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.

As a convenience, for each operator in the above list there is an identically named constructor function which returns the appropriate Comparison object.

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]

Initialization

Parameters :
operator : str

The comparison operator.

value : object

The value on the right hand side of the comparison operation.

units : str or Units, optional

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

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.
copy()

Return a deep copy.

Equivalent to copy.deepcopy(c).

Returns :
out :

The deep copy.

Examples

>>> c.copy()
dump(id=None)

Return a string containing a full description of the instance.

Parameters :
id : str, optional

Set the common prefix of component names. By default the instance’s class name is used.

Returns :
out : str

A string containing the description.

Examples

>>> x = c.dump()
>>> print c.dump()
>>> print c.dump(id='comparison1')
evaluate(x)

Evaluate the comparison operation for a given object.

Note that x==c is equivalent to c.evaluate(x) and x!=c is equivalent to c.evaluate(x)==False.

Parameters :
x : object

The object for the left hand side operand of the comparison operation.

Returns :
out :

The result of the operation given by the operator attribute with x as its left hand side operand and the value attribute as its right hand side operand.

Examples

>>> c = cf.Comparison('lt', 5.5)
>>> c.evaluate(6)
False
>>> c = cf.Comparison('wi', (1,2))
>>> array = numpy.arange(4)
>>> array
array([0, 1, 2, 3])
>>> c.evaluate(array)
array([False,  True,  True, False], dtype=bool)
operator = None

Examples

>>> c.operator = 'eq'
>>> c.operator
'eq'
>>> del c.operator
value = None

Examples

>>> c.value = 'Atlantic'
>>> c.value = 2.0
>>> c.value = cf.Data(2.0)
>>> c.value = cf.Data(2.0, units='m')
>>> c.value = [1, 2, 4]
>>> c.value = (1, 2, 4)
>>> c.value = set([1, 2, 4])
>>> c.value
set([1, 2, 4])
>>> del c.value

Previous topic

cf.Units

Next topic

cf.CoordinateList

This Page