Rudimentary data transforms and algorithms

satella.coding.transforms.unpack_dict(dct: ~typing.Dict[~satella.coding.typing.K, ~satella.coding.typing.V], *args: ~satella.coding.typing.K, map_through: ~typing.Callable[[~satella.coding.typing.V], ~satella.coding.typing.V] = <function <lambda>>, raise_if_not_found: bool = True) Iterator[V]

Unpack a dictionary by accessing it’s given keys in parallel.


>>> a, b, c = unpack_dict({1:2, 2:3, 4:5}, 1, 2, 4)
>>> assert a == 2 and b == 3 and c == 5
  • dct – dictionary to unpack

  • args – keys in this dictionary

  • map_through – a keyword argument, callable that will be called with each value returned and the result of this callable will be returned

  • raise_if_not_found – a KeyError will be returned upon encountering a key that does not exist. If set to False, a None will be returned.


an iterator


KeyError – a key was not found

satella.coding.transforms.is_subset(subset: Dict, superset: Dict) bool

Does superset contain all keys of subset, and are their values equal?

  • subset – the set that contains all the keys

  • superset – the set that is to contain all the keys in subset, and their values have to be equal


does the condition hold?

class satella.coding.transforms.merge_list(*lists: Iterator[Tuple[K, V]], merge_function: Callable[[V, V], V])

Merge two sorted lists.

This is an iterator which consumes elements as they are required.

Each list must be of type tuple/2 with the first element being the key. The list has to be sorted by this value, ascending.

When the algorithm encounters two identical keys, it calls merge_function on it’s result and inserts the result.

  • lists – lists to sort

  • merge_function – a callable that accepts two pieces of the tuple and returns a result


an resulting iterator

satella.coding.transforms.hashables_to_int(words: List[K]) Dict[K, int]

Assign each hashable an integer, starting from 0, and return the resulting mapping


words – a list of hashables


a dictionary keyed by hashable and values are the assigned integers

satella.coding.transforms.linear_interpolate(series: Sequence[Tuple[K, U]], t: K, clip: bool = False) U

Given a sorted (ascending) series of (t_value, y_value) interpolating linearly a function of y=f(t) compute a linear approximation of f at t of two closest values.

t must be larger or equal to t_min and smaller or equal to t_max

  • series – series of (t, y) sorted by t ascending

  • t – t to compute the value for

  • clip – if set to True, then values t: t<t_min f(t_min) will be returned and for values t: t>t_max f(t_max) will be returned


return value


ValueError – t was smaller than t_min or greater than t_max

satella.coding.transforms.intify(v: Any) int

Attempt to convert v to an int.

None will be converted to 0.

Any object will have int() called on it.

Failing that, it’s length will be taken.

Failing that, ValueError will be raised


v – parameter


int representation

satella.coding.transforms.jsonify(data: Any) str | int | float | list | dict | None

Convert any data to a value that’s serializable via JSON.

Objects that are JSONAble will have their to_json() method called.

Note that enums will be converted to their value.

As a last resort, str() will be called on the object, and if that fails it will have repr() called on it


data – data to convert to a jsonable


JSON-able data

satella.coding.transforms.percentile(n: List[float], percent: float) float

Find the percentile of a list of values.

  • n

    • is a list of values. Note this MUST BE already sorted.

  • percent

    • a float value from 0.0 to 1.0.


the percentile of the values

satella.coding.transforms.b64encode(content: bytes) str

Syntactic sugar for:

>>> import base64
>>> y = base64.b64encode(content).decode('utf-8')

Since base64.b64decode(str) returns bytes, the reverse is not provided.


content – content to encode


content encoded as a string


satella.coding.transforms.pad_to_multiple_of_length(seq: Appendable[T], multiple_of: int, pad_with: T | None = None, pad_with_factory: Callable[[], T] | None = None) Appendable[T]

Make sequence multiple of length, ie. append elements to the sequence until it’s length is a multiple of multiple_of.

  • seq – sequence to lengthify

  • multiple_of – sequence returned will be a multiple of this length.

  • pad_with – argument with which to pad the sequence

  • pad_with_factory – a callable/0 that returns an element with which to pad the sequence


a list with elements


satella.coding.transforms.split_shuffle_and_join(entries: ~typing.List[~satella.coding.typing.T], whether_to_shuffle: ~typing.Callable[[~satella.coding.typing.T], bool] = <function <lambda>>, not_shuffled_to_front: bool = True) List[T]

Split elements in entries into two groups - one group, called True, is the one for which whether_to_shuffle(elem) is True, the other is False.

Shuffle the group True.

If not_shuffled_to_front, elements in the group False will go at the beginning of the returned list, after which will go elements shuffled. If it’s False, the not-shuffled elements will be at the back of the list.

Order of the not shuffled elements will be preserved.

  • entries – list of elements

  • whether_to_shuffle – a decider to which group does given element belong?

  • not_shuffled_to_front – if True then not shuffled elements will be put before shuffled, else the not shuffled elements will be at the back of the list


list altered to specification


satella.coding.transforms.one_tuple(x: Iterable[T]) Iterator[Tuple[T]]

Change a sequence of iterables into a sequence that displays each element as a part of one-element tuple. Essentially syntactic sugar for:

>>> for z in x:
>>>     yield z,

x – sequence to tupleify


a iterator of one-element tuples


Make both keys and values (if dict), values (if list) or make an object a string by passing them through stringify function.

satella.coding.transforms.stringify(obj: ~typing.Any, stringifier: ~typing.Callable[[~typing.Any], str] = <class 'str'>, recursively: bool = False, str_none: bool = False) List[str] | Dict[str, str] | str

Stringify all object:

ie. if a dict, put every item and key (if a dict is given) through stringify.

if a list, put every item through stringify else just call stringify on it.

Note that if you use recursively, then dicts and lists are allowed to be valid elements of the returned representation!

Note that enums will be converted to their labels. eg:

>>> class Enum(enum.Enum):
>>>     A = 0
>>> assert stringify(Enum.A) == 'A'
  • obj – a list or a dict

  • stringifier – function that accepts any arguments and returns a string representation

  • recursively – whether to recursively stringify elements, ie. stringify will be called on all the children

  • str_none – whether to return None if given a None. If True, “None” will be returned instead


stringified object


satella.coding.transforms.clip(v: int | float, minimum: int | float, maximum: int | float) int | float

Clip v so it conforms to minimum <= v <= maximum

  • v – value to clip

  • minimum – minimum

  • maximum – maximum


clipped value


class satella.coding.transforms.merge_series(*series: Iterator[Tuple[float, Any]])

A merger for multiple sequences that return (timestamp, value).

This will behave as a single-use iterator and return (timestamp, value1, value2, …)


ValueError – one of the given series was empty

advance(i: int) None

i – timestamp to advance to


ValueError – given series was empty

assert_have_timestamps() None

Assert that self.timestamps is not empty, or raise StopIteration if it can’t be filled in

assert_preloaded(for_ts: int) bool

Assert every next preloaded value can at least report for for_ts


for_ts – timestamp to report for


whether every value can report for for_ts