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.
Example:
>>> a, b, c = unpack_dict({1:2, 2:3, 4:5}, 1, 2, 4) >>> assert a == 2 and b == 3 and c == 5
- Parameters:
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.
- Returns:
an iterator
- Raises:
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?
- Parameters:
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
- Returns:
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.
- Parameters:
lists – lists to sort
merge_function – a callable that accepts two pieces of the tuple and returns a result
- Returns:
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
- Parameters:
words – a list of hashables
- Returns:
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
- Parameters:
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
- Returns:
return value
- Raises:
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
- Parameters:
v – parameter
- Returns:
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
- Parameters:
data – data to convert to a jsonable
- Returns:
JSON-able data
- satella.coding.transforms.percentile(n: List[float], percent: float) float ¶
Find the percentile of a list of values.
- Parameters:
n –
is a list of values. Note this MUST BE already sorted.
percent –
a float value from 0.0 to 1.0.
- Returns:
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.
- Parameters:
content – content to encode
- Returns:
content encoded as a string
pad_to_multiple_of_length¶
- 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.
- Parameters:
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
- Returns:
a list with elements
split_shuffle_and_join¶
- 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.
- Parameters:
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
- Returns:
list altered to specification
one_tuple¶
- 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,
- Parameters:
x – sequence to tupleify
- Returns:
a iterator of one-element tuples
stringify¶
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'
- Parameters:
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
- Returns:
stringified object
clip¶
- satella.coding.transforms.clip(v: int | float, minimum: int | float, maximum: int | float) int | float ¶
Clip v so it conforms to minimum <= v <= maximum
- Parameters:
v – value to clip
minimum – minimum
maximum – maximum
- Returns:
clipped value
merge_series¶
- 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, …)
- Raises:
ValueError – one of the given series was empty
- advance(i: int) None ¶
- Parameters:
i – timestamp to advance to
- Raises:
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
- Parameters:
for_ts – timestamp to report for
- Returns:
whether every value can report for for_ts