Implment __len__ magic method

This commit is contained in:
Brant Watson 2016-10-24 07:19:32 -05:00
parent 5c45f57961
commit 0f9581cd85
2 changed files with 7 additions and 2 deletions

View File

@ -172,6 +172,9 @@ class SpooledIOBase(object):
self.seek(pos)
return val
def __len__(self):
return self.len
def __iter__(self):
yield self.readline()

View File

@ -197,15 +197,17 @@ class TestSpooledBytesIO(TestCase, BaseTestMixin, AssertionsMixin):
self.assertEqual(f.getvalue(), test_str)
def test_len_no_rollover(self):
"""Make sure len property works with in-memory flo"""
"""Make sure len works with in-memory flo"""
self.spooled_flo.write(self.test_str)
self.assertEqual(self.spooled_flo.len, len(self.test_str))
self.assertEqual(len(self.spooled_flo), len(self.test_str))
def test_len_rollover(self):
"""Make sure len property works with on-disk flo"""
"""Make sure len works with on-disk flo"""
self.spooled_flo.write(self.test_str)
self.spooled_flo.rollover()
self.assertEqual(self.spooled_flo.len, len(self.test_str))
self.assertEqual(len(self.spooled_flo), len(self.test_str))
def test_invalid_type(self):
"""Ensure TypeError raised when writing unicode to SpooledBytesIO"""