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: typing.Protocol

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

classmethod __class_getitem__(params)
classmethod __init_subclass__(*args, **kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

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:

__contains__(key, /)

True if the dictionary has the specified key, else False.

__delattr__(name, /)

Implement delattr(self, name).

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__

x.__getitem__(y) <==> x[y]

__gt__(value, /)

Return self>value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.

__reduce__()

Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.

__repr__()

Return repr(self).

__reversed__()

Return a reverse iterator over the dict keys.

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setitem__(key, value, /)

Set self[key] to value.

__sizeof__()

__str__()

Return str(self).

clear()

copy()

fromkeys([value])

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

get(key[, default])

Return the value for key if key is in the dictionary, else default.

items()

keys()

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

Remove and return a (key, value) pair as a 2-tuple.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

most_common([n])

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

__contains__(key, /)

True if the dictionary has the specified key, else False.

__delattr__(name, /)

Implement delattr(self, name).

__delitem__(key, /)

Delete self[key].

__eq__(value, /)

Return self==value.

__format__(format_spec, /)

Default object formatter.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

__getitem__()

x.__getitem__(y) <==> x[y]

__gt__(value, /)

Return self>value.

__iter__()

Implement iter(self).

__le__(value, /)

Return self<=value.

__len__()

Return len(self).

__lt__(value, /)

Return self<value.

__ne__(value, /)

Return self!=value.

__reduce__()

Helper for pickle.

__reduce_ex__(protocol, /)

Helper for pickle.

__repr__()

Return repr(self).

__reversed__()

Return a reverse iterator over the dict keys.

__setattr__(name, value, /)

Implement setattr(self, name, value).

__setitem__(key, value, /)

Set self[key] to value.

__sizeof__() → size of D in memory, in bytes
__str__()

Return str(self).

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys(value=None, /)

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

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() → a set-like object providing a view on D’s items
keys() → a set-like object providing a view on D’s keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → an object providing a view on D’s values
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]]