summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSybren A. St?vel <sybren@stuvel.eu>2011-07-31 19:54:53 +0200
committerSybren A. St?vel <sybren@stuvel.eu>2011-07-31 19:54:53 +0200
commitd60005063ce734cc963e4a93e1406d22ccb1383b (patch)
tree53093b5e0367185d93f367542abfcfc2c6f49f78
parent782f831e8d4f0a02250a5457e96040f1fcf56b36 (diff)
downloadrsa-d60005063ce734cc963e4a93e1406d22ccb1383b.tar.gz
Added yield_fixedblock
-rw-r--r--rsa/blocks.py19
-rw-r--r--tests/test_blocks.py7
2 files changed, 26 insertions, 0 deletions
diff --git a/rsa/blocks.py b/rsa/blocks.py
index c38c1f2..0250566 100644
--- a/rsa/blocks.py
+++ b/rsa/blocks.py
@@ -132,3 +132,22 @@ def yield_varblocks(infile):
yield block
+def yield_fixedblocks(infile, blocksize):
+ '''Generator, yields each block of ``blocksize`` bytes in the input file.
+
+ :param infile: file to read and separate in blocks.
+ :returns: a generator that yields the contents of each block
+ '''
+
+ while True:
+ block = infile.read(blocksize)
+
+ read_bytes = len(block)
+ if read_bytes == 0:
+ break
+
+ yield block
+
+ if read_bytes < blocksize:
+ break
+
diff --git a/tests/test_blocks.py b/tests/test_blocks.py
index 8481c3c..ce5f03a 100644
--- a/tests/test_blocks.py
+++ b/tests/test_blocks.py
@@ -65,4 +65,11 @@ class VarblockTest(unittest.TestCase):
varblocks = list(blocks.yield_varblocks(infile))
self.assertEqual(['12345', 'Sybren'], varblocks)
+class FixedblockTest(unittest.TestCase):
+ def test_yield_fixedblock(self):
+
+ infile = StringIO('123456Sybren')
+
+ fixedblocks = list(blocks.yield_fixedblocks(infile, 6))
+ self.assertEqual(['123456', 'Sybren'], fixedblocks)