Tally

Subclass of collections.Counter with additional methods.

New in version 0.3.0.

Data:

_F

Invariant TypeVar constrained to float, int and numbers.Real.

Classes:

SupportsMostCommon

typing.Protocol for classes which support a collections.Counter-like collections.Counter.most_common() method.

Tally([iterable])

Subclass of collections.Counter with additional methods.

Percentage

Provides a dictionary interface, but with collections.Counter’s collections.Counter.most_common() method.

_F = TypeVar(_F, float, int, Real)

Type:    TypeVar

Invariant TypeVar constrained to float, int and numbers.Real.

protocol SupportsMostCommon[source]

Bases: Protocol[~KT]

typing.Protocol for classes which support a collections.Counter-like collections.Counter.most_common() method.

This protocol is runtime checkable.

Classes that implement this protocol must have the following methods / attributes:

items()[source]

Returns an iterator over the mapping’s items (as (key, value) pairs).

Return type

Iterable[Tuple[~KT, float]]

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)]
Parameters

n (Optional[int]) – Default None.

Return type

Union[List[Tuple[~KT, float]], List[Tuple[~KT, int]]]

__non_callable_proto_members__ = {}

Type:    set

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

Bases: Counter[~KT]

Subclass of collections.Counter with additional methods.

New in version 0.3.0.

Methods:

as_percentage()

Returns the Tally as a collections.OrderedDict comprising the count for each element as a percentage of the sum of all elements.

get_percentage(item[, default])

Returns the count for item, as a percentage of the sum of all elements.

most_common([n])

List the n most common elements and their counts from the most common to the least.

Attributes:

total

Returns the total count for all elements.

as_percentage()[source]

Returns the Tally as a collections.OrderedDict comprising the count for each element as a percentage of the sum of all elements.

Important

The sum of the dictionary’s values may not add up to exactly 1.0 due to limitations of floating-point numbers.

Return type

Percentage[~KT]

property total

Returns the total count for all elements.

Return type

int

get_percentage(item, default=None)[source]

Returns the count for item, as a percentage of the sum of all elements.

Parameters
  • item (~KT)

  • default (Optional[~_F]) – A default percentage (as a float) to return if item is not in the dictionary. Default None.

Return type

Union[None, ~_F, float]

Overloads
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.

>>> Tally('abracadabra').most_common(3)
[('a', 5), ('b', 2), ('r', 2)]
Parameters

n (Optional[int]) – Default None.

Return type

List[Tuple[~KT, int]]

class Percentage[source]

Bases: Dict[~KT, float]

Provides a dictionary interface, but with collections.Counter’s collections.Counter.most_common() method.

Represents the return type of cawdrey.tally.Tally.as_percentage().

Methods:

most_common([n])

List the n most common elements and their counts from the most common to the least.

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.

>>> Tally('abracadabra').as_percentage().most_common(3)
[('a', 0.45454545454545453), ('b', 0.18181818181818182), ('r', 0.18181818181818182)]
Parameters

n (Optional[int]) – Default None.

Return type

List[Tuple[~KT, float]]