cf.Comparison

class cf.Comparison(**kwargs)

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.

Special attributes:

Attribute Description
relation A string definging the comparison operator. One of relations given above.
value The object with which to compare.

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)

Methods and attributes defined here:

dump(id=None, omit=())

Return a string containing a full description of the object.

Parameters:
  • id (str) – Optional. Set the common prefix of component names. If None then defaults to the class name.
  • omit (sequence) – Optional. Omit the given attributes from the description.
Returns:

A string containing the description.

See also

cf.dump

evaluate(x)

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.

Examples:

>>> c = cf.Comparison(lt = 5.5)
>>> c.evaluate(6)
False
>>> c = cf.Comparison(inside = (1,2))
>>> array = numpy.arange(4)
>>> c.evaluate(array)
array([False,  True,  True, False], dtype=bool)

Previous topic

cf.SpaceList

Next topic

Functions

This Page