[ https://issues.apache.org/jira/browse/BEAM-7389?focusedWorklogId=264846&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-264846 ]
ASF GitHub Bot logged work on BEAM-7389: ---------------------------------------- Author: ASF GitHub Bot Created on: 21/Jun/19 17:45 Start Date: 21/Jun/19 17:45 Worklog Time Spent: 10m Work Description: ibzib commented on pull request #8901: [BEAM-7389] Add Python snippet for FlatMap transform URL: https://github.com/apache/beam/pull/8901#discussion_r296331921 ########## File path: sdks/python/apache_beam/examples/snippets/transforms/element_wise/flat_map.py ########## @@ -0,0 +1,220 @@ +# 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 flat_map_simple(test=None): + # [START flat_map_simple] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + '🍓Strawberry 🥕Carrot 🍆Eggplant', + '🍅Tomato 🥔Potato', + ]) + | 'Split words' >> beam.FlatMap(str.split) + | beam.Map(print) + ) + # [END flat_map_simple] + if test: + test(plants) + + +def flat_map_function(test=None): + # [START flat_map_function] + import apache_beam as beam + + def split_words(text): + return text.split(',') + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + '🍓Strawberry,🥕Carrot,🍆Eggplant', + '🍅Tomato,🥔Potato', + ]) + | 'Split words' >> beam.FlatMap(split_words) + | beam.Map(print) + ) + # [END flat_map_function] + if test: + test(plants) + + +def flat_map_lambda(test=None): + # [START flat_map_lambda] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + ['🍓Strawberry', '🥕Carrot', '🍆Eggplant'], + ['🍅Tomato', '🥔Potato'], + ]) + | 'Flatten lists' >> beam.FlatMap(lambda elements: elements) + | beam.Map(print) + ) + # [END flat_map_lambda] + if test: + test(plants) + + +def flat_map_generator(test=None): + # [START flat_map_generator] + import apache_beam as beam + + def list_elements(elements): + for element in elements: + yield element + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + ['🍓Strawberry', '🥕Carrot', '🍆Eggplant'], + ['🍅Tomato', '🥔Potato'], + ]) + | 'Flatten lists' >> beam.FlatMap(list_elements) + | beam.Map(print) + ) + # [END flat_map_generator] + if test: + test(plants) + + +def flat_map_multiple_arguments(test=None): + # [START flat_map_multiple_arguments] + import apache_beam as beam + + def split_words(text, delimiter=None): + return text.split(delimiter) + + with beam.Pipeline() as pipeline: + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + '🍓Strawberry,🥕Carrot,🍆Eggplant', + '🍅Tomato,🥔Potato', + ]) + | 'Split words' >> beam.FlatMap(split_words, delimiter=',') + | beam.Map(print) + ) + # [END flat_map_multiple_arguments] + if test: + test(plants) + + +def flat_map_side_inputs_singleton(test=None): + # [START flat_map_side_inputs_singleton] + import apache_beam as beam + + with beam.Pipeline() as pipeline: + delimiter = pipeline | 'Create delimiter' >> beam.Create([',']) + + plants = ( + pipeline + | 'Gardening plants' >> beam.Create([ + '🍓Strawberry,🥕Carrot,🍆Eggplant', + '🍅Tomato,🥔Potato', + ]) + | 'Split words' >> beam.FlatMap( + lambda text, delimiter: text.split(delimiter), + delimiter=beam.pvalue.AsSingleton(delimiter), + ) + | beam.Map(print) + ) + # [END flat_map_side_inputs_singleton] + if test: + test(plants) + + +def flat_map_side_inputs_iter(test=None): + # [START flat_map_side_inputs_iter] + import apache_beam as beam + + def validate_durations(plant, valid_durations): Review comment: I'd prefer a unique example transform for `flat_map_side_inputs_iter` that could not be replaced by a filter ---------------------------------------------------------------- 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: 264846) Time Spent: 11h 50m (was: 11h 40m) > Colab examples for element-wise transforms (Python) > --------------------------------------------------- > > Key: BEAM-7389 > URL: https://issues.apache.org/jira/browse/BEAM-7389 > Project: Beam > Issue Type: Improvement > Components: website > Reporter: Rose Nguyen > Assignee: David Cavazos > Priority: Minor > Time Spent: 11h 50m > Remaining Estimate: 0h > -- This message was sent by Atlassian JIRA (v7.6.3#76005)