# HG changeset patch # User Wheerd # Date 1475016834 -7200 # Wed Sep 28 00:53:54 2016 +0200 # Node ID c5f6d122ddd684105e8f7b8910ab50c7d54bba73 # Parent 8f0df4db2b06389d31ceb82e55cb4d6cbfd6fd1d Added _le__ and __ge__ to collections.Counter. diff -r 8f0df4db2b06 -r c5f6d122ddd6 Lib/collections/__init__.py --- a/Lib/collections/__init__.py Tue Sep 27 15:23:04 2016 -0700 +++ b/Lib/collections/__init__.py Wed Sep 28 00:53:54 2016 +0200 @@ -843,6 +843,40 @@ if other_count < count: self[elem] = other_count return self._keep_positive() + + def __le__(self, other): + '''Checks if all counts from this counter are less than or equal to the other. + + >>> Counter('ab') <= Counter('aabc') + True + + ''' + if not isinstance(other, Counter): + return NotImplemented + for elem in self: + if self[elem] > other[elem]: + return False + for elem in other: + if self[elem] > other[elem]: + return False + return True + + def __ge__(self, other): + '''Checks if all counts from this counter are greater than or equal to the other. + + >>> Counter('aabc') >= Counter('ab') + True + + ''' + if not isinstance(other, Counter): + return NotImplemented + for elem in self: + if self[elem] < other[elem]: + return False + for elem in other: + if self[elem] < other[elem]: + return False + return True ########################################################################