shoyer commented on a change in pull request #9168:
URL: https://github.com/apache/beam/pull/9168#discussion_r462035731
##########
File path: sdks/python/apache_beam/transforms/core.py
##########
@@ -1316,6 +1322,144 @@ 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))
+
+ label = 'MapTuple(%s)' % ptransform.label_from_callable(fn)
+
+ argspec = getfullargspec(fn)
+ num_defaults = len(argspec.defaults or ())
+ if num_defaults < len(args) + len(kwargs):
+ raise TypeError('Side inputs must have defaults for MapTuple.')
Review comment:
@robertwb I'm quite curious about the reason for this restriction,
which doesn't exist on `beam.Map`.
Is this just a detail of the current implementation that could potentially
removed, or is there a more fundamental reason for it?
Also, would keyword only arguments be OK on Python 3? Based on the
implementation, I would assume the answer is
----------------------------------------------------------------
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]