Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53dbe39

Browse files
committed
Move UserList to collections.
1 parent 4513ef8 commit 53dbe39

14 files changed

Lines changed: 133 additions & 143 deletions

Doc/library/collections.rst

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
:mod:`collections` --- High-performance container datatypes
3-
===========================================================
2+
:mod:`collections` --- Container datatypes
3+
==========================================
44

55
.. module:: collections
6-
:synopsis: High-performance datatypes
6+
:synopsis: Container datatypes
77
.. moduleauthor:: Raymond Hettinger <[email protected]>
88
.. sectionauthor:: Raymond Hettinger <[email protected]>
99

@@ -663,3 +663,45 @@ In addition to supporting the methods and operations of mappings,
663663
.. attribute:: UserDict.data
664664

665665
A real dictionary used to store the contents of the :class:`UserDict` class.
666+
667+
668+
669+
:class:`UserList` objects
670+
-------------------------
671+
672+
This class acts as a wrapper around list objects. It is a useful base class
673+
for your own list-like classes which can inherit from them and override
674+
existing methods or add new ones. In this way, one can add new behaviors to
675+
lists.
676+
677+
The need for this class has been partially supplanted by the ability to
678+
subclass directly from :class:`list`; however, this class can be easier
679+
to work with because the underlying list is accessible as an attribute.
680+
681+
.. class:: UserList([list])
682+
683+
Class that simulates a list. The instance's contents are kept in a regular
684+
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
685+
instances. The instance's contents are initially set to a copy of *list*,
686+
defaulting to the empty list ``[]``. *list* can be any iterable, for
687+
example a real Python list or a :class:`UserList` object.
688+
689+
In addition to supporting the methods and operations of mutable sequences,
690+
:class:`UserList` instances provide the following attribute:
691+
692+
.. attribute:: UserList.data
693+
694+
A real :class:`list` object used to store the contents of the
695+
:class:`UserList` class.
696+
697+
**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
698+
offer a constructor which can be called with either no arguments or one
699+
argument. List operations which return a new sequence attempt to create an
700+
instance of the actual implementation class. To do so, it assumes that the
701+
constructor can be called with a single parameter, which is a sequence object
702+
used as a data source.
703+
704+
If a derived class does not wish to comply with this requirement, all of the
705+
special methods supported by this class will need to be overridden; please
706+
consult the sources for information about the methods which need to be provided
707+
in that case.

Doc/library/numeric.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The following modules are documented in this chapter:
2121
math.rst
2222
cmath.rst
2323
decimal.rst
24-
rational.rst
24+
fractions.rst
2525
random.rst
2626
itertools.rst
2727
functools.rst

Doc/library/userdict.rst

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
1-
:mod:`UserList` --- Class wrapper for list objects
2-
==================================================
3-
4-
.. module:: UserList
5-
:synopsis: Class wrapper for list objects.
6-
7-
8-
.. note::
9-
10-
This module is available for backward compatibility only. If you are writing
11-
code that does not need to work with versions of Python earlier than Python 2.2,
12-
please consider subclassing directly from the built-in :class:`list` type.
13-
14-
This module defines a class that acts as a wrapper around list objects. It is a
15-
useful base class for your own list-like classes, which can inherit from them
16-
and override existing methods or add new ones. In this way one can add new
17-
behaviors to lists.
18-
19-
The :mod:`UserList` module defines the :class:`UserList` class:
20-
21-
22-
.. class:: UserList([list])
23-
24-
Class that simulates a list. The instance's contents are kept in a regular
25-
list, which is accessible via the :attr:`data` attribute of
26-
:class:`UserList`
27-
instances. The instance's contents are initially set to a copy of *list*,
28-
defaulting to the empty list ``[]``. *list* can be any iterable, for
29-
example a real Python list or a :class:`UserList` object.
30-
31-
In addition to supporting the methods and operations of mutable sequences (see
32-
section :ref:`typesseq`), :class:`UserList` instances provide the following
33-
attribute:
34-
35-
36-
.. attribute:: UserList.data
37-
38-
A real Python list object used to store the contents of the :class:`UserList`
39-
class.
40-
41-
**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
42-
offer a constructor which can be called with either no arguments or one
43-
argument. List operations which return a new sequence attempt to create an
44-
instance of the actual implementation class. To do so, it assumes that the
45-
constructor can be called with a single parameter, which is a sequence object
46-
used as a data source.
47-
48-
If a derived class does not wish to comply with this requirement, all of the
49-
special methods supported by this class will need to be overridden; please
50-
consult the sources for information about the methods which need to be provided
51-
in that case.
52-
531

542
:mod:`UserString` --- Class wrapper for string objects
553
======================================================

Lib/UserList.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

Lib/collections.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,80 @@ def fromkeys(cls, iterable, value=None):
162162

163163

164164

165+
################################################################################
166+
### UserList
167+
################################################################################
168+
169+
class UserList(MutableSequence):
170+
"""A more or less complete user-defined wrapper around list objects."""
171+
def __init__(self, initlist=None):
172+
self.data = []
173+
if initlist is not None:
174+
# XXX should this accept an arbitrary sequence?
175+
if type(initlist) == type(self.data):
176+
self.data[:] = initlist
177+
elif isinstance(initlist, UserList):
178+
self.data[:] = initlist.data[:]
179+
else:
180+
self.data = list(initlist)
181+
def __repr__(self): return repr(self.data)
182+
def __lt__(self, other): return self.data < self.__cast(other)
183+
def __le__(self, other): return self.data <= self.__cast(other)
184+
def __eq__(self, other): return self.data == self.__cast(other)
185+
def __ne__(self, other): return self.data != self.__cast(other)
186+
def __gt__(self, other): return self.data > self.__cast(other)
187+
def __ge__(self, other): return self.data >= self.__cast(other)
188+
def __cast(self, other):
189+
return other.data if isinstance(other, UserList) else other
190+
def __cmp__(self, other):
191+
return cmp(self.data, self.__cast(other))
192+
def __contains__(self, item): return item in self.data
193+
def __len__(self): return len(self.data)
194+
def __getitem__(self, i): return self.data[i]
195+
def __setitem__(self, i, item): self.data[i] = item
196+
def __delitem__(self, i): del self.data[i]
197+
def __add__(self, other):
198+
if isinstance(other, UserList):
199+
return self.__class__(self.data + other.data)
200+
elif isinstance(other, type(self.data)):
201+
return self.__class__(self.data + other)
202+
return self.__class__(self.data + list(other))
203+
def __radd__(self, other):
204+
if isinstance(other, UserList):
205+
return self.__class__(other.data + self.data)
206+
elif isinstance(other, type(self.data)):
207+
return self.__class__(other + self.data)
208+
return self.__class__(list(other) + self.data)
209+
def __iadd__(self, other):
210+
if isinstance(other, UserList):
211+
self.data += other.data
212+
elif isinstance(other, type(self.data)):
213+
self.data += other
214+
else:
215+
self.data += list(other)
216+
return self
217+
def __mul__(self, n):
218+
return self.__class__(self.data*n)
219+
__rmul__ = __mul__
220+
def __imul__(self, n):
221+
self.data *= n
222+
return self
223+
def append(self, item): self.data.append(item)
224+
def insert(self, i, item): self.data.insert(i, item)
225+
def pop(self, i=-1): return self.data.pop(i)
226+
def remove(self, item): self.data.remove(item)
227+
def count(self, item): return self.data.count(item)
228+
def index(self, item, *args): return self.data.index(item, *args)
229+
def reverse(self): self.data.reverse()
230+
def sort(self, *args, **kwds): self.data.sort(*args, **kwds)
231+
def extend(self, other):
232+
if isinstance(other, UserList):
233+
self.data.extend(other.data)
234+
else:
235+
self.data.extend(other)
236+
237+
238+
165239
################################################################################
166240
### Simple tests
167241
################################################################################

Lib/test/string_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import unittest, string, sys, struct
66
from test import test_support
7-
from UserList import UserList
7+
from collections import UserList
88

99
class Sequence:
1010
def __init__(self, seq='wxyz'): self.seq = seq

Lib/test/test_bisect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test import test_support
33
from bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect
4-
from UserList import UserList
4+
from collections import UserList
55

66
class TestBisect(unittest.TestCase):
77

Lib/test/test_builtin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
run_with_locale
66
from operator import neg
77

8-
import sys, warnings, random, collections, io, rational, fractions
8+
import sys, warnings, random, collections, io, fractions
99
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
1010
FutureWarning, __name__)
1111
warnings.filterwarnings("ignore", "integer argument expected",
@@ -210,7 +210,7 @@ def test_cmp(self):
210210
# verify that circular objects are not handled
211211
a = []; a.append(a)
212212
b = []; b.append(b)
213-
from UserList import UserList
213+
from collections import UserList
214214
c = UserList(); c.append(c)
215215
self.assertRaises(RuntimeError, cmp, a, b)
216216
self.assertRaises(RuntimeError, cmp, b, c)

Lib/test/test_extcall.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from test.test_support import verify, verbose, TestFailed, sortdict
2-
from UserList import UserList
3-
from collections import UserDict
2+
from collections import UserDict, UserList
43

54
def e(a, b):
65
print(a, b)

Lib/test/test_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from weakref import proxy
66

77
from test.test_support import TESTFN, findfile, run_unittest
8-
from UserList import UserList
8+
from collections import UserList
99

1010
class AutoFileTests(unittest.TestCase):
1111
# file tests for which a test file is automatically set up

0 commit comments

Comments
 (0)