提交 7c085d96 编写于 作者: Y Yu Yang 提交者: GitHub

Merge pull request #1425 from helinwang/map

add decorator: map_readers, change from google python style to pep 287
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
__all__ = ['buffered', 'compose', 'chain', 'shuffle', 'ComposeNotAligned'] __all__ = [
'map_readers', 'buffered', 'compose', 'chain', 'shuffle',
'ComposeNotAligned'
]
from Queue import Queue from Queue import Queue
from threading import Thread from threading import Thread
...@@ -20,20 +23,38 @@ import itertools ...@@ -20,20 +23,38 @@ import itertools
import random import random
def map_readers(func, *readers):
"""
Creates a data reader that outputs return value of function using
output of each data readers as arguments.
:param func: function to use.
:param *readers: readers whose outputs will be used as arguments of func.
:returns: the created data reader.
"""
def reader():
rs = []
for r in readers:
rs.append(r())
for e in itertools.imap(func, *rs):
yield e
return reader
def shuffle(reader, buf_size): def shuffle(reader, buf_size):
"""Creates a data reader whose data output is suffled. """
Creates a data reader whose data output is suffled.
Output from the iterator that created by original reader will be Output from the iterator that created by original reader will be
buffered into shuffle buffer, and then shuffled. The size of shuffle buffer buffered into shuffle buffer, and then shuffled. The size of shuffle buffer
is determined by argument buf_size. is determined by argument buf_size.
Args: :param reader: the original reader whose output will be shuffled.
reader: the original reader whose output will be :param buf_size: shuffle buffer size.
shuffled.
buf_size: shuffle buffer size.
Returns: :returns:the new reader whose output is shuffled.
the new reader whose output is shuffled.
""" """
def data_reader(): def data_reader():
...@@ -55,8 +76,9 @@ def shuffle(reader, buf_size): ...@@ -55,8 +76,9 @@ def shuffle(reader, buf_size):
def chain(*readers): def chain(*readers):
"""Creates a data reader whose output is the outputs of input data """
readers chained together. Creates a data reader whose output is the outputs of input data
readers chained together.
If input readers output following data entries: If input readers output following data entries:
[0, 0, 0] [0, 0, 0]
...@@ -65,11 +87,8 @@ def chain(*readers): ...@@ -65,11 +87,8 @@ def chain(*readers):
The chained reader will output: The chained reader will output:
[0, 0, 0, 1, 1, 1, 2, 2, 2] [0, 0, 0, 1, 1, 1, 2, 2, 2]
Args: :param readers: input readers.
readers: input readers. :returns: the new data reader.
Returns:
the new data reader.
""" """
def reader(): def reader():
...@@ -88,25 +107,23 @@ class ComposeNotAligned(ValueError): ...@@ -88,25 +107,23 @@ class ComposeNotAligned(ValueError):
def compose(*readers, **kwargs): def compose(*readers, **kwargs):
"""Creates a data reader whose output is the combination of input readers. """
Creates a data reader whose output is the combination of input readers.
If input readers output following data entries: If input readers output following data entries:
(1, 2) 3 (4, 5) (1, 2) 3 (4, 5)
The composed reader will output: The composed reader will output:
(1, 2, 3, 4, 5) (1, 2, 3, 4, 5)
Args: :*readers: readers that will be composed together.
*readers: readers that will be composed together. :check_alignment: if True, will check if input readers are aligned
check_alignment: If True, will check if input readers are aligned correctly. If False, will not check alignment and trailing outputs
correctly. If False, will not check alignment and trailing outputs will be discarded. Defaults to True.
will be discarded. Defaults to True.
Returns: :returns: the new data reader.
the new data reader.
Raises: :raises ComposeNotAligned: outputs of readers are not aligned.
ComposeNotAligned: outputs of readers are not aligned. Will not raise when check_alignment is set to False.
Will not raise when check_alignment is set to False.
""" """
check_alignment = kwargs.pop('check_alignment', True) check_alignment = kwargs.pop('check_alignment', True)
...@@ -136,18 +153,17 @@ def compose(*readers, **kwargs): ...@@ -136,18 +153,17 @@ def compose(*readers, **kwargs):
def buffered(reader, size): def buffered(reader, size):
"""Creates a buffered data reader. """
Creates a buffered data reader.
The buffered data reader will read and save data entries into a The buffered data reader will read and save data entries into a
buffer. Reading from the buffered data reader will proceed as long buffer. Reading from the buffered data reader will proceed as long
as the buffer is not empty. as the buffer is not empty.
Args: :param reader: the data reader to read from.
reader: the data reader to read from. :param size: max buffer size.
size: max buffer size.
Returns: :returns: the buffered data reader.
The buffered data reader.
""" """
class EndSignal(): class EndSignal():
......
...@@ -26,6 +26,22 @@ def reader_creator_10(dur): ...@@ -26,6 +26,22 @@ def reader_creator_10(dur):
return reader return reader
class TestMap(unittest.TestCase):
def test_map(self):
d = {"h": 0, "i": 1}
def tokenize(x):
return d[x]
def read():
yield "h"
yield "i"
r = paddle.reader.map_readers(tokenize, read)
for i, e in enumerate(r()):
self.assertEqual(e, i)
class TestBuffered(unittest.TestCase): class TestBuffered(unittest.TestCase):
def test_read(self): def test_read(self):
for size in range(20): for size in range(20):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册