Skip to content

Commit 230b566

Browse files
Crystal Qiantswast
authored andcommitted
Add Python dependencies example workflow. (GoogleCloudPlatform#1576)
* Add Python dependencies example workflow. * Move dependencies example to workflows folder and add tests Updates sample to use relative imports. Adds license headers. Uses 4 spaces for indentation. * Add region tag for local deps sample. * Rename local deps sample for better explaination in the docs. * Rename to coin_module to reflect correct Python term.
1 parent 318cff2 commit 230b566

File tree

7 files changed

+173
-1
lines changed

7 files changed

+173
-1
lines changed

composer/workflows/README.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Composer Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=composer/workflows/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Composer. `Google Cloud Composer`_ is a fully managed workflow orchestration service built on Apache Airflow.
11+
12+
13+
14+
15+
.. _Google Cloud Composer: https://cloud.google.com/composer/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
62+
63+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

composer/workflows/README.rst.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Composer
5+
short_name: Cloud Composer
6+
url: https://cloud.google.com/composer/docs
7+
description: >
8+
`Google Cloud Composer`_ is a fully managed workflow orchestration
9+
service built on Apache Airflow.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
folder: composer/workflows

composer/workflows/dependencies/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""A custom Python package example.
16+
17+
This package requires that your environment has the scipy PyPI package
18+
installed. """
19+
20+
import numpy as np # numpy is installed by default in Composer.
21+
from scipy import special # scipy is not.
22+
23+
24+
def flip_coin():
25+
"""Return "Heads" or "Tails" depending on a calculation."""
26+
# Returns a 2x2 randomly sampled array of values in the range [-5, 5]
27+
rand_array = 10 * np.random.random((2, 2)) - 5
28+
# Computes the average of this
29+
avg = rand_array.mean()
30+
# Returns the Gaussian CDF of this average
31+
ndtr = special.ndtr(avg)
32+
return "Heads" if ndtr > .5 else "Tails"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
https://dist.apache.org/repos/dist/dev/incubator/airflow/1.10.0beta1/apache-airflow-1.10.0b1+incubating.tar.gz#egg=apache-airflow[gcp_api]
1+
https://dist.apache.org/repos/dist/dev/incubator/airflow/1.10.0rc2/apache-airflow-1.10.0rc2+incubating-bin.tar.gz#egg=apache-airflow[gcp_api]
2+
scipy==1.1.0
3+
numpy==1.14.2
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""A DAG consisting of a BashOperator that prints the result of a coin flip."""
16+
17+
import datetime
18+
19+
import airflow
20+
from airflow.operators import bash_operator
21+
22+
# [START composer_dag_local_deps]
23+
from .dependencies import coin_module
24+
# [END composer_dag_local_deps]
25+
26+
default_args = {
27+
'start_date':
28+
datetime.datetime.combine(
29+
datetime.datetime.today() - datetime.timedelta(days=1),
30+
datetime.datetime.min.time()),
31+
}
32+
33+
with airflow.DAG('dependencies_dag', default_args=default_args) as dag:
34+
t1 = bash_operator.BashOperator(
35+
task_id='print_coin_result',
36+
bash_command='echo "{0}"'.format(coin_module.flip_coin()),
37+
dag=dag)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_dag_import():
17+
"""Test that the DAG file can be successfully imported.
18+
19+
This tests that the DAG can be parsed, but does not run it in an Airflow
20+
environment. This is a recommended sanity check by the official Airflow
21+
docs: https://airflow.incubator.apache.org/tutorial.html#testing
22+
"""
23+
from . import use_local_deps # noqa

0 commit comments

Comments
 (0)