pyjacket.core

Pyjacket.core contains features that extend the base language. Its features are directly accessible from the top level:

>>> import pyjacket as pj
>>> pj.core.digits(123)  # works, but not intended
>>> pj.digits(123)
(1, 2, 3)
class pyjacket.PositiveCounter(*a, **kw)[source]

Bases: Counter

Variant of collections.Counter that only keeps positive counts.

Example:

>>> pc = PositiveCounter()
>>> pc.add('apple')
>>> pc.add('apple')
>>> pc.remove('apple')
>>> pc
PositiveCounter({'apple': 1})
>>> pc.remove('apple')
>>> pc
PositiveCounter({})
add(element)[source]

Increment the count for element by 1.

remove(element)[source]

Subtract 1 from element count; remove if count <= 0.

sub(element)[source]

Decrement the count for element by 1; remove if count <= 0.

pyjacket.all_same(it: Iterable) bool[source]

Determine if all elements of the array are identical

Parameters:

it (Iterable) – elements to compare

Returns:

True/False

Return type:

bool

pyjacket.batched(it: Iterable, batch_size: int, fill_value=None)[source]

Yield successive batches from the iterable.

Parameters:
  • iterable – An iterable to batch.

  • batch_size – The size of each batch.

  • fill_value – The value to fill in for incomplete batches.

Yields:

Lists containing the batches of specified size.

Examples:

>>> batched('0123456789', 4, '#')
('0', '1', '2', '3')
('4', '5', '6', '7')
('8', '9', '#', '#')
pyjacket.cyclic_shifts(it: Iterable)[source]

Find all the cyclic arrangements of the iterable

Parameters:

iterable (iterable) – iterable to cycle

Yields:

tuple – shifted permutation of iterable

Examples

>>> cyclic_shifts([1, 2, 3, 4, 5])
(1, 2, 3, 4, 5)
(2, 3, 4, 5, 1)
(3, 4, 5, 1, 2)
(4, 5, 1, 2, 3)
(5, 1, 2, 3, 4)
pyjacket.digits(n: int, base: int = 10) list[int][source]

Obtain the digits of an integer in any number base

Parameters:
  • n (int) – number of which to obtain the digits

  • base (int, optional) – base of number system, by default 10

Returns:

digits of n

Return type:

list

Raises:

ValueError – Number base must be larger than 1 Number should be an integer

Examples

>>> digits(137)
[1, 3, 7]
>>> digits(254, 16)
[15, 14]  # 255 = 15*16 + 14
pyjacket.extend_num(num, n)[source]
pyjacket.extend_str()[source]
pyjacket.index_nth(it: Iterable, element, n: int = -1) int[source]

Find nth (default: last) occurence of element in iterable.

Parameters:
  • iterable (iterable)

  • element (Any) – element to find in iterable

  • n (int) – n’th occurence of element to find. Default is -1 and finds the last element.

Returns:

index of the n’th occurence

Return type:

int

Raises:

ValueError – n must be nonzero iterable does not contain element iterable does not contain element n times

Examples

>>> find_nth([1, 2, 3, 4, 1, 2, 3, 1, 1, 1, 2], 1, 3)
7
pyjacket.isplit(s: str, i: int | list[int])[source]

split a string at index or list of indices

pyjacket.oom(num, base=10)[source]

Order of a magnitude belong to a number (base 10)

pyjacket.partition(condition: Callable, it: Iterable) tuple[list, list][source]

Split an iterable into (truths, falses)

Parameters:
  • condition (callable) – function to evaluate elements of iterable

  • iterable (iterable) – sequence of elements to partition.

Returns:

  • list – elements that evaluated true

  • list – elements that evaluated false

Examples

>>> Partition(lambda x: x<5, [1, 2, 3, 4, 5, 6, 7, 8])
[1, 2, 3, 4], [5, 6, 7, 8]
pyjacket.round_significant(num, significance)[source]

Round the number to nearest significant digits

pyjacket.sign(num)[source]
pyjacket.sliding_window(iterable: Iterable, n: int)[source]

Iterate the sliding windows of size n

Parameters:
  • iterable (Iterable) – iterable to slide across

  • n (int) – window size

Yields:

tuple – elements of the sliding window

Examples

>>> sliding_window('abcdefg', 4)
('a', 'b', 'c', 'd')
('b', 'c', 'd', 'e')
('c', 'd', 'e', 'f')
('d', 'e', 'f', 'g')
pyjacket.sortby(it: Iterable, val: Iterable) list[source]

Sort iterable by the values of another iterables

Parameters:
  • it (Iterable) – iterable that requires sorting

  • Y (Iterable) – values on which sorting is based

Returns:

sorted it

Return type:

list

pyjacket.sumprod(a, b, mod=None)[source]
pyjacket.truncate_num(num, n)[source]
pyjacket.truncate_significant(num, significance, count_zero=False)[source]

Round the number down to desired significant digits

pyjacket.truncate_str(s: str, n: int, symbol='')[source]

Fixes the length of a string. Use truncation symbol <symbol> to denote truncation.