forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.py
91 lines (74 loc) · 2.52 KB
/
collections.py
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
"""
Implements collections.abc for common .NET types
https://docs.python.org/3/library/collections.abc.html
"""
import collections.abc as col
class IteratorMixin(col.Iterator):
def close(self):
if hasattr(self, 'Dispose'):
self.Dispose()
else:
from System import IDisposable
IDisposable(self).Dispose()
class IterableMixin(col.Iterable):
pass
class SizedMixin(col.Sized):
def __len__(self): return self.Count
class ContainerMixin(col.Container):
def __contains__(self, item):
if hasattr('self', 'Contains'):
return self.Contains(item)
else:
from System.Collections.Generic import ICollection
return ICollection(self).Contains(item)
try:
abc_Collection = col.Collection
except AttributeError:
# Python 3.5- does not have collections.abc.Collection
abc_Collection = col.Container
class CollectionMixin(SizedMixin, IterableMixin, ContainerMixin, abc_Collection):
pass
class SequenceMixin(CollectionMixin, col.Sequence):
pass
class MutableSequenceMixin(SequenceMixin, col.MutableSequence):
pass
class MappingMixin(CollectionMixin, col.Mapping):
def __contains__(self, item): return self.ContainsKey(item)
def keys(self): return self.Keys
def items(self): return [(k,self.get(k)) for k in self.Keys]
def values(self): return self.Values
def __iter__(self): return self.Keys.__iter__()
def get(self, key, default=None):
existed, item = self.TryGetValue(key, None)
return item if existed else default
class MutableMappingMixin(MappingMixin, col.MutableMapping):
_UNSET_ = object()
def __delitem__(self, key):
self.Remove(key)
def clear(self):
self.Clear()
def pop(self, key, default=_UNSET_):
existed, item = self.TryGetValue(key, None)
if existed:
self.Remove(key)
return item
elif default == self._UNSET_:
raise KeyError(key)
else:
return default
def setdefault(self, key, value=None):
existed, item = self.TryGetValue(key, None)
if existed:
return item
else:
self[key] = value
return value
def update(self, items, **kwargs):
if isinstance(items, col.Mapping):
for key, value in items.items():
self[key] = value
else:
for key, value in items:
self[key] = value
for key, value in kwargs.items():
self[key] = value