Tally¶
Subclass of collections.Counter
with additional methods.
New in version 0.3.0.
Data:
Invariant |
Classes:
|
|
|
Subclass of |
Provides a dictionary interface, but with |
-
_F
= TypeVar(_F, float, int, Real)¶ Type:
TypeVar
Invariant
TypeVar
constrained tofloat
,int
andnumbers.Real
.
-
protocol
SupportsMostCommon
[source]¶ Bases:
typing.Protocol
typing.Protocol
for classes which support acollections.Counter
-likecollections.Counter.most_common()
method.This protocol is runtime checkable.
Classes that implement this protocol must have the following methods / attributes:
-
most_common
(n=None)[source]¶ List the
n
most common elements and their counts from the most common to the least. Ifn
isNone
then list all element counts.>>> Counter('abracadabra').most_common(3) [('a', 5), ('b', 2), ('r', 2)]
-
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:
Returns the
Tally
as acollections.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:
Returns the total count for all elements.
-
as_percentage
()[source]¶ Returns the
Tally
as acollections.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
]
-
get_percentage
(item, default=None)[source]¶ Returns the count for
item
, as a percentage of the sum of all elements.
-
-
class
Percentage
[source]¶ -
Provides a dictionary interface, but with
collections.Counter
’scollections.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).
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.
Helper for pickle.
__reduce_ex__
(protocol, /)Helper for pickle.
__repr__
()Return repr(self).
Return a reverse iterator over the dict keys.
__setattr__
(name, value, /)Implement setattr(self, name, value).
__setitem__
(key, value, /)Set self[key] to value.
__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¶
-