/
_collections.py
197 lines (145 loc) · 5.17 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
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
# -*- coding: utf-8 -*-
"""
==============================================================================
Custom container datatypes (:mod:`sknano.core._collections`)
==============================================================================
.. currentmodule:: sknano.core._collections
"""
from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals
__docformat__ = 'restructuredtext en'
try:
from collections.abc import Mapping, MutableSequence, Set
except ImportError:
from collections import Mapping, MutableSequence, Set
__all__ = ['ListBasedSet', 'UserList', 'frozendict']
# class AttrDict(dict):
# def __init__(self, *args, **kwargs):
# super(AttrDict, self).__init__(*args, **kwargs)
# self.__dict__ = self
class ListBasedSet(Set):
"""Alternate set implementation favoring space over speed and not
requiring the set elements to be hashable.
Parameters
----------
iterable
"""
def __init__(self, iterable):
self.elements = elements = []
for value in iterable:
if value not in elements:
elements.append(value)
def __iter__(self):
return iter(self.elements)
def __contains__(self, value):
return value in self.elements
def __len__(self):
return len(self.elements)
class UserList(MutableSequence):
"""Modified implementation of :class:`~python:collections.UserList`.
Constructor takes an additional variable keyword argument.
Parameters
----------
initlist : {None, sequence, UserList}, optional
if not `None`, then a list or an instance of `UserList`
"""
def __init__(self, initlist=None, **kwargs):
self.data = []
if initlist is not None:
if type(initlist) == type(self.data):
self.data[:] = initlist
elif isinstance(initlist, UserList):
self.data[:] = initlist.data[:]
else:
self.data = list(initlist)
self.kwargs = kwargs
def __repr__(self):
return repr(self.data)
def __lt__(self, other):
return self.data < self.__cast(other)
def __le__(self, other):
return self.data <= self.__cast(other)
def __eq__(self, other):
return self.data == self.__cast(other)
def __ne__(self, other):
return self.data != self.__cast(other)
def __gt__(self, other):
return self.data > self.__cast(other)
def __ge__(self, other):
return self.data >= self.__cast(other)
def __cast(self, other):
return other.data if isinstance(other, UserList) else other
def __contains__(self, item):
return item in self.data
def __len__(self):
return len(self.data)
def __getitem__(self, i):
return self.data[i]
def __setitem__(self, i, item):
self.data[i] = item
def __delitem__(self, i):
del self.data[i]
def __add__(self, other):
if isinstance(other, UserList):
return self.__class__(self.data + other.data, **self.kwargs)
elif isinstance(other, type(self.data)):
return self.__class__(self.data + other, **self.kwargs)
return self.__class__(self.data + list(other), **self.kwargs)
def __radd__(self, other):
if isinstance(other, UserList):
return self.__class__(other.data + self.data, **self.kwargs)
elif isinstance(other, type(self.data)):
return self.__class__(other + self.data, **self.kwargs)
return self.__class__(list(other) + self.data, **self.kwargs)
def __iadd__(self, other):
if isinstance(other, UserList):
self.data += other.data
elif isinstance(other, type(self.data)):
self.data += other
else:
self.data += list(other)
return self
def __mul__(self, n):
return self.__class__(self.data * n, **self.kwargs)
__rmul__ = __mul__
def __imul__(self, n):
self.data *= n
return self
def append(self, item):
self.data.append(item)
def insert(self, i, item):
self.data.insert(i, item)
def pop(self, i=-1):
return self.data.pop(i)
def remove(self, item):
self.data.remove(item)
def clear(self):
try:
self.data.clear()
except AttributeError:
del self.data[:]
def copy(self):
return self.__class__(self, **self.kwargs)
def count(self, item):
return self.data.count(item)
def index(self, item, *args):
return self.data.index(item, *args)
def reverse(self):
self.data.reverse()
def sort(self, *args, **kwds):
self.data.sort(*args, **kwds)
def extend(self, other):
if isinstance(other, UserList):
self.data.extend(other.data)
else:
self.data.extend(other)
class frozendict(Mapping):
"""A simple implementation of a read-only *frozen* `dict`."""
def __init__(self, data):
self._data = data
def __getitem__(self, key):
return self._data[key]
def __len__(self):
return len(self._data)
def __iter__(self):
return iter(self._data)