Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.3k
Expand file tree
/
Copy path__init__.py
More file actions
Latest commit
1576 lines (1270 loc) · 50.8 KB
/
Copy path__init__.py
File metadata and controls
1576 lines (1270 loc) · 50.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.
* namedtuple factory function for creating tuple subclasses with named fields
* deque list-like container with fast appends and pops on either end
* ChainMap dict-like class for creating a single view of multiple mappings
* Counter dict subclass for counting hashable objects
* OrderedDict dict subclass that remembers the order entries were added
* defaultdict dict subclass that calls a factory function to supply missing values
* UserDict wrapper around dictionary objects for easier dict subclassing
* UserList wrapper around list objects for easier list subclassing
* UserString wrapper around string objects for easier string subclassing
'''
__all__= [
'ChainMap',
'Counter',
'OrderedDict',
'UserDict',
'UserList',
'UserString',
'defaultdict',
'deque',
'namedtuple',
]
import_collections_abc
importsysas_sys
fromitertoolsimportchainas_chain
fromitertoolsimportrepeatas_repeat
fromitertoolsimportstarmapas_starmap
fromkeywordimportiskeywordas_iskeyword
fromoperatorimporteqas_eq
fromoperatorimportitemgetteras_itemgetter
fromreprlibimportrecursive_repras_recursive_repr
from_weakrefimportproxyas_proxy
try:
from_collectionsimportdeque
exceptImportError:
pass
else:
_collections_abc.MutableSequence.register(deque)
try:
from_collectionsimportdefaultdict
exceptImportError:
pass
################################################################################
### OrderedDict
################################################################################
class_OrderedDictKeysView(_collections_abc.KeysView):
def__reversed__(self):
yieldfromreversed(self._mapping)
class_OrderedDictItemsView(_collections_abc.ItemsView):
def__reversed__(self):
forkeyinreversed(self._mapping):
yield (key, self._mapping[key])
class_OrderedDictValuesView(_collections_abc.ValuesView):
def__reversed__(self):
forkeyinreversed(self._mapping):
yieldself._mapping[key]
class_Link(object):
__slots__='prev', 'next', 'key', '__weakref__'
classOrderedDict(dict):
'Dictionary that remembers insertion order'
# An inherited dict maps keys to values.
# The inherited dict provides __getitem__, __len__, __contains__, and get.
# The remaining methods are order-aware.
# Big-O running times for all methods are the same as regular dictionaries.
# The internal self.__map dict maps keys to links in a doubly linked list.
# The circular doubly linked list starts and ends with a sentinel element.
# The sentinel element never gets deleted (this simplifies the algorithm).
# The sentinel is in self.__hardroot with a weakref proxy in self.__root.
# The prev links are weakref proxies (to prevent circular references).
# Individual links are kept alive by the hard reference in self.__map.
# Those hard references disappear when a key is deleted from an OrderedDict.
def__new__(cls, /, *args, **kwds):
"Create the ordered dict object and set up the underlying structures."
self=dict.__new__(cls)
self.__hardroot=_Link()
self.__root=root=_proxy(self.__hardroot)
root.prev=root.next=root
self.__map= {}
returnself
def__init__(self, other=(), /, **kwds):
'''Initialize an ordered dictionary. The signature is the same as
regular dictionaries. Keyword argument order is preserved.
'''
self.__update(other, **kwds)
def__setitem__(self, key, value,
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link at the end of the linked list,
# and the inherited dictionary is updated with the new key/value pair.
ifkeynotinself:
self.__map[key] =link=Link()
root=self.__root
last=root.prev
link.prev, link.next, link.key=last, root, key
last.next=link
root.prev=proxy(link)
dict_setitem(self, key, value)
def__delitem__(self, key, dict_delitem=dict.__delitem__):
'od.__delitem__(y) <==> del od[y]'
# Deleting an existing item uses self.__map to find the link which gets
# removed by updating the links in the predecessor and successor nodes.
dict_delitem(self, key)
link=self.__map.pop(key)
link_prev=link.prev
link_next=link.next
link_prev.next=link_next
link_next.prev=link_prev
link.prev=None
link.next=None
def__iter__(self):
'od.__iter__() <==> iter(od)'
# Traverse the linked list in order.
root=self.__root
curr=root.next
whilecurrisnotroot:
yieldcurr.key
curr=curr.next
def__reversed__(self):
'od.__reversed__() <==> reversed(od)'
# Traverse the linked list in reverse order.
root=self.__root
curr=root.prev
whilecurrisnotroot:
yieldcurr.key
curr=curr.prev
defclear(self):
'od.clear() -> None. Remove all items from od.'
root=self.__root
root.prev=root.next=root
self.__map.clear()
dict.clear(self)
defpopitem(self, last=True):
'''Remove and return a (key, value) pair from the dictionary.
Pairs are returned in LIFO order if last is true or FIFO order if false.
'''
ifnotself:
raiseKeyError('dictionary is empty')
root=self.__root
iflast:
link=root.prev
link_prev=link.prev
link_prev.next=root
root.prev=link_prev
else:
link=root.next
link_next=link.next
root.next=link_next
link_next.prev=root
key=link.key
delself.__map[key]
value=dict.pop(self, key)
returnkey, value
defmove_to_end(self, key, last=True):
'''Move an existing element to the end (or beginning if last is false).
Raise KeyError if the element does not exist.
'''
link=self.__map[key]
link_prev=link.prev
link_next=link.next
soft_link=link_next.prev
link_prev.next=link_next
link_next.prev=link_prev
root=self.__root
iflast:
last=root.prev
link.prev=last
link.next=root
root.prev=soft_link
last.next=link
else:
first=root.next
link.prev=root
link.next=first
first.prev=soft_link
root.next=link
def__sizeof__(self):
sizeof=_sys.getsizeof
n=len(self) +1# number of links including root
size=sizeof(self.__dict__) # instance dictionary
size+=sizeof(self.__map) *2# internal dict and inherited dict
size+=sizeof(self.__hardroot) *n# link objects
size+=sizeof(self.__root) *n# proxy objects
returnsize
update=__update=_collections_abc.MutableMapping.update
defkeys(self):
"D.keys() -> a set-like object providing a view on D's keys"
return_OrderedDictKeysView(self)
defitems(self):
"D.items() -> a set-like object providing a view on D's items"
return_OrderedDictItemsView(self)
defvalues(self):
"D.values() -> an object providing a view on D's values"
return_OrderedDictValuesView(self)
__ne__=_collections_abc.MutableMapping.__ne__
__marker=object()
defpop(self, key, default=__marker):
'''od.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.
'''
marker=self.__marker
result=dict.pop(self, key, marker)
ifresultisnotmarker:
# The same as in __delitem__().
link=self.__map.pop(key)
link_prev=link.prev
link_next=link.next
link_prev.next=link_next
link_next.prev=link_prev
link.prev=None
link.next=None
returnresult
ifdefaultismarker:
raiseKeyError(key)
returndefault
defsetdefault(self, 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.
'''
ifkeyinself:
returnself[key]
self[key] =default
returndefault
@_recursive_repr()
def__repr__(self):
'od.__repr__() <==> repr(od)'
ifnotself:
return'%s()'% (self.__class__.__name__,)
return'%s(%r)'% (self.__class__.__name__, list(self.items()))
def__reduce__(self):
'Return state information for pickling'
state=self.__getstate__()
ifstate:
ifisinstance(state, tuple):
state, slots=state
else:
slots= {}
state=state.copy()
slots=slots.copy()
forkinvars(OrderedDict()):
state.pop(k, None)
slots.pop(k, None)
ifslots:
state=state, slots
else:
state=stateorNone
returnself.__class__, (), state, None, iter(self.items())
defcopy(self):
'od.copy() -> a shallow copy of od'
returnself.__class__(self)
@classmethod
deffromkeys(cls, iterable, value=None):
'''Create a new ordered dictionary with keys from iterable and values set to value.
'''
self=cls()
forkeyiniterable:
self[key] =value
returnself
def__eq__(self, other):
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
while comparison to a regular mapping is order-insensitive.
'''
ifisinstance(other, OrderedDict):
returndict.__eq__(self, other) andall(map(_eq, self, other))
returndict.__eq__(self, other)
def__ior__(self, other):
self.update(other)
returnself
def__or__(self, other):
ifnotisinstance(other, dict):
returnNotImplemented
new=self.__class__(self)
new.update(other)
returnnew
def__ror__(self, other):
ifnotisinstance(other, dict):
returnNotImplemented
new=self.__class__(other)
new.update(self)
returnnew
try:
from_collectionsimportOrderedDict
exceptImportError:
# Leave the pure Python version in place.
pass
################################################################################
### namedtuple
################################################################################
try:
from_collectionsimport_tuplegetter
exceptImportError:
_tuplegetter=lambdaindex, doc: property(_itemgetter(index), doc=doc)
defnamedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
"""Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain tuple
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> d = p._asdict() # convert to a dictionary
>>> d['x']
11
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
"""
# Validate the field names. At the user's option, either generate an error
# message or automatically replace the field name with a valid name.
ifisinstance(field_names, str):
field_names=field_names.replace(',', ' ').split()
field_names=list(map(str, field_names))
typename=_sys.intern(str(typename))
ifrename:
seen=set()
forindex, nameinenumerate(field_names):
if (notname.isidentifier()
or_iskeyword(name)
orname.startswith('_')
ornameinseen):
field_names[index] =f'_{index}'
seen.add(name)
fornamein [typename] +field_names:
iftype(name) isnotstr:
raiseTypeError('Type names and field names must be strings')
ifnotname.isidentifier():
raiseValueError('Type names and field names must be valid '
f'identifiers: {name!r}')
if_iskeyword(name):
raiseValueError('Type names and field names cannot be a '
f'keyword: {name!r}')
seen=set()
fornameinfield_names:
ifname.startswith('_') andnotrename:
raiseValueError('Field names cannot start with an underscore: '
f'{name!r}')
ifnameinseen:
raiseValueError(f'Encountered duplicate field name: {name!r}')
seen.add(name)
field_defaults= {}
ifdefaultsisnotNone:
defaults=tuple(defaults)
iflen(defaults) >len(field_names):
raiseTypeError('Got more default values than field names')
field_defaults=dict(reversed(list(zip(reversed(field_names),
reversed(defaults)))))
# Variables used in the methods and docstrings
field_names=tuple(map(_sys.intern, field_names))
num_fields=len(field_names)
arg_list=', '.join(field_names)
ifnum_fields==1:
arg_list+=','
repr_fmt='('+', '.join(f'{name}=%r'fornameinfield_names) +')'
tuple_new=tuple.__new__
_dict, _tuple, _len, _map, _zip=dict, tuple, len, map, zip
# Create all the named tuple methods to be added to the class namespace
namespace= {
'_tuple_new': tuple_new,
'__builtins__': {},
'__name__': f'namedtuple_{typename}',
}
code=f'lambda _cls, {arg_list}: _tuple_new(_cls, ({arg_list}))'
__new__=eval(code, namespace)
__new__.__name__='__new__'
__new__.__doc__=f'Create new instance of {typename}({arg_list})'
ifdefaultsisnotNone:
__new__.__defaults__=defaults
@classmethod
def_make(cls, iterable):
result=tuple_new(cls, iterable)
if_len(result) !=num_fields:
raiseTypeError(f'Expected {num_fields} arguments, got {len(result)}')
returnresult
_make.__func__.__doc__= (f'Make a new {typename} object from a sequence '
'or iterable')
def_replace(self, /, **kwds):
result=self._make(_map(kwds.pop, field_names, self))
ifkwds:
raiseValueError(f'Got unexpected field names: {list(kwds)!r}')
returnresult
_replace.__doc__= (f'Return a new {typename} object replacing specified '
'fields with new values')
def__repr__(self):
'Return a nicely formatted representation string'
returnself.__class__.__name__+repr_fmt%self
def_asdict(self):
'Return a new dict which maps field names to their values.'
return_dict(_zip(self._fields, self))
def__getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return_tuple(self)
# Modify function metadata to help with introspection and debugging
formethodin (
__new__,
_make.__func__,
_replace,
__repr__,
_asdict,
__getnewargs__,
):
method.__qualname__=f'{typename}.{method.__name__}'
# Build-up the class namespace dictionary
# and use type() to build the result class
class_namespace= {
'__doc__': f'{typename}({arg_list})',
'__slots__': (),
'_fields': field_names,
'_field_defaults': field_defaults,
'__new__': __new__,
'_make': _make,
'_replace': _replace,
'__repr__': __repr__,
'_asdict': _asdict,
'__getnewargs__': __getnewargs__,
'__match_args__': field_names,
}
forindex, nameinenumerate(field_names):
doc=_sys.intern(f'Alias for field number {index}')
class_namespace[name] =_tuplegetter(index, doc)
result=type(typename, (tuple,), class_namespace)
# For pickling to work, the __module__ variable needs to be set to the frame
# where the named tuple is created. Bypass this step in environments where
# sys._getframe is not defined (Jython for example) or sys._getframe is not
# defined for arguments greater than 0 (IronPython), or where the user has
# specified a particular module.
ifmoduleisNone:
try:
module=_sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
ifmoduleisnotNone:
result.__module__=module
returnresult
########################################################################
### Counter
########################################################################
def_count_elements(mapping, iterable):
'Tally elements from the iterable.'
mapping_get=mapping.get
foreleminiterable:
mapping[elem] =mapping_get(elem, 0) +1
try: # Load C helper function if available
from_collectionsimport_count_elements
exceptImportError:
pass
classCounter(dict):
'''Dict subclass for counting hashable items. Sometimes called a bag
or multiset. Elements are stored as dictionary keys and their counts
are stored as dictionary values.
>>> c = Counter('abcdeabcdabcaba') # count elements from a string
>>> c.most_common(3) # three most common elements
[('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c) # list all unique elements
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements())) # list elements with repetitions
'aaaaabbbbcccdde'
>>> sum(c.values()) # total of all counts
15
>>> c['a'] # count of letter 'a'
5
>>> for elem in 'shazam': # update counts from an iterable
... c[elem] += 1 # by adding 1 to each element's count
>>> c['a'] # now there are seven 'a'
7
>>> del c['b'] # remove all 'b'
>>> c['b'] # now there are zero 'b'
0
>>> d = Counter('simsalabim') # make another counter
>>> c.update(d) # add in the second counter
>>> c['a'] # now there are nine 'a'
9
>>> c.clear() # empty the counter
>>> c
Counter()
Note: If a count is set to zero or reduced to zero, it will remain
in the counter until the entry is deleted or the counter is cleared:
>>> c = Counter('aaabbc')
>>> c['b'] -= 2 # reduce the count of 'b' by two
>>> c.most_common() # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]
'''
# References:
# http://en.wikipedia.org/wiki/Multiset
# http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
# http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
# http://code.activestate.com/recipes/259174/
# Knuth, TAOCP Vol. II section 4.6.3
def__init__(self, iterable=None, /, **kwds):
'''Create a new, empty Counter object. And if given, count elements
from an input iterable. Or, initialize the count from another mapping
of elements to their counts.
>>> c = Counter() # a new, empty counter
>>> c = Counter('gallahad') # a new counter from an iterable
>>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
>>> c = Counter(a=4, b=2) # a new counter from keyword args
'''
super().__init__()
self.update(iterable, **kwds)
def__missing__(self, key):
'The count of elements not in the Counter is zero.'
# Needed so that self[missing_item] does not raise KeyError
return0
deftotal(self):
'Sum of the counts'
returnsum(self.values())
defmost_common(self, n=None):
'''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)]
'''
# Emulate Bag.sortedByCount from Smalltalk
ifnisNone:
returnsorted(self.items(), key=_itemgetter(1), reverse=True)
# Lazy import to speedup Python startup time
importheapq
returnheapq.nlargest(n, self.items(), key=_itemgetter(1))
defelements(self):
'''Iterator over elements repeating each as many times as its count.
>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> import math
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> math.prod(prime_factors.elements())
1836
Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.
'''
# Emulate Bag.do from Smalltalk and Multiset.begin from C++.
return_chain.from_iterable(_starmap(_repeat, self.items()))
# Override dict methods where necessary
@classmethod
deffromkeys(cls, iterable, v=None):
# There is no equivalent method for counters because the semantics
# would be ambiguous in cases such as Counter.fromkeys('aaabbc', v=2).
# Initializing counters to zero values isn't necessary because zero
# is already the default value for counter lookups. Initializing
# to one is easily accomplished with Counter(set(iterable)). For
# more exotic cases, create a dictionary first using a dictionary
# comprehension or dict.fromkeys().
raiseNotImplementedError(
'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
defupdate(self, iterable=None, /, **kwds):
'''Like dict.update() but add counts instead of replacing them.
Source can be an iterable, a dictionary, or another Counter instance.
>>> c = Counter('which')
>>> c.update('witch') # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h'] # four 'h' in which, witch, and watch
4
'''
# The regular dict.update() operation makes no sense here because the
# replace behavior results in some of the original untouched counts
# being mixed-in with all of the other counts for a mismash that
# doesn't have a straight-forward interpretation in most counting
# contexts. Instead, we implement straight-addition. Both the inputs
# and outputs are allowed to contain zero and negative counts.
ifiterableisnotNone:
ifisinstance(iterable, _collections_abc.Mapping):
ifself:
self_get=self.get
forelem, countiniterable.items():
self[elem] =count+self_get(elem, 0)
else:
# fast path when counter is empty
super().update(iterable)
else:
_count_elements(self, iterable)
ifkwds:
self.update(kwds)
defsubtract(self, iterable=None, /, **kwds):
'''Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero. Both the inputs and outputs are
allowed to contain zero and negative counts.
Source can be an iterable, a dictionary, or another Counter instance.
>>> c = Counter('which')
>>> c.subtract('witch') # subtract elements from another iterable
>>> c.subtract(Counter('watch')) # subtract elements from another counter
>>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
-1
'''
ifiterableisnotNone:
self_get=self.get
ifisinstance(iterable, _collections_abc.Mapping):
forelem, countiniterable.items():
self[elem] =self_get(elem, 0) -count
else:
foreleminiterable:
self[elem] =self_get(elem, 0) -1
ifkwds:
self.subtract(kwds)
defcopy(self):
'Return a shallow copy.'
returnself.__class__(self)
def__reduce__(self):
returnself.__class__, (dict(self),)
def__delitem__(self, elem):
'Like dict.__delitem__() but does not raise KeyError for missing values.'
ifeleminself:
super().__delitem__(elem)
def__repr__(self):
ifnotself:
returnf'{self.__class__.__name__}()'
try:
# dict() preserves the ordering returned by most_common()
d=dict(self.most_common())
exceptTypeError:
# handle case where values are not orderable
d=dict(self)
returnf'{self.__class__.__name__}({d!r})'
# Multiset-style mathematical operations discussed in:
# Knuth TAOCP Volume II section 4.6.3 exercise 19
# and at http://en.wikipedia.org/wiki/Multiset
#
# Outputs guaranteed to only include positive counts.
#
# To strip negative and zero counts, add-in an empty counter:
# c += Counter()
#
# Results are ordered according to when an element is first
# encountered in the left operand and then by the order
# encountered in the right operand.
#
# When the multiplicities are all zero or one, multiset operations
# are guaranteed to be equivalent to the corresponding operations
# for regular sets.
# Given counter multisets such as:
# cp = Counter(a=1, b=0, c=1)
# cq = Counter(c=1, d=0, e=1)
# The corresponding regular sets would be:
# sp = {'a', 'c'}
# sq = {'c', 'e'}
# All of the following relations would hold:
# set(cp + cq) == sp | sq
# set(cp - cq) == sp - sq
# set(cp | cq) == sp | sq
# set(cp & cq) == sp & sq
# (cp == cq) == (sp == sq)
# (cp != cq) == (sp != sq)
# (cp <= cq) == (sp <= sq)
# (cp < cq) == (sp < sq)
# (cp >= cq) == (sp >= sq)
# (cp > cq) == (sp > sq)
def__eq__(self, other):
'True if all counts agree. Missing counts are treated as zero.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnall(self[e] ==other[e] forcin (self, other) foreinc)
def__ne__(self, other):
'True if any counts disagree. Missing counts are treated as zero.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnnotself==other
def__le__(self, other):
'True if all counts in self are a subset of those in other.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnall(self[e] <=other[e] forcin (self, other) foreinc)
def__lt__(self, other):
'True if all counts in self are a proper subset of those in other.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnself<=otherandself!=other
def__ge__(self, other):
'True if all counts in self are a superset of those in other.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnall(self[e] >=other[e] forcin (self, other) foreinc)
def__gt__(self, other):
'True if all counts in self are a proper superset of those in other.'
ifnotisinstance(other, Counter):
returnNotImplemented
returnself>=otherandself!=other
def__add__(self, other):
'''Add counts from two counters.
>>> Counter('abbb') + Counter('bcc')
Counter({'b': 4, 'c': 2, 'a': 1})
'''
ifnotisinstance(other, Counter):
returnNotImplemented
result=Counter()
forelem, countinself.items():
newcount=count+other[elem]
ifnewcount>0:
result[elem] =newcount
forelem, countinother.items():
ifelemnotinselfandcount>0:
result[elem] =count
returnresult
def__sub__(self, other):
''' Subtract count, but keep only results with positive counts.
>>> Counter('abbbc') - Counter('bccd')
Counter({'b': 2, 'a': 1})
'''
ifnotisinstance(other, Counter):
returnNotImplemented
result=Counter()
forelem, countinself.items():
newcount=count-other[elem]
ifnewcount>0:
result[elem] =newcount
forelem, countinother.items():
ifelemnotinselfandcount<0:
result[elem] =0-count
returnresult
def__or__(self, other):
'''Union is the maximum of value in either of the input counters.
>>> Counter('abbb') | Counter('bcc')
Counter({'b': 3, 'c': 2, 'a': 1})
'''
ifnotisinstance(other, Counter):
returnNotImplemented
result=Counter()
forelem, countinself.items():
other_count=other[elem]
newcount=other_countifcount<other_countelsecount
ifnewcount>0:
result[elem] =newcount
forelem, countinother.items():
ifelemnotinselfandcount>0:
result[elem] =count
returnresult
def__and__(self, other):
''' Intersection is the minimum of corresponding counts.
>>> Counter('abbb') & Counter('bcc')
Counter({'b': 1})
'''
ifnotisinstance(other, Counter):
returnNotImplemented
result=Counter()
forelem, countinself.items():
other_count=other[elem]
newcount=countifcount<other_countelseother_count
ifnewcount>0:
result[elem] =newcount
returnresult
def__pos__(self):
'Adds an empty counter, effectively stripping negative and zero counts'
result=Counter()
forelem, countinself.items():
ifcount>0:
result[elem] =count
returnresult
def__neg__(self):
'''Subtracts from an empty counter. Strips positive and zero counts,
and flips the sign on negative counts.
'''
result=Counter()
forelem, countinself.items():
ifcount<0:
result[elem] =0-count
returnresult
def_keep_positive(self):
'''Internal method to strip elements with a negative or zero count'''
nonpositive= [elemforelem, countinself.items() ifnotcount>0]
foreleminnonpositive:
delself[elem]
returnself
def__iadd__(self, other):
'''Inplace add from another counter, keeping only positive counts.
>>> c = Counter('abbb')
>>> c += Counter('bcc')
>>> c
Counter({'b': 4, 'c': 2, 'a': 1})
'''
forelem, countinother.items():
self[elem] +=count
returnself._keep_positive()
def__isub__(self, other):
'''Inplace subtract counter, but keep only results with positive counts.
>>> c = Counter('abbbc')
>>> c -= Counter('bccd')
>>> c
Counter({'b': 2, 'a': 1})
'''
forelem, countinother.items():
self[elem] -=count
returnself._keep_positive()
def__ior__(self, other):
'''Inplace union is the maximum of value from either counter.
>>> c = Counter('abbb')
>>> c |= Counter('bcc')
>>> c
Counter({'b': 3, 'c': 2, 'a': 1})
'''
forelem, other_countinother.items():
count=self[elem]
ifother_count>count:
self[elem] =other_count
returnself._keep_positive()
def__iand__(self, other):
'''Inplace intersection is the minimum of corresponding counts.
>>> c = Counter('abbb')
>>> c &= Counter('bcc')
>>> c
Counter({'b': 1})
'''
forelem, countinself.items():
other_count=other[elem]
ifother_count<count:
self[elem] =other_count
returnself._keep_positive()
########################################################################
### ChainMap
########################################################################
classChainMap(_collections_abc.MutableMapping):
''' A ChainMap groups multiple dicts (or other mappings) together
to create a single, updateable view.
The underlying mappings are stored in a list. That list is public and can
be accessed or updated using the *maps* attribute. There is no other
state.
Lookups search the underlying mappings successively until a key is found.
In contrast, writes, updates, and deletions only operate on the first
mapping.
'''
def__init__(self, *maps):
'''Initialize a ChainMap by setting *maps* to the given mappings.
If no mappings are provided, a single empty dictionary is used.
'''
self.maps=list(maps) or [{}] # always at least one map
def__missing__(self, key):
raiseKeyError(key)
def__getitem__(self, key):