You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/06/10 17:32:14 UTC

[GitHub] [beam] robertwb commented on a change in pull request #11943: [BEAM-10209] Add without_defaults to Mean combiner in Python

robertwb commented on a change in pull request #11943:
URL: https://github.com/apache/beam/pull/11943#discussion_r438291777



##########
File path: sdks/python/apache_beam/transforms/combiners_test.py
##########
@@ -105,6 +109,16 @@ def test_builtin_combines(self):
       assert_that(result_mean, equal_to([mean]), label='assert:mean')
       assert_that(result_count, equal_to([size]), label='assert:size')
 
+      # Now for global combines without default
+      timestamped = pcoll | Map(lambda x: TimestampedValue(timestamp))

Review comment:
       TimestampedValue requires the timestamp and the value in the constructor. 

##########
File path: sdks/python/apache_beam/transforms/combiners_test.py
##########
@@ -97,6 +100,7 @@ def test_builtin_combines(self):
       vals = [6, 3, 1, 1, 9, 1, 5, 2, 0, 6]
       mean = sum(vals) / float(len(vals))
       size = len(vals)
+      timestamp = 1591485720

Review comment:
       Just use a timestamp of 0 or similar. 

##########
File path: sdks/python/apache_beam/transforms/combiners.py
##########
@@ -66,8 +66,21 @@ class Mean(object):
   """Combiners for computing arithmetic means of elements."""
   class Globally(ptransform.PTransform):
     """combiners.Mean.Globally computes the arithmetic mean of the elements."""
+    def __init__(self, has_defaults=True, *args, **kwargs):
+      super(Mean.Globally, self).__init__()
+      self.has_defaults = has_defaults
+      self.args = args
+      self.kwargs = kwargs
+
     def expand(self, pcoll):
-      return pcoll | core.CombineGlobally(MeanCombineFn())
+      if self.has_defaults:
+        return pcoll | core.CombineGlobally(MeanCombineFn())
+      else:
+        return pcoll | core.CombineGlobally(MeanCombineFn()).without_defaults()
+
+    def without_defaults(self):
+      self.has_defaults = False

Review comment:
       Better to return a new copy than mutate self. 




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