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.
- 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 aTypeError
).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)
- util.concat(items: Sequence = [])[source]¶
Concatenate items
items must must be a container or iterable
- 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.extend(q: Queue, items: Sequence)[source]¶
Fill queue q with items, similar to list.extend
Parameters¶
w : queue.Queue or asyncio.Queue
- 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.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.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.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.partial_simple(f: Callable, *args, **kwds)[source]¶
Similar to functools.partial. Can be can be used converts bound methods to functions.
- 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