[
https://issues.apache.org/jira/browse/BEAM-7389?focusedWorklogId=265995&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-265995
]
ASF GitHub Bot logged work on BEAM-7389:
----------------------------------------
Author: ASF GitHub Bot
Created on: 24/Jun/19 19:20
Start Date: 24/Jun/19 19:20
Worklog Time Spent: 10m
Work Description: davidcavazos commented on pull request #8905:
[BEAM-7389] Add Python snippet for Regex transform
URL: https://github.com/apache/beam/pull/8905#discussion_r296874037
##########
File path:
sdks/python/apache_beam/examples/snippets/transforms/element_wise/regex.py
##########
@@ -0,0 +1,161 @@
+# 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 regex_matches(test=None):
+ # [START regex_matches]
+ import apache_beam as beam
+ import re
+
+ def parse_plant(text):
+ m = re.match(r'^([^\s-]+)\s*-\s*(\w+)\s*-\s*(?P<duration>\w+)$', text)
+ if m:
+ yield {
+ 'match': m.group(0), # contains the entire matched text
+ 'icon': m.group(1), # ([^\s-]+) - group
+ 'name': m.group(2), # (\w+) - group
+ 'duration': m.group('duration'), # (?P<duration>\w+) - named group
+ }
+
+ with beam.Pipeline() as pipeline:
+ plant_matches = (
+ pipeline
+ | 'Garden plants' >> beam.Create([
+ '🍓 - Strawberry - perennial',
+ '🥕 - Carrot - biennial',
+ '# 🍌 - invalid - format',
+ '🍆\t-\tEggplant\t-\tperennial',
+ '🍅 - Tomato - annual',
+ '🍉 - invalid - format with trailing words',
+ '🥔-Potato-perennial',
+ ])
+ | 'Parse plants' >> beam.FlatMap(parse_plant)
+ | beam.Map(print)
Review comment:
This is a sample, when people copy-paste this and run it they should see the
results in their machines. It's using the `print` function which prints to
`stdout` and returns `None`. This is just an easier way to demonstrate the
transforms without actually going to the filesystem and explaining entire
(potentially new) concepts.
It only works on the DirectRunner, but we will eventually generate Jupyter
Notebooks from these files too. Since the notebooks use the DirectRunner, it's
actually a great experience to interactively see the results with a simple
print function.
For testing, the `print` function is replaced by `lambda elem: elem` which
makes it a NoOp and just returns the same element so we can then test the
resulting PCollection
----------------------------------------------------------------
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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 265995)
Time Spent: 14h (was: 13h 50m)
> 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: 14h
> Remaining Estimate: 0h
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)