You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@beam.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2020/07/29 08:00:33 UTC
[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple
to ease migration to Python 3.
[ https://issues.apache.org/jira/browse/BEAM-7840?focusedWorklogId=464088&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-464088 ]
ASF GitHub Bot logged work on BEAM-7840:
----------------------------------------
Author: ASF GitHub Bot
Created on: 29/Jul/20 08:00
Start Date: 29/Jul/20 08:00
Worklog Time Spent: 10m
Work Description: 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:
users@infra.apache.org
Issue Time Tracking
-------------------
Worklog Id: (was: 464088)
Time Spent: 1h 50m (was: 1h 40m)
> 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: P2
> Fix For: 2.15.0
>
> Time Spent: 1h 50m
> 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
(v8.3.4#803005)