This is an automated email from the ASF dual-hosted git repository. altay 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 da79821 Add Python snippet for Keys, Values, KvSwap, and ToString transform new 778bf33 Merge pull request #8906 from davidcavazos/element-wise-simple da79821 is described below commit da79821368ffd7dd4ab0261a274731cbb8e9a44e Author: David Cavazos <dcava...@google.com> AuthorDate: Mon Jun 10 16:17:20 2019 -0700 Add Python snippet for Keys, Values, KvSwap, and ToString transform --- .../snippets/transforms/element_wise/keys.py | 42 +++++++++++++++++ .../snippets/transforms/element_wise/keys_test.py | 55 ++++++++++++++++++++++ .../snippets/transforms/element_wise/kvswap.py | 42 +++++++++++++++++ .../transforms/element_wise/kvswap_test.py | 55 ++++++++++++++++++++++ .../snippets/transforms/element_wise/to_string.py | 42 +++++++++++++++++ .../transforms/element_wise/to_string_test.py | 55 ++++++++++++++++++++++ .../snippets/transforms/element_wise/values.py | 42 +++++++++++++++++ .../transforms/element_wise/values_test.py | 55 ++++++++++++++++++++++ 8 files changed, 388 insertions(+) diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys.py new file mode 100644 index 0000000..01c9d6b --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys.py @@ -0,0 +1,42 @@ +# 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 keys(test=None): + # [START keys] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + icons = ( + pipeline + | 'Garden plants' >> beam.Create([ + ('🍓', 'Strawberry'), + ('🥕', 'Carrot'), + ('🍆', 'Eggplant'), + ('🍅', 'Tomato'), + ('🥔', 'Potato'), + ]) + | 'Keys' >> beam.Keys() + | beam.Map(print) + ) + # [END keys] + if test: + test(icons) diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys_test.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys_test.py new file mode 100644 index 0000000..9cb4909 --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/keys_test.py @@ -0,0 +1,55 @@ +# 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.transforms.element_wise.keys import * +from apache_beam.testing.test_pipeline import TestPipeline +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to + + +@mock.patch('apache_beam.Pipeline', TestPipeline) +# pylint: disable=line-too-long +@mock.patch('apache_beam.examples.snippets.transforms.element_wise.keys.print', lambda elem: elem) +# pylint: enable=line-too-long +class KeysTest(unittest.TestCase): + def __init__(self, methodName): + super(KeysTest, self).__init__(methodName) + # [START icons] + icons = [ + '🍓', + '🥕', + '🍆', + '🍅', + '🥔', + ] + # [END icons] + self.icons_test = lambda actual: assert_that(actual, equal_to(icons)) + + def test_keys(self): + keys(self.icons_test) + + +if __name__ == '__main__': + unittest.main() diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap.py new file mode 100644 index 0000000..2107fd5 --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap.py @@ -0,0 +1,42 @@ +# 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 kvswap(test=None): + # [START kvswap] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Garden plants' >> beam.Create([ + ('🍓', 'Strawberry'), + ('🥕', 'Carrot'), + ('🍆', 'Eggplant'), + ('🍅', 'Tomato'), + ('🥔', 'Potato'), + ]) + | 'Key-Value swap' >> beam.KvSwap() + | beam.Map(print) + ) + # [END kvswap] + if test: + test(plants) diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap_test.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap_test.py new file mode 100644 index 0000000..85fa9dc --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/kvswap_test.py @@ -0,0 +1,55 @@ +# 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.transforms.element_wise.kvswap import * +from apache_beam.testing.test_pipeline import TestPipeline +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to + + +@mock.patch('apache_beam.Pipeline', TestPipeline) +# pylint: disable=line-too-long +@mock.patch('apache_beam.examples.snippets.transforms.element_wise.kvswap.print', lambda elem: elem) +# pylint: enable=line-too-long +class KvSwapTest(unittest.TestCase): + def __init__(self, methodName): + super(KvSwapTest, self).__init__(methodName) + # [START plants] + plants = [ + ('Strawberry', '🍓'), + ('Carrot', '🥕'), + ('Eggplant', '🍆'), + ('Tomato', '🍅'), + ('Potato', '🥔'), + ] + # [END plants] + self.plants_test = lambda actual: assert_that(actual, equal_to(plants)) + + def test_kvswap(self): + kvswap(self.plants_test) + + +if __name__ == '__main__': + unittest.main() diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string.py new file mode 100644 index 0000000..7a64bfa --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string.py @@ -0,0 +1,42 @@ +# 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 to_string(test=None): + # [START to_string] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Garden plants' >> beam.Create([ + ('Strawberry', 'perennial'), + ('Carrot', 'biennial'), + ('Eggplant', 'perennial'), + ('Tomato', 'annual'), + ('Potato', 'perennial'), + ]) + | 'To string' >> beam.Map(lambda plant: str(plant)) + | beam.Map(print) + ) + # [END to_string] + if test: + test(plants) diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string_test.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string_test.py new file mode 100644 index 0000000..5fcf83c --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/to_string_test.py @@ -0,0 +1,55 @@ +# 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.transforms.element_wise.to_string import * +from apache_beam.testing.test_pipeline import TestPipeline +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to + + +@mock.patch('apache_beam.Pipeline', TestPipeline) +# pylint: disable=line-too-long +@mock.patch('apache_beam.examples.snippets.transforms.element_wise.to_string.print', lambda elem: elem) +# pylint: enable=line-too-long +class ToStringTest(unittest.TestCase): + def __init__(self, methodName): + super(ToStringTest, self).__init__(methodName) + # [START plants] + plants = [ + "('Strawberry', 'perennial')", + "('Carrot', 'biennial')", + "('Eggplant', 'perennial')", + "('Tomato', 'annual')", + "('Potato', 'perennial')", + ] + # [END plants] + self.plants_test = lambda actual: assert_that(actual, equal_to(plants)) + + def test_to_string(self): + to_string(self.plants_test) + + +if __name__ == '__main__': + unittest.main() diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values.py new file mode 100644 index 0000000..8504ff4 --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values.py @@ -0,0 +1,42 @@ +# 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 values(test=None): + # [START values] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Garden plants' >> beam.Create([ + ('🍓', 'Strawberry'), + ('🥕', 'Carrot'), + ('🍆', 'Eggplant'), + ('🍅', 'Tomato'), + ('🥔', 'Potato'), + ]) + | 'Values' >> beam.Values() + | beam.Map(print) + ) + # [END values] + if test: + test(plants) diff --git a/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values_test.py b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values_test.py new file mode 100644 index 0000000..b43d911 --- /dev/null +++ b/sdks/python/apache_beam/examples/snippets/transforms/element_wise/values_test.py @@ -0,0 +1,55 @@ +# 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.transforms.element_wise.values import * +from apache_beam.testing.test_pipeline import TestPipeline +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to + + +@mock.patch('apache_beam.Pipeline', TestPipeline) +# pylint: disable=line-too-long +@mock.patch('apache_beam.examples.snippets.transforms.element_wise.values.print', lambda elem: elem) +# pylint: enable=line-too-long +class ValuesTest(unittest.TestCase): + def __init__(self, methodName): + super(ValuesTest, self).__init__(methodName) + # [START plants] + plants = [ + 'Strawberry', + 'Carrot', + 'Eggplant', + 'Tomato', + 'Potato', + ] + # [END plants] + self.plants_test = lambda actual: assert_that(actual, equal_to(plants)) + + def test_values(self): + values(self.plants_test) + + +if __name__ == '__main__': + unittest.main()