[
https://issues.apache.org/jira/browse/BEAM-7840?focusedWorklogId=294289&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-294289
]
ASF GitHub Bot logged work on BEAM-7840:
----------------------------------------
Author: ASF GitHub Bot
Created on: 14/Aug/19 00:00
Start Date: 14/Aug/19 00:00
Worklog Time Spent: 10m
Work Description: udim commented on pull request #9168: [BEAM-7840]
Provide MapTuple and FlatMapTuple for Python 3 users.
URL: https://github.com/apache/beam/pull/9168#discussion_r313659791
##########
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.')
+
+ if argspec.defaults or args or kwargs:
+ wrapper = lambda x, *args, **kwargs: [fn(*(tuple(x) + args), **kwargs)]
Review comment:
I was looking at this code and wondered why is there a `tuple(x)` not `x`,
since x should always be tuple according to the docstring? The tests pass with
a plain `x`.
----------------------------------------------------------------
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: 294289)
Time Spent: 1h 40m (was: 1.5h)
> 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
> Assignee: Robert Bradshaw
> Priority: Major
> Fix For: 2.15.0
>
> Time Spent: 1h 40m
> 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)