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 : |
|
---|
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. |
Return a deep copy.
Equivalent to copy.deepcopy(c).
Returns : |
|
---|
Examples
>>> c.copy()
Return a string containing a full description of the instance.
Parameters : |
|
---|---|
Returns : |
|
Examples
>>> x = c.dump()
>>> print c.dump()
>>> print c.dump(id='comparison1')
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 : |
|
---|---|
Returns : |
|
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)
Examples
>>> c.operator = 'eq'
>>> c.operator
'eq'
>>> del c.operator
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