This is an automated email from the ASF dual-hosted git repository.

pabloem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new f0ab9dd  [BEAM-7390] Add code snippet for Sum
     new 0ec28de  Merge pull request #10178 from davidcavazos/sum-code
f0ab9dd is described below

commit f0ab9dd2c94eb60c37cd70021389fdb42af599cc
Author: David Cavazos <dcava...@google.com>
AuthorDate: Tue Nov 19 17:37:05 2019 -0800

    [BEAM-7390] Add code snippet for Sum
---
 .../snippets/transforms/aggregation/sum.py         | 59 +++++++++++++++++++++
 .../snippets/transforms/aggregation/sum_test.py    | 60 ++++++++++++++++++++++
 2 files changed, 119 insertions(+)

diff --git 
a/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum.py 
b/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum.py
new file mode 100644
index 0000000..26094a5
--- /dev/null
+++ b/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum.py
@@ -0,0 +1,59 @@
+# coding=utf-8
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+
+def sum_globally(test=None):
+  # [START sum_globally]
+  import apache_beam as beam
+
+  with beam.Pipeline() as pipeline:
+    total = (
+        pipeline
+        | 'Create numbers' >> beam.Create([3, 4, 1, 2])
+        | 'Sum values' >> beam.CombineGlobally(sum)
+        | beam.Map(print)
+    )
+    # [END sum_globally]
+    if test:
+      test(total)
+
+
+def sum_per_key(test=None):
+  # [START sum_per_key]
+  import apache_beam as beam
+
+  with beam.Pipeline() as pipeline:
+    totals_per_key = (
+        pipeline
+        | 'Create produce' >> beam.Create([
+            ('🥕', 3),
+            ('🥕', 2),
+            ('🍆', 1),
+            ('🍅', 4),
+            ('🍅', 5),
+            ('🍅', 3),
+        ])
+        | 'Sum values per key' >> beam.CombinePerKey(sum)
+        | beam.Map(print)
+    )
+    # [END sum_per_key]
+    if test:
+      test(totals_per_key)
diff --git 
a/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum_test.py 
b/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum_test.py
new file mode 100644
index 0000000..dd59770
--- /dev/null
+++ 
b/sdks/python/apache_beam/examples/snippets/transforms/aggregation/sum_test.py
@@ -0,0 +1,60 @@
+# coding=utf-8
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+import unittest
+
+import mock
+
+from apache_beam.examples.snippets.util import assert_matches_stdout
+from apache_beam.testing.test_pipeline import TestPipeline
+
+from . import sum as beam_sum
+
+
+def check_total(actual):
+  expected = '''[START total]
+10
+[END total]'''.splitlines()[1:-1]
+  assert_matches_stdout(actual, expected)
+
+
+def check_totals_per_key(actual):
+  expected = '''[START totals_per_key]
+('🥕', 5)
+('🍆', 1)
+('🍅', 12)
+[END totals_per_key]'''.splitlines()[1:-1]
+  assert_matches_stdout(actual, expected)
+
+
+@mock.patch('apache_beam.Pipeline', TestPipeline)
+@mock.patch(
+    'apache_beam.examples.snippets.transforms.aggregation.sum.print', str)
+class SumTest(unittest.TestCase):
+  def test_sum_globally(self):
+    beam_sum.sum_globally(check_total)
+
+  def test_sum_per_key(self):
+    beam_sum.sum_per_key(check_totals_per_key)
+
+
+if __name__ == '__main__':
+  unittest.main()

Reply via email to