pyjacket

class pyjacket.core.Counter(iterable=None, /, **kwds)[source]

Bases: dict

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.

>>> c = Counter('abcdeabcdabcaba')  # count elements from a string
>>> c.most_common(3)                # three most common elements
[('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c)                       # list all unique elements
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements()))   # list elements with repetitions
'aaaaabbbbcccdde'
>>> sum(c.values())                 # total of all counts
15
>>> c['a']                          # count of letter 'a'
5
>>> for elem in 'shazam':           # update counts from an iterable
...     c[elem] += 1                # by adding 1 to each element's count
>>> c['a']                          # now there are seven 'a'
7
>>> del c['b']                      # remove all 'b'
>>> c['b']                          # now there are zero 'b'
0
>>> d = Counter('simsalabim')       # make another counter
>>> c.update(d)                     # add in the second counter
>>> c['a']                          # now there are nine 'a'
9
>>> c.clear()                       # empty the counter
>>> c
Counter()

Note: If a count is set to zero or reduced to zero, it will remain in the counter until the entry is deleted or the counter is cleared:

>>> c = Counter('aaabbc')
>>> c['b'] -= 2                     # reduce the count of 'b' by two
>>> c.most_common()                 # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]
copy()[source]

Return a shallow copy.

elements()[source]

Iterator over elements repeating each as many times as its count.

>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']

# Knuth’s example for prime factors of 1836: 2**2 * 3**3 * 17**1 >>> import math >>> prime_factors = Counter({2: 2, 3: 3, 17: 1}) >>> math.prod(prime_factors.elements()) 1836

Note, if an element’s count has been set to zero or is a negative number, elements() will ignore it.

classmethod fromkeys(iterable, v=None)[source]

Create a new dictionary with keys from iterable and values set to value.

most_common(n=None)[source]

List the n most common elements and their counts from the most common to the least. If n is None, then list all element counts.

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('b', 2), ('r', 2)]
subtract(iterable=None, /, **kwds)[source]

Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.subtract('witch')             # subtract elements from another iterable
>>> c.subtract(Counter('watch'))    # subtract elements from another counter
>>> c['h']                          # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch
-1
total()[source]

Sum of the counts

update(iterable=None, /, **kwds)[source]

Like dict.update() but add counts instead of replacing them.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.update('witch')           # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d)                 # add elements from another counter
>>> c['h']                      # four 'h' in which, witch, and watch
4
class pyjacket.core.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.core.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.core.apply_to_columns(df: DataFrame, modify: callable)[source]
pyjacket.core.apply_to_rows(df: DataFrame, modify: callable)[source]
pyjacket.core.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.core.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.core.df_apply(df: DataFrame, modify: callable, *args, **kwargs)[source]
pyjacket.core.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.core.extend_modulo(s: str, n: int, fillval='0')[source]

Pads a fill value to ensure len(s) is a multiple of n

pyjacket.core.extend_num(num, n)[source]
pyjacket.core.extend_str()[source]
class pyjacket.core.filterfalse(function, iterable, /)

Bases: object

Return those items of iterable for which function(item) is false.

If function is None, return the items that are false.

pyjacket.core.floor(x, /)

Return the floor of x as an Integral.

This is the largest integer <= x.

pyjacket.core.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.core.isplit(s: str, i: int | list[int])[source]

split a string at index or list of indices

pyjacket.core.log(x[, base=math.e])

Return the logarithm of x to the given base.

If the base not specified, returns the natural logarithm (base e) of x.

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

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

pyjacket.core.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.core.prod_modulo(arr, mod=None)[source]
pyjacket.core.round_significant(num, significance)[source]

Round the number to nearest significant digits

pyjacket.core.sign(num)[source]
pyjacket.core.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.core.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.core.sum_modulo(arr, mod=None)[source]
pyjacket.core.sumprod(a, b, mod=None)[source]
pyjacket.core.truncate_modulo(s: str, mod: int)[source]

Ensures length of a string <s> is a multiple of <n>

pyjacket.core.truncate_num(num, n)[source]
pyjacket.core.truncate_significant(num, significance, count_zero=False)[source]

Round the number down to desired significant digits

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

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

class pyjacket.core.zip_longest

Bases: object

zip_longest(iter1 [,iter2 […]], [fillvalue=None]) –> zip_longest object

Return a zip_longest object whose .__next__() method returns a tuple where the i-th element comes from the i-th iterable argument. The .__next__() method continues until the longest iterable in the argument sequence is exhausted and then it raises StopIteration. When the shorter iterables are exhausted, the fillvalue is substituted in their place. The fillvalue defaults to None or can be specified by a keyword argument.