datastruct#


class Trivalent(value)[source]#

Bases: Enum

An enumeration.

TRUE = 'true'#
FALSE = 'false'#
UNKNOWN = 'unknown'#
classmethod from_bool(b: bool)[source]#
is_true()[source]#
is_false()[source]#
class Maybe(value: Optional[TValue])[source]#

Bases: Generic[TValue]

class DeferredParams[source]#

Bases: ToStringMixin

Represents a dictionary of parameters that is specifically designed to hold parameters that can only defined late within a process (i.e. not initially at construction time), e.g. because the parameters are data-dependent and therefore can only be determined once the data has been seen.

UNDEFINED = '__undefined__DeferredParams'#
set_param(name: str, value: Any)[source]#
get_param(name, default='__undefined__DeferredParams')[source]#
Parameters:
  • name – the parameter name

  • default – in case no value is set, return this value, and if UNDEFINED (default), raise KeyError

Returns:

the parameter value

get_dict() Dict[str, Any][source]#
class SortedValues(sorted_values: Sequence[TValue])[source]#

Bases: Generic[TValue]

Provides convenient binary search (bisection) operations for sorted sequences

floor_index(value) Optional[int][source]#

Finds the rightmost index where the value is less than or equal to the given value

Parameters:

value – the value to search for

Returns:

the index or None if there is no such index

ceil_index(value) Optional[int][source]#

Finds the leftmost index where the value is greater than or equal to the given value

Parameters:

value – the value to search for

Returns:

the index or None if there is no such index

closest_index(value) Optional[int][source]#

Finds the index of the value that is closest to the given value. If two subsequent values have the same distance, the smaller index is returned.

Parameters:

value – the value to search for

Returns:

the index or None if this object is empty

floor_value(value) Optional[TValue][source]#

Finds the largest value that is less than or equal to the given value

Parameters:

value – the value to search for

Returns:

the value or None if there is no such value

ceil_value(value) Optional[TValue][source]#

Finds the smallest value that is greater than or equal to the given value

Parameters:

value – the value to search for

Returns:

the value or None if there is no such value

closest_value(value) Optional[TValue][source]#

Finds the value that is closest to the given value. If two subsequent values have the same distance, the smaller value is returned.

Parameters:

value – the value to search for

Returns:

the value or None if this object is empty

value_slice(lowest_key, highest_key) Optional[Sequence[TValue]][source]#
class SortedKeyValueStructure(*args, **kwds)[source]#

Bases: Generic[TKey, TValue], ABC

abstract floor_index(key: TKey) Optional[int][source]#

Finds the rightmost index where the key value is less than or equal to the given value

Parameters:

key – the value to search for

Returns:

the index or None if there is no such index

abstract ceil_index(key: TKey) Optional[int][source]#

Finds the leftmost index where the key value is greater than or equal to the given value

Parameters:

key – the value to search for

Returns:

the index or None if there is no such index

abstract closest_index(key: TKey) Optional[int][source]#

Finds the index where the key is closest to the given value. If two subsequent keys have the same distance, the smaller index is returned.

Parameters:

key – the value to search for

Returns:

the index or None if this object is empty.

abstract floor_value(key: TKey) Optional[TValue][source]#

Returns the value for the largest index where the corresponding key is less than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

abstract ceil_value(key: TKey) Optional[TValue][source]#

Returns the value for the smallest index where the corresponding key is greater than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

abstract closest_value(key: TKey) Optional[TValue][source]#

Finds the value that is closest to the given value. If two subsequent values have the same distance, the smaller value is returned.

Parameters:

key – the key to search for

Returns:

the value or None if this object is empty

abstract floor_key_and_value(key: TKey) Optional[Tuple[TKey, TValue]][source]#
abstract ceil_key_and_value(key: TKey) Optional[Tuple[TKey, TValue]][source]#
abstract closest_key_and_value(key: TKey) Optional[Tuple[TKey, TValue]][source]#
interpolated_value(key: TKey) Optional[TValue][source]#

Computes a linearly interpolated value for the given key - based on the two closest key-value pairs found in the data structure. If the key is found in the data structure, the corresponding value is directly returned.

NOTE: This operation is supported only for value types that support the required arithmetic operations.

Parameters:

key – the key for which the interpolated value is to be computed.

Returns:

the interpolated value or None if the data structure does not contain floor/ceil entries for the given key

slice(lower_bound_key=None, upper_bound_key=None, inner=True) TSortedKeyValueStructure[source]#
Parameters:
  • lower_bound_key – the key defining the start of the slice (depending on inner); if None, the first included entry will be the very first entry

  • upper_bound_key – the key defining the end of the slice (depending on inner); if None, the last included entry will be the very last entry

  • inner – if True, the returned slice will be within the bounds; if False, the returned slice is extended by one entry in both directions such that it contains the bounds (where possible)

Returns:

class SortedKeysAndValues(keys: Sequence[TKey], values: Sequence[TValue])[source]#

Bases: Generic[TKey, TValue], SortedKeyValueStructure[TKey, TValue]

Parameters:
  • keys – a sorted sequence of keys

  • values – a sequence of corresponding values

classmethod from_series(s: pd.Series)[source]#

Creates an instance from a pandas Series, using the series’ index as the keys and its values as the values

Parameters:

s – the series

Returns:

an instance

floor_index(key) Optional[int][source]#

Finds the rightmost index where the key value is less than or equal to the given value

Parameters:

key – the value to search for

Returns:

the index or None if there is no such index

ceil_index(key) Optional[int][source]#

Finds the leftmost index where the key value is greater than or equal to the given value

Parameters:

key – the value to search for

Returns:

the index or None if there is no such index

closest_index(key) Optional[int][source]#

Finds the index where the key is closest to the given value. If two subsequent keys have the same distance, the smaller index is returned.

Parameters:

key – the value to search for

Returns:

the index or None if this object is empty.

floor_value(key) Optional[TValue][source]#

Returns the value for the largest index where the corresponding key is less than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

ceil_value(key) Optional[TValue][source]#

Returns the value for the smallest index where the corresponding key is greater than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

closest_value(key) Optional[TValue][source]#

Finds the value that is closest to the given value. If two subsequent values have the same distance, the smaller value is returned.

Parameters:

key – the key to search for

Returns:

the value or None if this object is empty

floor_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
ceil_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
closest_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
value_slice_inner(lower_bound_key, upper_bound_key)[source]#
value_slice_outer(lower_bound_key, upper_bound_key, fallback=False)[source]#
class SortedKeyValuePairs(sorted_key_value_pairs: Sequence[Tuple[TKey, TValue]])[source]#

Bases: Generic[TKey, TValue], SortedKeyValueStructure[TKey, TValue]

classmethod from_unsorted_key_value_pairs(unsorted_key_value_pairs: Sequence[Tuple[TKey, TValue]])[source]#
value_for_index(idx: int) TValue[source]#
key_for_index(idx: int) TKey[source]#
floor_index(key) Optional[int][source]#

Finds the rightmost index where the key is less than or equal to the given key

floor_value(key) Optional[TValue][source]#

Returns the value for the largest index where the corresponding key is less than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

floor_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
ceil_index(key) Optional[int][source]#

Find leftmost index where the key is greater than or equal to the given key

ceil_value(key) Optional[TValue][source]#

Returns the value for the smallest index where the corresponding key is greater than or equal to the given value

Parameters:

key – the key to search for

Returns:

the value or None if there is no such value

ceil_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
closest_index(key) Optional[int][source]#

Finds the index where the key is closest to the given value. If two subsequent keys have the same distance, the smaller index is returned.

Parameters:

key – the value to search for

Returns:

the index or None if this object is empty.

closest_value(key) Optional[TValue][source]#

Finds the value that is closest to the given value. If two subsequent values have the same distance, the smaller value is returned.

Parameters:

key – the key to search for

Returns:

the value or None if this object is empty

closest_key_and_value(key) Optional[Tuple[TKey, TValue]][source]#
value_slice(lowest_key, highest_key) Optional[Sequence[TValue]][source]#