@@ -243,6 +243,7 @@ def pop(self, key, default=__marker):
243
243
marker = self .__marker
244
244
result = dict .pop (self , key , marker )
245
245
if result is not marker :
246
+ # The same as in __delitem__().
246
247
link = self .__map .pop (key )
247
248
link_prev = link .prev
248
249
link_next = link .next
@@ -274,10 +275,22 @@ def __repr__(self):
274
275
275
276
def __reduce__ (self ):
276
277
'Return state information for pickling'
277
- inst_dict = vars (self ).copy ()
278
- for k in vars (OrderedDict ()):
279
- inst_dict .pop (k , None )
280
- return self .__class__ , (), inst_dict or None , None , iter (self .items ())
278
+ state = self .__getstate__ ()
279
+ if state :
280
+ if isinstance (state , tuple ):
281
+ state , slots = state
282
+ else :
283
+ slots = {}
284
+ state = state .copy ()
285
+ slots = slots .copy ()
286
+ for k in vars (OrderedDict ()):
287
+ state .pop (k , None )
288
+ slots .pop (k , None )
289
+ if slots :
290
+ state = state , slots
291
+ else :
292
+ state = state or None
293
+ return self .__class__ , (), state , None , iter (self .items ())
281
294
282
295
def copy (self ):
283
296
'od.copy() -> a shallow copy of od'
@@ -620,11 +633,9 @@ def elements(self):
620
633
['A', 'A', 'B', 'B', 'C', 'C']
621
634
622
635
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
636
+ >>> import math
623
637
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
624
- >>> product = 1
625
- >>> for factor in prime_factors.elements(): # loop over factors
626
- ... product *= factor # and multiply them
627
- >>> product
638
+ >>> math.prod(prime_factors.elements())
628
639
1836
629
640
630
641
Note, if an element's count has been set to zero or is a negative
@@ -721,42 +732,6 @@ def __delitem__(self, elem):
721
732
if elem in self :
722
733
super ().__delitem__ (elem )
723
734
724
- def __eq__ (self , other ):
725
- 'True if all counts agree. Missing counts are treated as zero.'
726
- if not isinstance (other , Counter ):
727
- return NotImplemented
728
- return all (self [e ] == other [e ] for c in (self , other ) for e in c )
729
-
730
- def __ne__ (self , other ):
731
- 'True if any counts disagree. Missing counts are treated as zero.'
732
- if not isinstance (other , Counter ):
733
- return NotImplemented
734
- return not self == other
735
-
736
- def __le__ (self , other ):
737
- 'True if all counts in self are a subset of those in other.'
738
- if not isinstance (other , Counter ):
739
- return NotImplemented
740
- return all (self [e ] <= other [e ] for c in (self , other ) for e in c )
741
-
742
- def __lt__ (self , other ):
743
- 'True if all counts in self are a proper subset of those in other.'
744
- if not isinstance (other , Counter ):
745
- return NotImplemented
746
- return self <= other and self != other
747
-
748
- def __ge__ (self , other ):
749
- 'True if all counts in self are a superset of those in other.'
750
- if not isinstance (other , Counter ):
751
- return NotImplemented
752
- return all (self [e ] >= other [e ] for c in (self , other ) for e in c )
753
-
754
- def __gt__ (self , other ):
755
- 'True if all counts in self are a proper superset of those in other.'
756
- if not isinstance (other , Counter ):
757
- return NotImplemented
758
- return self >= other and self != other
759
-
760
735
def __repr__ (self ):
761
736
if not self :
762
737
return f'{ self .__class__ .__name__ } ()'
@@ -802,6 +777,42 @@ def __repr__(self):
802
777
# (cp >= cq) == (sp >= sq)
803
778
# (cp > cq) == (sp > sq)
804
779
780
+ def __eq__ (self , other ):
781
+ 'True if all counts agree. Missing counts are treated as zero.'
782
+ if not isinstance (other , Counter ):
783
+ return NotImplemented
784
+ return all (self [e ] == other [e ] for c in (self , other ) for e in c )
785
+
786
+ def __ne__ (self , other ):
787
+ 'True if any counts disagree. Missing counts are treated as zero.'
788
+ if not isinstance (other , Counter ):
789
+ return NotImplemented
790
+ return not self == other
791
+
792
+ def __le__ (self , other ):
793
+ 'True if all counts in self are a subset of those in other.'
794
+ if not isinstance (other , Counter ):
795
+ return NotImplemented
796
+ return all (self [e ] <= other [e ] for c in (self , other ) for e in c )
797
+
798
+ def __lt__ (self , other ):
799
+ 'True if all counts in self are a proper subset of those in other.'
800
+ if not isinstance (other , Counter ):
801
+ return NotImplemented
802
+ return self <= other and self != other
803
+
804
+ def __ge__ (self , other ):
805
+ 'True if all counts in self are a superset of those in other.'
806
+ if not isinstance (other , Counter ):
807
+ return NotImplemented
808
+ return all (self [e ] >= other [e ] for c in (self , other ) for e in c )
809
+
810
+ def __gt__ (self , other ):
811
+ 'True if all counts in self are a proper superset of those in other.'
812
+ if not isinstance (other , Counter ):
813
+ return NotImplemented
814
+ return self >= other and self != other
815
+
805
816
def __add__ (self , other ):
806
817
'''Add counts from two counters.
807
818
0 commit comments