* teach repr.repr() about collections.deque()

* rename a variable for clarity
This commit is contained in:
Raymond Hettinger 2004-05-21 23:01:18 +00:00
parent ba6cd3647f
commit 1453e4aa75
2 changed files with 10 additions and 2 deletions

View File

@ -15,6 +15,7 @@ class Repr:
self.maxdict = 4
self.maxset = 6
self.maxfrozenset = 6
self.maxdeque = 6
self.maxstring = 30
self.maxlong = 40
self.maxother = 20
@ -37,7 +38,7 @@ class Repr:
s = s[:i] + '...' + s[len(s)-j:]
return s
def _repr_iterable(self, x, level, left, right, maxiter, final=''):
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
n = len(x)
if level <= 0 and n:
s = '...'
@ -47,7 +48,7 @@ class Repr:
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
if n > maxiter: pieces.append('...')
s = ', '.join(pieces)
if n == 1 and final: s += final
if n == 1 and trail: right = trail + right
return '%s%s%s' % (left, s, right)
def repr_tuple(self, x, level):
@ -67,6 +68,9 @@ class Repr:
return self._repr_iterable(x, level, 'frozenset([', '])',
self.maxfrozenset)
def repr_deque(self, x, level):
return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
def repr_dict(self, x, level):
n = len(x)
if n == 0: return '{}'

View File

@ -35,6 +35,7 @@ class ReprTests(unittest.TestCase):
def test_container(self):
from array import array
from collections import deque
eq = self.assertEquals
# Tuples give up after 6 elements
@ -65,6 +66,9 @@ class ReprTests(unittest.TestCase):
eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
# collections.deque after 6
eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
# Dictionaries give up after 4.
eq(r({}), "{}")
d = {'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}