pg.lt

Accessible via pg.lt, pg.symbolic.lt.

lt(left, right)[source]

Returns True if a value is symbolically less than the other value.

Symbolic values are comparable by their symbolic representations. For common types such as numbers and string, symbolic comparison returns the same value as value comparisons. For example:

assert pg.lt(False, True) == Flase < True
assert pg.lt(0.1, 1) == 0.1 < 1
assert pg.lt('a', 'ab') == 'a' < 'ab'

However, symbolic comparison can be applied on hierarchical values, for example:

assert pg.lt(['a'], ['a', 'b'])
assert pg.lt(['a', 'b', 'c'], ['b'])
assert pg.lt({'x': 1}, {'x': 2})
assert pg.lt({'x': 1}, {'y': 1})
assert pg.lt(A(x=1), A(x=2))

Also, symbolic values of different types can be compared, for example:

assert pg.lt(pg.MISSING_VALUE, None)
assert pg.lt(None, 1)
assert pg.lt(1, 'abc')
assert pg.lt('abc', [])
assert pg.lt([], {})
assert pg.lt([], A(x=1))

The high-level idea is that a value with lower information entropy is less than a value with higher information entropy. As a result, we know that pg.MISSING_VALUE is the smallest among all values.

The order of symbolic representation are defined by the following rules: :rtype: bool

  1. If x and y are comparable by their values, they will be compared using operator <. (e.g. bool, int, float, str)

  2. If x and y are not directly comparable and are different in their types, they will be compared based on their types. The order of different types are: pg.MISSING_VALUE, NoneType, bool, int, float, str, list, tuple, set, dict, functions/classes. When different functions/classes compare, their order is determined by their qualified name.

  3. If x and y are of the same type, which are symbolic containers (e.g. list, dict, pg.Symbolic objects), their order will be determined by the order of their first sub-nodes which are different. Therefore [‘b’] is greater than [‘a’, ‘b’], though the later have 2 elements.

  4. Non-symbolic classes can define method sym_lt to enable symbolic comparison.

Parameters:
  • left – The left-hand value to compare.

  • right – The right-hand value to compare.

Returns:

True if the left value is symbolically less than the right value.