提交 dbfe58ca 编写于 作者: G gongweibao

add local recordio reader interface

上级 7bce40d7
...@@ -16,7 +16,7 @@ Creator package contains some simple reader creator, which could be used in user ...@@ -16,7 +16,7 @@ Creator package contains some simple reader creator, which could be used in user
program. program.
""" """
__all__ = ['np_array', 'text_file'] __all__ = ['np_array', 'text_file', "RecordIO"]
def np_array(x): def np_array(x):
...@@ -55,3 +55,22 @@ def text_file(path): ...@@ -55,3 +55,22 @@ def text_file(path):
f.close() f.close()
return reader return reader
def RecordIO(path):
"""
Creates a data reader that outputs record one one by one from given recordio file
:path: path of recordio file
:returns: data reader of recordio file
"""
def reader():
f = recordio.reader(path)
while True:
r = f.read()
if r is None:
break
yield r
f.close()
return reader
...@@ -36,5 +36,14 @@ class TestTextFile(unittest.TestCase): ...@@ -36,5 +36,14 @@ class TestTextFile(unittest.TestCase):
self.assertEqual(e, str(idx * 2) + " " + str(idx * 2 + 1)) self.assertEqual(e, str(idx * 2) + " " + str(idx * 2 + 1))
class TestRecordIO(unittest.TestCase):
def test_RecordIO(self):
path = os.path.join(
os.path.dirname(__file__), "test_recordio_creator.dat")
reader = paddle.v2.reader.creator.RecordIO(path)
for idx, r in enumerate(reader()):
self.assertSequenceEqual(r, str(idx))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册