Commit 4421a558 authored by Evan Simpson's avatar Evan Simpson

Rename 'total' to 'sequence_length' and make it lazy.

parent 72f599e1
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
############################################################################## ##############################################################################
__doc__='''Batch class, for iterating over a sequence in batches __doc__='''Batch class, for iterating over a sequence in batches
$Id: Batch.py,v 1.8 2002/03/15 16:01:52 chrisw Exp $''' $Id: Batch.py,v 1.9 2002/03/15 21:47:38 evan Exp $'''
__version__='$Revision: 1.8 $'[11:-2] __version__='$Revision: 1.9 $'[11:-2]
from ExtensionClass import Base from ExtensionClass import Base
...@@ -31,12 +31,18 @@ class LazyNextBatch(Base): ...@@ -31,12 +31,18 @@ class LazyNextBatch(Base):
parent.end - parent.overlap, 0, parent.end - parent.overlap, 0,
parent.orphan, parent.overlap) parent.orphan, parent.overlap)
class LazySequenceLength(Base):
def __of__(self, parent):
parent.sequence_length = l = len(parent._sequence)
return l
class Batch(Base): class Batch(Base):
"""Create a sequence batch""" """Create a sequence batch"""
__allow_access_to_unprotected_subobjects__ = 1 __allow_access_to_unprotected_subobjects__ = 1
previous = LazyPrevBatch() previous = LazyPrevBatch()
next = LazyNextBatch() next = LazyNextBatch()
sequence_length = LazySequenceLength()
def __init__(self, sequence, size, start=0, end=0, def __init__(self, sequence, size, start=0, end=0,
orphan=0, overlap=0): orphan=0, overlap=0):
...@@ -54,7 +60,7 @@ class Batch(Base): ...@@ -54,7 +60,7 @@ class Batch(Base):
0-based index. "length" is the actual number of elements in 0-based index. "length" is the actual number of elements in
the batch. the batch.
"total" is the length of the original, unbatched, sequence "sequence_length" is the length of the original, unbatched, sequence
''' '''
start = start + 1 start = start + 1
...@@ -70,7 +76,6 @@ class Batch(Base): ...@@ -70,7 +76,6 @@ class Batch(Base):
self.overlap = overlap self.overlap = overlap
self.first = max(start - 1, 0) self.first = max(start - 1, 0)
self.length = self.end - self.first self.length = self.end - self.first
self.total = len(sequence)
if self.first == 0: if self.first == 0:
self.previous = None self.previous = None
......
...@@ -4,11 +4,11 @@ ZTUtils changes ...@@ -4,11 +4,11 @@ ZTUtils changes
Change information for previous versions can be found in the Change information for previous versions can be found in the
file HISTORY.txt. file HISTORY.txt.
After Version 1.1.3 After Version 1.1.4
Features Added Features Added
- Added useful 'total' attribute to batches. - Added 'sequence_length' attribute to batches.
Version 1.1.4 Version 1.1.4
......
...@@ -23,7 +23,7 @@ class BatchTests(TestCase): ...@@ -23,7 +23,7 @@ class BatchTests(TestCase):
assert b.next is None assert b.next is None
assert b.start == 1, b.start assert b.start == 1, b.start
assert len(b) == b.end == bsize assert len(b) == b.end == bsize
assert b.total == len(seq) assert b.sequence_length == len(seq)
for i in seq: for i in seq:
assert b[i] == i, (b[i], i) assert b[i] == i, (b[i], i)
neg = -1 - i neg = -1 - i
...@@ -36,10 +36,10 @@ class BatchTests(TestCase): ...@@ -36,10 +36,10 @@ class BatchTests(TestCase):
assert b.next is None assert b.next is None
assert len(b) == bsize assert len(b) == bsize
assert b[bsize - 1] == bsize - 1 assert b[bsize - 1] == bsize - 1
assert b.total == bsize assert b.sequence_length == bsize
b = Batch(range(8), 5) b = Batch(range(8), 5)
assert len(b) == 5 assert len(b) == 5
assert b.total == 8 assert b.sequence_length == 8
assert len(b.next) == 3 assert len(b.next) == 3
def test_suite(): def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment