satella.coding.transforms package¶
Submodules¶
satella.coding.transforms.base64 module¶
- satella.coding.transforms.base64.b64encode(content)¶
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 (bytes) – content to encode
- Returns:
content encoded as a string
- Return type:
str
satella.coding.transforms.interpol module¶
- satella.coding.transforms.interpol.linear_interpolate(series, t, clip=False)¶
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 (Sequence[Tuple[K, U]]) – series of (t, y) sorted by t ascending
t (K) – t to compute the value for
clip (bool) – 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
- Return type:
U
satella.coding.transforms.jsonify module¶
- satella.coding.transforms.jsonify.jsonify(data)¶
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 (Any) – data to convert to a jsonable
- Returns:
JSON-able data
- Return type:
Optional[Union[str, int, float, list, dict]]
satella.coding.transforms.merge_list module¶
- class satella.coding.transforms.merge_list.merge_list(*lists, merge_function)¶
Bases:
Iterator
[Tuple
[K
,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 (Iterator[Tuple[K, V]]) – lists to sort
merge_function (Callable[[V, V], V]) – a callable that accepts two pieces of the tuple and returns a result
- Returns:
an resulting iterator
- available_lists¶
- closed¶
- heap¶
- i¶
- its¶
- k¶
- merge_func¶
- pop()¶
- v¶
satella.coding.transforms.merger module¶
- class satella.coding.transforms.merger.merge_series(*series)¶
Bases:
object
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
- Parameters:
series (Iterator[Tuple[float, Any]]) –
- advance(i)¶
- Parameters:
i (int) – timestamp to advance to
- Raises:
ValueError – given series was empty
- Return type:
None
- assert_have_timestamps()¶
Assert that self.timestamps is not empty, or raise StopIteration if it can’t be filled in
- Return type:
None
- assert_preloaded(for_ts)¶
Assert every next preloaded value can at least report for for_ts
- Parameters:
for_ts (int) – timestamp to report for
- Returns:
whether every value can report for for_ts
- Return type:
bool
- empty¶
- next()¶
- Return type:
None
- next_preloaded_values¶
- series¶
- super_next_preloaded_values¶
- timestamps¶
satella.coding.transforms.misc module¶
- satella.coding.transforms.misc.list_values_to_indices(lst)¶
Transform a list of entries into a dict mapping where given entry can be found.
Example:
>>> a = ['abc', 'def', 'ghi'] >>> b = list_values_to_indices(a) >>> assert b == {'abc': 0, 'def': 1, 'ghi': 2}
- Parameters:
lst (List[V]) – list to process. Take care for the list to be composed of unique entries.
- Raises:
ValueError – item was found more than once
- Return type:
Dict[V, int]
satella.coding.transforms.percentile module¶
- satella.coding.transforms.percentile.percentile(n, percent)¶
Find the percentile of a list of values.
- Parameters:
n (List[float]) –
is a list of values. Note this MUST BE already sorted.
percent (float) –
a float value from 0.0 to 1.0.
- Returns:
the percentile of the values
- Return type:
float
satella.coding.transforms.predicates module¶
- satella.coding.transforms.predicates.is_subset(subset, superset)¶
Does superset contain all keys of subset, and are their values equal?
- Parameters:
subset (Dict) – the set that contains all the keys
superset (Dict) – the set that is to contain all the keys in subset, and their values have to be equal
- Returns:
does the condition hold?
- Return type:
bool
satella.coding.transforms.words module¶
- satella.coding.transforms.words.hashables_to_int(words)¶
Assign each hashable an integer, starting from 0, and return the resulting mapping
- Parameters:
words (List[K]) – a list of hashables
- Returns:
a dictionary keyed by hashable and values are the assigned integers
- Return type:
Dict[K, int]
Module contents¶
- satella.coding.transforms.b64encode(content)¶
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 (bytes) – content to encode
- Returns:
content encoded as a string
- Return type:
str
- satella.coding.transforms.clip(v, minimum, maximum)¶
Clip v so it conforms to minimum <= v <= maximum
- Parameters:
v (Union[int, float]) – value to clip
minimum (Union[int, float]) – minimum
maximum (Union[int, float]) – maximum
- Returns:
clipped value
- Return type:
Union[int, float]
- satella.coding.transforms.hashables_to_int(words)¶
Assign each hashable an integer, starting from 0, and return the resulting mapping
- Parameters:
words (List[K]) – a list of hashables
- Returns:
a dictionary keyed by hashable and values are the assigned integers
- Return type:
Dict[K, int]
- satella.coding.transforms.intify(v)¶
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 (Any) – parameter
- Returns:
int representation
- Return type:
int
- satella.coding.transforms.is_subset(subset, superset)¶
Does superset contain all keys of subset, and are their values equal?
- Parameters:
subset (Dict) – the set that contains all the keys
superset (Dict) – the set that is to contain all the keys in subset, and their values have to be equal
- Returns:
does the condition hold?
- Return type:
bool
- satella.coding.transforms.jsonify(data)¶
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 (Any) – data to convert to a jsonable
- Returns:
JSON-able data
- Return type:
Optional[Union[str, int, float, list, dict]]
- satella.coding.transforms.linear_interpolate(series, t, clip=False)¶
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 (Sequence[Tuple[K, U]]) – series of (t, y) sorted by t ascending
t (K) – t to compute the value for
clip (bool) – 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
- Return type:
U
- satella.coding.transforms.list_values_to_indices(lst)¶
Transform a list of entries into a dict mapping where given entry can be found.
Example:
>>> a = ['abc', 'def', 'ghi'] >>> b = list_values_to_indices(a) >>> assert b == {'abc': 0, 'def': 1, 'ghi': 2}
- Parameters:
lst (List[V]) – list to process. Take care for the list to be composed of unique entries.
- Raises:
ValueError – item was found more than once
- Return type:
Dict[V, int]
- class satella.coding.transforms.merge_list(*lists, merge_function)¶
Bases:
Iterator
[Tuple
[K
,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 (Iterator[Tuple[K, V]]) – lists to sort
merge_function (Callable[[V, V], V]) – a callable that accepts two pieces of the tuple and returns a result
- Returns:
an resulting iterator
- available_lists¶
- closed¶
- heap¶
- i¶
- its¶
- k¶
- merge_func¶
- pop()¶
- v¶
- class satella.coding.transforms.merge_series(*series)¶
Bases:
object
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
- Parameters:
series (Iterator[Tuple[float, Any]]) –
- advance(i)¶
- Parameters:
i (int) – timestamp to advance to
- Raises:
ValueError – given series was empty
- Return type:
None
- assert_have_timestamps()¶
Assert that self.timestamps is not empty, or raise StopIteration if it can’t be filled in
- Return type:
None
- assert_preloaded(for_ts)¶
Assert every next preloaded value can at least report for for_ts
- Parameters:
for_ts (int) – timestamp to report for
- Returns:
whether every value can report for for_ts
- Return type:
bool
- empty¶
- next()¶
- Return type:
None
- next_preloaded_values¶
- series¶
- super_next_preloaded_values¶
- timestamps¶
- satella.coding.transforms.none_if_false(y)¶
Return None if y is false, else return y
- Parameters:
y (Any) – value to check
- Returns:
None if y is false, else y
- Return type:
Optional[Any]
- satella.coding.transforms.one_tuple(x)¶
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 (Iterable[T]) – sequence to tupleify
- Returns:
a iterator of one-element tuples
- Return type:
Iterator[Tuple[T]]
- satella.coding.transforms.pad_to_multiple_of_length(seq, multiple_of, pad_with=None, pad_with_factory=None)¶
Make sequence multiple of length, ie. append elements to the sequence until it’s length is a multiple of multiple_of.
- Parameters:
seq (Appendable[T]) – sequence to lengthify
multiple_of (int) – sequence returned will be a multiple of this length.
pad_with (Optional[T]) – argument with which to pad the sequence
pad_with_factory (Optional[Callable[[], T]]) – a callable/0 that returns an element with which to pad the sequence
- Returns:
a list with elements
- Return type:
Appendable[T]
- satella.coding.transforms.percentile(n, percent)¶
Find the percentile of a list of values.
- Parameters:
n (List[float]) –
is a list of values. Note this MUST BE already sorted.
percent (float) –
a float value from 0.0 to 1.0.
- Returns:
the percentile of the values
- Return type:
float
- satella.coding.transforms.split_shuffle_and_join(entries, whether_to_shuffle=<function <lambda>>, not_shuffled_to_front=True)¶
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[T]) – list of elements
whether_to_shuffle (Callable[[T], bool]) – a decider to which group does given element belong?
not_shuffled_to_front (bool) – 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
- Return type:
List[T]
- satella.coding.transforms.stringify(obj, stringifier=<class 'str'>, recursively=False, str_none=False)¶
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 (Any) – a list or a dict
stringifier (Callable[[Any], str]) – function that accepts any arguments and returns a string representation
recursively (bool) – whether to recursively stringify elements, ie. stringify will be called on all the children
str_none (bool) – whether to return None if given a None. If True, “None” will be returned instead
- Returns:
stringified object
- Return type:
Union[List[str], Dict[str, str], str]
- satella.coding.transforms.unpack_dict(dct, *args, map_through=<function <lambda>>, raise_if_not_found=True)¶
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 (Dict[K, V]) – dictionary to unpack
args (K) – keys in this dictionary
map_through (Callable[[V], V]) – 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 (bool) – 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
- Return type:
Iterator[V]