delphi.utils package

Submodules

delphi.utils.fp module

Helper functions for functional programming.

prepend(x: delphi.utils.fp.T, xs: Iterable[delphi.utils.fp.T]) Iterator[delphi.utils.fp.T][source]

Prepend a value to an iterable.

Parameters:
  • x – An element of type T.

  • xs – An iterable of elements of type T.

Returns:

Iterator – An iterator that yields x followed by elements of xs.

Examples

>>> from delphi.utils.fp import prepend
>>> list(prepend(1, [2, 3]))
[1, 2, 3]
append(x: delphi.utils.fp.T, xs: Iterable[delphi.utils.fp.T]) Iterator[delphi.utils.fp.T][source]

Append a value to an iterable.

Parameters:
  • x – An element of type T.

  • xs – An iterable of elements of type T.

Returns:

Iterator – An iterator that yields elements of xs, then yields x.

Examples

>>> from delphi.utils.fp import append
>>> list(append(1, [2, 3]))
[2, 3, 1]
scanl(f: Callable[[delphi.utils.fp.T, delphi.utils.fp.U], delphi.utils.fp.T], x: delphi.utils.fp.T, xs: Iterable[delphi.utils.fp.U]) Iterator[delphi.utils.fp.T][source]

Make an iterator that returns accumulated results of a binary function applied to elements of an iterable.

\[scanl(f, x_0, [x_1, x_2, ...]) = [x_0, f(x_0, x_1), f(f(x_0, x_1), x_2), ...]\]
Parameters:
  • f – A binary function of two arguments of type T.

  • x – An initializer element of type T.

  • xs – An iterable of elements of type T.

Returns:

Iterator – The iterator of accumulated results.

Examples

>>> from delphi.utils.fp import scanl
>>> list(scanl(lambda x, y: x + y, 10, range(5)))
[10, 10, 11, 13, 16, 20]
scanl1(f: Callable[[delphi.utils.fp.T, delphi.utils.fp.T], delphi.utils.fp.T], xs: Iterable[delphi.utils.fp.T]) Iterator[delphi.utils.fp.T][source]

Make an iterator that returns accumulated results of a binary function applied to elements of an iterable.

\[scanl1(f, [x_0, x_1, x_2, ...]) = [x_0, f(x_0, x_1), f(f(x_0, x_1), x_2), ...]\]
Parameters:
  • f – A binary function of two arguments of type T.

  • xs – An iterable of elements of type T.

Returns:

Iterator – The iterator of accumulated results.

Examples

>>> from delphi.utils.fp import scanl1
>>> list(scanl1(lambda x, y: x + y, range(5)))
[0, 1, 3, 6, 10]
foldl(f: Callable[[delphi.utils.fp.T, delphi.utils.fp.U], delphi.utils.fp.T], x: delphi.utils.fp.T, xs: Iterable[delphi.utils.fp.U]) delphi.utils.fp.T[source]

Returns the accumulated result of a binary function applied to elements of an iterable.

\[foldl(f, x_0, [x_1, x_2, x_3]) = f(f(f(f(x_0, x_1), x_2), x_3)\]

Examples

>>> from delphi.utils.fp import foldl
>>> foldl(lambda x, y: x + y, 10, range(5))
20
foldl1(f: Callable[[delphi.utils.fp.T, delphi.utils.fp.T], delphi.utils.fp.T], xs: Iterable[delphi.utils.fp.T]) delphi.utils.fp.T[source]

Returns the accumulated result of a binary function applied to elements of an iterable.

\[foldl1(f, [x_0, x_1, x_2, x_3]) = f(f(f(f(x_0, x_1), x_2), x_3)\]

Examples

>>> from delphi.utils.fp import foldl1
>>> foldl1(lambda x, y: x + y, range(5))
10
flatten(xs: Union[List, Tuple]) List[source]

Flatten a nested list or tuple.

iterate(f: Callable[[delphi.utils.fp.T], delphi.utils.fp.T], x: delphi.utils.fp.T) Iterator[delphi.utils.fp.T][source]

Makes infinite iterator that returns the result of successive applications of a function to an element

\[iterate(f, x) = [x, f(x), f(f(x)), f(f(f(x))), ...]\]

Examples

>>> from delphi.utils.fp import iterate, take
>>> list(take(5, iterate(lambda x: x*2, 1)))
[1, 2, 4, 8, 16]
take(n: int, xs: Iterable[delphi.utils.fp.T]) Iterable[delphi.utils.fp.T][source]
ptake(n: int, xs: Iterable[delphi.utils.fp.T]) Iterable[delphi.utils.fp.T][source]

take with a tqdm progress bar.

ltake(n: int, xs: Iterable[delphi.utils.fp.T]) List[delphi.utils.fp.T][source]

A non-lazy version of take.

compose(*fs: Any) Callable[source]

Compose functions from left to right.

e.g. compose(f, g)(x) = f(g(x))

rcompose(*fs: Any) Callable[source]

Compose functions from right to left.

e.g. rcompose(f, g)(x) = g(f(x))

flatMap(f: Callable, xs: Iterable) List[source]

Map a function onto an iterable and flatten the result.

exists(x: Any) bool[source]
repeatfunc(func: Callable, *args)[source]

Repeat calls to func with specified arguments.

Example: repeatfunc(random.random)

grouper(xs: Iterable, n: int, fillvalue=None)[source]

Collect data into fixed-length chunks or blocks. >>> from delphi.utils.fp import grouper >>> list(grouper(‘ABCDEFG’, 3, ‘x’)) [(‘A’, ‘B’, ‘C’), (‘D’, ‘E’, ‘F’), (‘G’, ‘x’, ‘x’)]

pairwise(iterable)[source]

s -> (s0,s1), (s1,s2), (s2, s3), …

delphi.utils.indra module

delphi.utils.misc module

choose_font()[source]
multiple_replace(d: Dict[str, str], text: str) str[source]

Performs string replacement from dict in a single pass. Taken from https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s15.html

delphi.utils.shell module

Helper functions for shell operations.

cd(destination_directory)

delphi.utils.web module

Helper functions for web activity.

tqdm_reporthook(t)[source]

Wraps tqdm instance. Don’t forget to close() or __exit__() the tqdm instance once you’re done with it (easiest using with syntax). .. rubric:: Example

get_data_from_url(url: str)[source]
download_file(url: str, filename: str)[source]

Module contents