util module

class util.DataClassHelper(data: dataclass)[source]

Bases: object

Methods that mutate dataclass fields. Keep track of dependent fields, and ask for user input to fill them in.

ensure_field(key: str)[source]
set_field(key: str)[source]
verify_context_key(key)[source]
class util.Encoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
serialize_dataclass(obj: dataclass)[source]
util.accumulate_list(items: Iterable[T]) Iterable[List[T]][source]
util.append_list(a: list, b) list[source]
util.call(f, *_)[source]

Call f and ignore all other arguments

util.concat(items: Sequence = [])[source]

Concatenate items

items must must be a container or iterable

util.concat_empty_container(items)[source]
util.constant(value)[source]

Returns a constant function

util.crop(s: str, n=100, suffix='..') str[source]
util.dataclass_to_string(data)[source]

Convert a dataclass to a string.

util.decorate(decoratee: dataclass, cls: object)[source]

Adapt a class instance to have an hasA and isA relationships with cls. See https://en.wikipedia.org/wiki/Decorator_pattern

util.deserialize_dataclass(obj: dict)[source]
util.deserialize_dataclasses(obj: dict)[source]
util.equals(*args)[source]

Return True if args are equal to each other.

util.extend(q: Queue, items: Sequence)[source]

Fill queue q with items, similar to list.extend

Parameters

w : queue.Queue or asyncio.Queue

util.extract_exception()[source]
except AssertionError:

print(extract_exception())

util.find_fuzzy_matches(element: str, elements: List[str])[source]

Yield elements that are most similar. Similarity is based on the Levenshtein edit-distance.

util.find_prefix_matches(element: str, elements: MappingView[str])[source]

Yields all elements that are equal to a prefix of element. Elements with better matches are chosen first.

Raise a ValueError when no matches are found.

util.first(*values)[source]
util.flip(f)[source]

Similar to functools.partial, but flip the arguments

util.for_all(foreach_items: Sequence, predicate: Callable, *args, **kwds) bool[source]

Evaluate whether all item satisfy predicate(*args, item)

util.for_any(foreach_items: Sequence, predicate: Callable, *args, **kwds) bool[source]

Evaluate whether any item satisfies predicate(*args, item)

util.for_each(foreach_items: Sequence, predicate: Callable, *args, **kwds) Iterable[source]
util.glob(value: str, options: List[str] = [], strict=False) Iterable[str][source]

Filter items based on Unix shell-style wildcards E.g.

w?ldcard
[a-e]*
ranges_{1..3}
options_{a,b,c}
util.group(items: Iterable[T], n: int) Iterable[List[T]][source]

Group items by chunks of size n. I.e. a lazy version of itertools.pairwise with variable groupsize.

util.hamming(a: str, b: str) float[source]

Approximate the Hamming distance of two strings.

util.has_annotations(cls: type) bool[source]
util.has_method(cls, method) bool[source]
util.identity(*values)[source]
util.infer_dependencies(known_deps: Dict[str, List[str]], key: str)[source]
util.infer_inner_cls(cls=typing.Dict[str, str])[source]
util.is_Dict(cls)[source]
util.is_Dict_or_List(cls)[source]
util.is_List(cls)[source]
util.is_alpha(key: str, ignore=[]) bool[source]
util.is_alphanumerical(key: str, ignore=[]) bool[source]
util.is_callable(method) bool[source]
util.is_digit(s: str) bool[source]
util.is_enum(cls: type) bool[source]
util.is_globbable(value: str) bool[source]
util.is_valid_method_name(value: str) bool[source]
util.lazy_map(func, func_args, generator: ~typing.Callable[[~typing.Any], ~typing.Iterable], post_func=<function identity>)[source]

Apply func to the result of generator(). Apply post_func afterwards.

util.list_prefix_matches(element: str, elements: List[str])[source]

Yields all elements that are equal to a prefix of element. Elements with better matches are chosen first.

util.match_words(s: str, prefix='') List[str][source]

Match a words that: - starts with a letter - contains exclusively alphanumerical chars and underscores

An optional prefix can be added, e.g. a delimiter.

util.none(*_)[source]

Do nothing

util.not_equals(*args)[source]

Return True if not all args are equal to each other.

util.omit_prefixes(items: Sequence[T], prefixes: Sequence[T]) Iterable[T][source]
util.partial_no_args(f, *args)[source]

Similar to functools.partial

util.partial_simple(f: Callable, *args, **kwds)[source]

Similar to functools.partial. Can be can be used converts bound methods to functions.

util.quote(item: str, ignore=[]) str[source]
util.quote_all(items: List[str], ignore=[]) Iterable[str][source]
util.removeprefix(s: str, prefix: str) str[source]
util.rename(func, new_name: str)[source]
util.split(line: str, delimiters=',.')[source]
util.split_prefixes(items: Sequence[T], prefixes: Sequence[T]) Iterable[T][source]
util.split_sequence(items: Sequence[T], delimiters: Sequence[T] = ['\n', ';'], return_delimiters: bool | str = False, prefixes=[]) Generator[List[T], None, None][source]

An abstraction of list.split. Multiple delimiters are supported.

Paramters

return_delimitersbool | ‘always’

prefix yielded items with the delimiters that were encountered. See [polish notation](https://en.wikipedia.org/wiki/Polish_notation) If ‘always’, then include left-hand side delimiters.

util.split_tips(line: str, delimiters: str = ',.') Generator[str, None, None][source]

Split line based on delimiters.

util.take(values: Iterable[T], n: int) List[T][source]

Return the first n items https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#v:take

util.translate_items(terms: Iterable[str], translations: dict)[source]

Iterate over items and return any translations found.

util.use_recursion_limit(limit=100)[source]