[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-08-13 Thread ASF GitHub Bot (JIRA)


 [ 
https://issues.apache.org/jira/browse/BEAM-7840?focusedWorklogId=294289=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:
us...@infra.apache.org


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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-31 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


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

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285561)
Time Spent: 1.5h  (was: 1h 20m)

> 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: 1.5h
>  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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


Author: ASF GitHub Bot
Created on: 30/Jul/19 14:56
Start Date: 30/Jul/19 14:56
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_r308772014
 
 

 ##
 File path: sdks/python/apache_beam/transforms/core.py
 ##
 @@ -1431,24 +1432,25 @@ def FlatMapTuple(fn, *args, **kwargs):  # pylint: 
disable=invalid-name
 'MapTuple can be used only with callable objects. '
 
 Review comment:
   s/MapTuple/FlatMapTuple in line 1432
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285019)
Time Spent: 1h 20m  (was: 1h 10m)

> 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 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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


Author: ASF GitHub Bot
Created on: 30/Jul/19 14:53
Start Date: 30/Jul/19 14:53
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_r308770160
 
 

 ##
 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:
   Thanks, good to know.
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285017)
Time Spent: 1h 10m  (was: 1h)

> 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 10m
>  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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


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

 ##
 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):
+wrapper = lambda x, *args, **kwargs: [fn(*(tuple(x) + args), **kwargs)]
+  else:
+wrapper = lambda x: [fn(*x)]
 
 Review comment:
   The Python interpreter will do that for us. As this is the fast path, 
probably better avoid the extra cast. 
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285016)
Time Spent: 1h  (was: 50m)

> 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
>  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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


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

 ##
 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:
   Nice catch. 
   
   Calling a function without varargs and kwargs is over twice as fast (e.g. no 
need to create empty dictionaries), which is why these branches were 
introduced. I've fixed the logic. 
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285015)
Time Spent: 50m  (was: 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: Major
> Fix For: 2.15.0
>
>  Time Spent: 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
(v7.6.14#76016)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


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

 ##
 File path: sdks/python/apache_beam/pipeline_test.py
 ##
 @@ -168,6 +168,35 @@ def test_flatmap_builtin(self):
 assert_that(pcoll4, equal_to([11, 12, 12, 12, 13]), label='pcoll4')
 pipeline.run()
 
+  def test_maptuple_builtin(self):
+pipeline = TestPipeline()
+pcoll = pipeline | Create([('e1', 'e2')])
+side1 = beam.pvalue.AsSingleton(pipeline | 'side1' >> Create(['s1']))
+side2 = beam.pvalue.AsSingleton(pipeline | 'side2' >> Create(['s2']))
+
+# A test function with a tuple input, an auxiliary parameter,
+# and some side inputs.
+fn = lambda e1, e2, t=DoFn.TimestampParam, s1=None, s2=None: (
+e1, e2, t, s1, s2)
+assert_that(pcoll | 'NoSides' >> beam.core.MapTuple(fn),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, None, None)]),
+label='NoSidesCheck')
+assert_that(pcoll | 'StaticSides' >> beam.core.MapTuple(fn, 's1', 's2'),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, 's1', 's2')]),
+label='StaticSidesCheck')
+assert_that(pcoll | 'DynamicSides' >> beam.core.MapTuple(fn, side1, side2),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, 's1', 's2')]),
+label='DynamicSidesCheck')
+assert_that(pcoll | 'MixedSides' >> beam.core.MapTuple(fn, s2=side2),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, None, 's2')]),
+label='MixedSidesCheck')
+
+# FlatMapTuple is similar.
+assert_that(pcoll | 'FlatMap' >> beam.core.FlatMapTuple(fn, s2=side2),
 
 Review comment:
   Done.
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285011)
Time Spent: 0.5h  (was: 20m)

> 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: 0.5h
>  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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

ASF GitHub Bot logged work on BEAM-7840:


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

 ##
 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):
+wrapper = lambda x, *args, **kwargs: [fn(*(tuple(x) + args), **kwargs)]
+  else:
+wrapper = lambda x: [fn(*x)]
 
 Review comment:
   Set.
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 285012)
Time Spent: 40m  (was: 0.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: 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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

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_r308739128
 
 

 ##
 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):
+wrapper = lambda x, *args, **kwargs: [fn(*(tuple(x) + args), **kwargs)]
+  else:
+wrapper = lambda x: [fn(*x)]
 
 Review comment:
   If we keep `else` branch, we probably want to cast `x` to tuple as well.
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 284966)
Time Spent: 20m  (was: 10m)

> 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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

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_r308703727
 
 

 ##
 File path: sdks/python/apache_beam/pipeline_test.py
 ##
 @@ -168,6 +168,35 @@ def test_flatmap_builtin(self):
 assert_that(pcoll4, equal_to([11, 12, 12, 12, 13]), label='pcoll4')
 pipeline.run()
 
+  def test_maptuple_builtin(self):
+pipeline = TestPipeline()
+pcoll = pipeline | Create([('e1', 'e2')])
+side1 = beam.pvalue.AsSingleton(pipeline | 'side1' >> Create(['s1']))
+side2 = beam.pvalue.AsSingleton(pipeline | 'side2' >> Create(['s2']))
+
+# A test function with a tuple input, an auxiliary parameter,
+# and some side inputs.
+fn = lambda e1, e2, t=DoFn.TimestampParam, s1=None, s2=None: (
+e1, e2, t, s1, s2)
+assert_that(pcoll | 'NoSides' >> beam.core.MapTuple(fn),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, None, None)]),
+label='NoSidesCheck')
+assert_that(pcoll | 'StaticSides' >> beam.core.MapTuple(fn, 's1', 's2'),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, 's1', 's2')]),
+label='StaticSidesCheck')
+assert_that(pcoll | 'DynamicSides' >> beam.core.MapTuple(fn, side1, side2),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, 's1', 's2')]),
+label='DynamicSidesCheck')
+assert_that(pcoll | 'MixedSides' >> beam.core.MapTuple(fn, s2=side2),
+equal_to([('e1', 'e2', MIN_TIMESTAMP, None, 's2')]),
+label='MixedSidesCheck')
+
+# FlatMapTuple is similar.
+assert_that(pcoll | 'FlatMap' >> beam.core.FlatMapTuple(fn, s2=side2),
 
 Review comment:
   Please move this to test_flatmaptuple_builtin and add repeat all scenarios.
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 284965)
Time Spent: 10m
Remaining Estimate: 0h

> 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: 10m
>  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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


 [ 
https://issues.apache.org/jira/browse/BEAM-7840?focusedWorklogId=284968=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:
us...@infra.apache.org


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)


[jira] [Work logged] (BEAM-7840) Create MapTuple and FlatMapTuple to ease migration to Python 3.

2019-07-30 Thread ASF GitHub Bot (JIRA)


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

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_r308740823
 
 

 ##
 File path: sdks/python/apache_beam/examples/snippets/snippets.py
 ##
 @@ -446,7 +446,7 @@ def examples_wordcount_minimal(renames):
   # [END examples_wordcount_minimal_count]
 
   # [START examples_wordcount_minimal_map]
-  | beam.Map(lambda word_count: '%s: %s' % (word_count[0], word_count[1]))
 
 Review comment:
   Ok, we should tag associated Jira with 'fix_version==2.15.0' in this case. 
 

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:
us...@infra.apache.org


Issue Time Tracking
---

Worklog Id: (was: 284967)
Time Spent: 20m  (was: 10m)

> 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)