pathex.adts.util module

Useful functions and constants

Module

pathex.adts.util


This module contains some useful little functions and constants.

Caution

The content of this module is not intended to be part of the exposed main features of PathEx, so it may change in the future. It is documented here only because it is part of the current internal design of the library.

SET_OF_STRS

alias of types.SetWrapper

SET_OF_TUPLES

alias of types.SetWrapper

get_head_tail(iterable: Iterable[T]) tuple[object, Iterator[T] | None][source]

Decompose the given iterable into its head and tail.

Example:

>>> head, tail = get_head_tail([1, 2, 3, 4])
>>> assert head == 1
>>> assert list(tail) == [2, 3, 4]
>>> assert (None, None) == get_head_tail(())
>>> head, tail = get_head_tail(iter([1, 2, 3, 4]))
>>> assert head == 1
>>> assert list(tail) == [2, 3, 4]
Parameters

iterable (Iterable[T]) – The iterable to be decomposed. If it is an iterator it must also be copiable.

Returns

A tuple (head, tail) where head is the first element and tail is the rest of the iterable.

Return type

tuple[object, Iterator[T] | None]

take(n: int, iterable: Iterable[T]) Iterator[T][source]

Returns an iterator that takes n elements from iterable.

If iterable has less than n elements, all elements from iterable are given.

Example:

>>> assert list(take(5, range(7))) == [0, 1, 2, 3, 4]
>>> assert list(take(5, range(4))) == [0, 1, 2, 3]
Parameters
  • n (int) – the amount of elements to be taken.

  • it (Iterable[T]) – the iterable to be taken from.

Returns

The resulting iterator.

Return type

Iterator[T]