know.util

Util objects

class know.util.FixedStepHunker(src, chk_size, chk_step=None, start_idx=0, end_idx=None)[source]
exception know.util.IteratorExit[source]

Raised when an iterator should quit being iterated on, signaling this event any process that cares to catch the signal. We chose to inherit directly from BaseException instead of Exception for the same reason that GeneratorExit does: Because it’s not technically an error.

See: https://docs.python.org/3/library/exceptions.html#GeneratorExit

class know.util.LiveProcess(streams: Dict[str, Iterable], slab_callback: Callable[[Slab], Any] = <built-in function print>, walk: Callable = <class 'know.util.DictZip'>)[source]
slab_callback()

print(value, …, sep=’ ‘, end=’n’, file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

class know.util.MultiIterable(*unnamed, stop_condition: Callable[[Any], bool] = <function always_false>, **named)[source]

Join several iterables together.

from know.util import any_value_is_none from functools import partial

any_value_is_none = lambda d: any(d[k] is None for k in d) mk_multi_iterable = partial(MultiIterable, stop_condition=any_value_is_none) mi = mk_multi_iterable(lets=’abc’, nums=[1, 2, 3, 4]) list(mi) [{‘lets’: ‘a’, ‘nums’: 1}, {‘lets’: ‘b’, ‘nums’: 2}, {‘lets’: ‘c’, ‘nums’: 3}]

mi = MultiIterable( … x=[5, 4, 3, 2, 1], y=[1, 2, 3, 4, 5], … stop_condition=lambda d: d[‘x’] == d[‘y’] … ) list(mi) [{‘x’: 5, ‘y’: 1}, {‘x’: 4, ‘y’: 2}]

takewhile(predicate=None)[source]

itertools.takewhile applied to self, with a bit of syntactic sugar There’s nothing to stop the iteration

class know.util.Slabbing(slabs: Iterable[Slab], slab_callback: Callable[[Slab], Any])[source]
class know.util.SlabsPush(slabs: Iterable[Slab], services: Mapping[str, Consumer])[source]
class know.util.SlabsPushTuple(slabs: Iterable[Slab], services: Mapping[str, Consumer])[source]
know.util.always_false(x: Any) → False[source]

Returns False, regardless of input. Meant for stopping (filter) functions

know.util.always_true(x: Any) → True[source]

Returns True, regardless of input. Meant for filter functions.

know.util.any_value_is_none(d: Mapping)[source]

Returns True if any value of the mapping is None