Skip to content

Commit

Permalink
Migrate to doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenDS9 committed Mar 10, 2023
1 parent 70a0f3c commit ce70b45
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions torchdata/datapipes/iter/util/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,37 @@ class IterToMapConverterMapDataPipe(MapDataPipe):
will be replaced by the new value.
Example:
>>> from torchdata.datapipes.iter import IterableWrapper
>>> source_dp = IterableWrapper([(i, i) for i in range(10)])
>>> map_dp = source_dp.to_map_datapipe()
>>> list(map_dp)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> source_dp2 = IterableWrapper([('a', 1), ('b', 2), ('c', 1)])
>>> map_dp2 = source_dp2.to_map_datapipe()
>>> map_dp2['a']
1
>>> def row_to_tuple(row):
>>> label = row[0]
>>> data = row[1:]
>>> return label, data
>>> source_dp3 = IterableWrapper([('a', 1, 1, 1, 1, 1, 1), ('b', 2, 2, 2, 2, 2, 2), ('c', 3, 3, 3, 3, 3, 3)])
>>> map_dp3 = source_dp3.to_map_datapipe(key_value_fn=row_to_tuple)
>>> map_dp3['a']
.. testsetup::
from torchdata.datapipes.iter import IterableWrapper
.. testcode::
source_dp = IterableWrapper([(i, i) for i in range(10)])
map_dp = source_dp.to_map_datapipe()
assert list(map_dp) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
source_dp2 = IterableWrapper([('a', 1), ('b', 2), ('c', 1)])
map_dp2 = source_dp2.to_map_datapipe()
assert map_dp2['a']) == 1
.. testcode::
def row_to_tuple(row):
label = row[0]
data = row[1:]
return label, data
source_dp3 = IterableWrapper([('a', 1, 1, 1, 1, 1, 1), ('b', 2, 2, 2, 2, 2, 2), ('c', 3, 3, 3, 3, 3, 3)])
map_dp3 = source_dp3.to_map_datapipe(key_value_fn=row_to_tuple)
print(map_dp3['a'])
.. testoutput::
(1, 1, 1, 1, 1, 1)
"""

datapipe: IterDataPipe
key_value_fn: Optional[Callable]
_map: Optional[Dict]
Expand Down

0 comments on commit ce70b45

Please sign in to comment.