[ 
https://issues.apache.org/jira/browse/BEAM-7840?focusedWorklogId=284968&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-284968
 ]

ASF GitHub Bot logged work on BEAM-7840:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 30/Jul/19 14:05
            Start Date: 30/Jul/19 14:05
    Worklog Time Spent: 10m 
      Work Description: tvalentyn commented on pull request #9168: [BEAM-7840] 
Provide MapTuple and FlatMapTuple for Python 3 users.
URL: https://github.com/apache/beam/pull/9168#discussion_r308707480
 
 

 ##########
 File path: sdks/python/apache_beam/transforms/core.py
 ##########
 @@ -1316,6 +1322,142 @@ def Map(fn, *args, **kwargs):  # pylint: 
disable=invalid-name
   return pardo
 
 
+def MapTuple(fn, *args, **kwargs):  # pylint: disable=invalid-name
+  r""":func:`MapTuple` is like :func:`Map` but expects tuple inputs and
+  flattens them into multiple input arguments.
+
+      beam.MapTuple(lambda a, b, ...: ...)
+
+  is equivalent to Python 2
+
+      beam.Map(lambda (a, b, ...), ...: ...)
+
+  In other words
+
+      beam.MapTuple(fn)
+
+  is equivalent to
+
+      beam.Map(lambda element, ...: fn(\*element, ...))
+
+  This can be useful when processing a PCollection of tuples
+  (e.g. key-value pairs).
+
+  Args:
+    fn (callable): a callable object.
+    *args: positional arguments passed to the transform callable.
+    **kwargs: keyword arguments passed to the transform callable.
+
+  Returns:
+    ~apache_beam.pvalue.PCollection:
+    A :class:`~apache_beam.pvalue.PCollection` containing the
+    :func:`MapTuple` outputs.
+
+  Raises:
+    ~exceptions.TypeError: If the **fn** passed as argument is not a callable.
+      Typical error is to pass a :class:`DoFn` instance which is supported only
+      for :class:`ParDo`.
+  """
+  if not callable(fn):
+    raise TypeError(
+        'MapTuple can be used only with callable objects. '
+        'Received %r instead.' % (fn))
+
+  if _fn_takes_side_inputs(fn):
 
 Review comment:
   implementation of `_fn_takes_side_inputs` relies on a fact that `fn` accepts 
only one argument that is not a side-input. It seems that this assumption does 
not hold for fn that expects an unfolded tuple  (multiple arguments which are 
not side-inputs). Should we just remove the `if` and always go through the 
positive branch? There is the same branch in `FlatMapTuple`, and actually in 
`Map`/`FlatMap` as well. I wonder if this could be simplified everywhere. WDYT?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 284968)

> Create MapTuple and FlatMapTuple to ease migration to Python 3.
> ---------------------------------------------------------------
>
>                 Key: BEAM-7840
>                 URL: https://issues.apache.org/jira/browse/BEAM-7840
>             Project: Beam
>          Issue Type: Test
>          Components: sdk-py-core
>            Reporter: Robert Bradshaw
>            Priority: Major
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> These are like Map and FlatMap but expand out tuple input elements across
> several arguments. This will be useful as tuple argument unpacking has been
> removed in Python 3. Instead of having to convert
>     Map(lambda (k, v): expresion(k, v))
> into
>     Map(lambda k_v: expression(k_v[0], k_v[1]))
> one can now write
>     MapTuple(lambda k, v: expression(k, v))



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to