From aa64705f63ab7d7b7efce23a68f3cdc8b4861b9e Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:11:39 -0700 Subject: [PATCH 01/24] docs: add sync api samples with json request (#13) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add code snippets for sync and async api * remove async test samples * use f-string * change project_id to input arg * add noxfile * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * rename noxfile and add requirements * rm noxfile * rm noxfile local * add root noxfile * Update noxfile.py * Update noxfile.py * Update noxfile.py Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> * Apply suggestions from code review * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add commit to trigger kokoro * add indentation * add type annotations Co-authored-by: Jeffrey Rennie Co-authored-by: Owl Bot Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Anthonios Partheniou --- optimization/snippets/noxfile.py | 291 ++++++++++++++++++ optimization/snippets/noxfile_config.py | 42 +++ optimization/snippets/requirements-test.txt | 2 + optimization/snippets/requirements.txt | 1 + .../snippets/resources/sync_request.json | 113 +++++++ optimization/snippets/sync_api.py | 45 +++ optimization/snippets/sync_api_test.py | 28 ++ 7 files changed, 522 insertions(+) create mode 100644 optimization/snippets/noxfile.py create mode 100644 optimization/snippets/noxfile_config.py create mode 100644 optimization/snippets/requirements-test.txt create mode 100644 optimization/snippets/requirements.txt create mode 100644 optimization/snippets/resources/sync_request.json create mode 100644 optimization/snippets/sync_api.py create mode 100644 optimization/snippets/sync_api_test.py diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py new file mode 100644 index 000000000000..949e0fde9ae1 --- /dev/null +++ b/optimization/snippets/noxfile.py @@ -0,0 +1,291 @@ +# Copyright 2019 Google LLC +# +# Licensed 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 print_function + +import glob +import os +from pathlib import Path +import sys +from typing import Callable, Dict, List, Optional + +import nox + + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +BLACK_VERSION = "black==22.3.0" + +# Copy `noxfile_config.py` to your directory and modify it instead. + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + "ignored_versions": [], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": False, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append(".") + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars() -> Dict[str, str]: + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG["gcloud_project_env"] + # This should error out if not set. + ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG["envs"]) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to test samples. +ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( + "True", + "true", +) + +# Error if a python version is missing +nox.options.error_on_missing_interpreters = True + +# +# Style Checks +# + + +def _determine_local_import_names(start_dir: str) -> List[str]: + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session: nox.sessions.Session) -> None: + if not TEST_CONFIG["enforce_type_hints"]: + session.install("flake8", "flake8-import-order") + else: + session.install("flake8", "flake8-import-order", "flake8-annotations") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Black +# + + +@nox.session +def blacken(session: nox.sessions.Session) -> None: + session.install(BLACK_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + session.run("black", *python_files) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests( + session: nox.sessions.Session, post_install: Callable = None +) -> None: + # check for presence of tests + test_list = glob.glob("*_test.py") + glob.glob("test_*.py") + test_list.extend(glob.glob("tests")) + + if len(test_list) == 0: + print("No tests found, skipping directory.") + return + + if TEST_CONFIG["pip_version_override"]: + pip_version = TEST_CONFIG["pip_version_override"] + session.install(f"pip=={pip_version}") + """Runs py.test for a particular project.""" + concurrent_args = [] + if os.path.exists("requirements.txt"): + if os.path.exists("constraints.txt"): + session.install("-r", "requirements.txt", "-c", "constraints.txt") + else: + session.install("-r", "requirements.txt") + with open("requirements.txt") as rfile: + packages = rfile.read() + + if os.path.exists("requirements-test.txt"): + if os.path.exists("constraints-test.txt"): + session.install( + "-r", "requirements-test.txt", "-c", "constraints-test.txt" + ) + else: + session.install("-r", "requirements-test.txt") + with open("requirements-test.txt") as rtfile: + packages += rtfile.read() + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + if post_install: + post_install(session) + + if "pytest-parallel" in packages: + concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto']) + elif "pytest-xdist" in packages: + concurrent_args.extend(['-n', 'auto']) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), + # Pytest will return 5 when no tests are collected. This can happen + # on travis where slow and flaky tests are excluded. + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html + success_codes=[0, 5], + env=get_pytest_env_vars(), + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session: nox.sessions.Session) -> None: + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip( + "SKIPPED: {} tests are disabled for this sample.".format(session.python) + ) + + +# +# Readmegen +# + + +def _get_repo_root() -> Optional[str]: + """ Returns the root folder of the project. """ + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + # .git is not available in repos cloned via Cloud Build + # setup.py is always in the library's root, so use that instead + # https://github.com/googleapis/synthtool/issues/792 + if Path(p / "setup.py").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session: nox.sessions.Session, path: str) -> None: + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) diff --git a/optimization/snippets/noxfile_config.py b/optimization/snippets/noxfile_config.py new file mode 100644 index 000000000000..545546d21cb6 --- /dev/null +++ b/optimization/snippets/noxfile_config.py @@ -0,0 +1,42 @@ +# Copyright 2021 Google LLC +# +# Licensed 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.6"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/optimization/snippets/requirements-test.txt b/optimization/snippets/requirements-test.txt new file mode 100644 index 000000000000..27eb6f29d8ad --- /dev/null +++ b/optimization/snippets/requirements-test.txt @@ -0,0 +1,2 @@ +pytest==7.1.1 + diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt new file mode 100644 index 000000000000..ba9c484035ad --- /dev/null +++ b/optimization/snippets/requirements.txt @@ -0,0 +1 @@ +google-cloud-optimization==0.1.0 diff --git a/optimization/snippets/resources/sync_request.json b/optimization/snippets/resources/sync_request.json new file mode 100644 index 000000000000..46458981245b --- /dev/null +++ b/optimization/snippets/resources/sync_request.json @@ -0,0 +1,113 @@ +{ + "parent": "projects/${YOUR_GCP_PROJECT_ID}", + "model": { + "shipments": [ + { + "deliveries": [ + { + "arrivalLocation": { + "latitude": 48.880942, + "longitude": 2.323866 + }, + "duration": "250s", + "timeWindows": [ + { + "endTime": "1970-01-01T01:06:40Z", + "startTime": "1970-01-01T00:50:00Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "10" + } + }, + "pickups": [ + { + "arrivalLocation": { + "latitude": 48.874507, + "longitude": 2.30361 + }, + "duration": "150s", + "timeWindows": [ + { + "endTime": "1970-01-01T00:33:20Z", + "startTime": "1970-01-01T00:16:40Z" + } + ] + } + ] + }, + { + "deliveries": [ + { + "arrivalLocation": { + "latitude": 48.88094, + "longitude": 2.323844 + }, + "duration": "251s", + "timeWindows": [ + { + "endTime": "1970-01-01T01:06:41Z", + "startTime": "1970-01-01T00:50:01Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "20" + } + }, + "pickups": [ + { + "arrivalLocation": { + "latitude": 48.880943, + "longitude": 2.323867 + }, + "duration": "151s", + "timeWindows": [ + { + "endTime": "1970-01-01T00:33:21Z", + "startTime": "1970-01-01T00:16:41Z" + } + ] + } + ] + } + ], + "vehicles": [ + { + "loadLimits": { + "weight": { + "maxLoad": 50 + } + }, + "endLocation": { + "latitude": 48.86311, + "longitude": 2.341205 + }, + "startLocation": { + "latitude": 48.863102, + "longitude": 2.341204 + } + }, + { + "loadLimits": { + "weight": { + "maxLoad": 60 + } + }, + "endLocation": { + "latitude": 48.86312, + "longitude": 2.341215 + }, + "startLocation": { + "latitude": 48.863112, + "longitude": 2.341214 + } + } + ] + } + } \ No newline at end of file diff --git a/optimization/snippets/sync_api.py b/optimization/snippets/sync_api.py new file mode 100644 index 000000000000..c139ea239cd6 --- /dev/null +++ b/optimization/snippets/sync_api.py @@ -0,0 +1,45 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +# [START cloudoptimization_sync_api] + +from google.cloud import optimization_v1 + +# TODO(developer): Uncomment these variables before running the sample. +# project_id= 'YOUR_PROJECT_ID' + + +def call_sync_api(project_id: str) -> None: + """Call the sync api for fleet routing.""" + # Use the default credentials for the environment. + # Change the file name to your request file. + request_file_name = "resources/sync_request.json" + fleet_routing_client = optimization_v1.FleetRoutingClient() + + with open(request_file_name, 'r') as f: + # The request must include the `parent` field with the value set to + # 'projects/{YOUR_GCP_PROJECT_ID}'. + fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read()) + fleet_routing_request.parent = f"projects/{project_id}" + # Send the request and print the response. + # Fleet Routing will return a response by the earliest of the `timeout` + # field in the request payload and the gRPC timeout specified below. + fleet_routing_response = fleet_routing_client.optimize_tours( + fleet_routing_request, timeout=100) + print(fleet_routing_response) + # If you want to format the response to JSON, you can do the following: + # from google.protobuf.json_format import MessageToJson + # json_obj = MessageToJson(fleet_routing_response._pb) + +# [END cloudoptimization_sync_api] diff --git a/optimization/snippets/sync_api_test.py b/optimization/snippets/sync_api_test.py new file mode 100644 index 000000000000..a5354e18c3be --- /dev/null +++ b/optimization/snippets/sync_api_test.py @@ -0,0 +1,28 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +import google.auth +import pytest + +from samples.snippets import sync_api + + +def test_call_sync_api(capsys: pytest.LogCaptureFixture) -> None: + _, project_id = google.auth.default() + sync_api.call_sync_api(project_id) + out, _ = capsys.readouterr() + + expected_strings = ["routes", "visits", "transitions", "metrics"] + for expected_string in expected_strings: + assert expected_string in out From 4601bcfbab1ce2ce4f47373c9f003254322510eb Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Fri, 8 Apr 2022 13:54:21 -0700 Subject: [PATCH 02/24] docs: add get_operation code snippets (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add get_operation code snippets * update comment * docs: add sync api samples with json request (#13) * add code snippets for sync and async api * remove async test samples * use f-string * change project_id to input arg * add noxfile * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * rename noxfile and add requirements * rm noxfile * rm noxfile local * add root noxfile * Update noxfile.py * Update noxfile.py * Update noxfile.py Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> * Apply suggestions from code review * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * Update noxfile.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add commit to trigger kokoro * add indentation * add type annotations Co-authored-by: Jeffrey Rennie Co-authored-by: Owl Bot Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Anthonios Partheniou * rebase and add type annotations * fix operation_id type * Update samples/snippets/get_operation.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * Update samples/snippets/get_operation_test.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * move TODO outside func, create operation in test * lint fix * fix asyncmodelconfig * change model_config to list * add blank line * change request to dict * change parent to project id * remove model config * Update samples/snippets/get_operation.py Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> * add TODO back Co-authored-by: Jeffrey Rennie Co-authored-by: Owl Bot Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Anthonios Partheniou Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> --- optimization/snippets/get_operation.py | 32 +++++++++++++++++ optimization/snippets/get_operation_test.py | 39 +++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 optimization/snippets/get_operation.py create mode 100644 optimization/snippets/get_operation_test.py diff --git a/optimization/snippets/get_operation.py b/optimization/snippets/get_operation.py new file mode 100644 index 000000000000..8189f69ab700 --- /dev/null +++ b/optimization/snippets/get_operation.py @@ -0,0 +1,32 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +# [START cloudoptimization_get_operation] +from google.cloud import optimization_v1 + + +def get_operation(operation_full_id: str) -> None: + """Get operation details and status.""" + # TODO(developer): Uncomment and set the following variables + # operation_full_id = \ + # "projects/[projectId]/locations/operations/[operationId]" + + client = optimization_v1.FleetRoutingClient() + # Get the latest state of a long-running operation. + response = client.transport.operations_client.get_operation(operation_full_id) + + print("Name: {}".format(response.name)) + print("Operation details:") + print(response) +# [END cloudoptimization_get_operation] diff --git a/optimization/snippets/get_operation_test.py b/optimization/snippets/get_operation_test.py new file mode 100644 index 000000000000..378c99f0391f --- /dev/null +++ b/optimization/snippets/get_operation_test.py @@ -0,0 +1,39 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + + +import google.auth +from google.cloud import optimization_v1 +import pytest + +import get_operation + + +@pytest.fixture(scope="function") +def operation_id() -> str: + fleet_routing_client = optimization_v1.FleetRoutingClient() + + _, project_id = google.auth.default() + fleet_routing_request = {"parent": f"projects/{project_id}"} + + # Make the request + operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request) + + yield operation.operation.name + + +def test_get_operation_status(capsys: pytest.LogCaptureFixture, operation_id: str) -> None: + get_operation.get_operation(operation_id) + out, _ = capsys.readouterr() + assert "Operation details" in out From 8fe6675e911c9d3a45473b3bdebda518fe5c2ad9 Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Fri, 8 Apr 2022 14:57:19 -0700 Subject: [PATCH 03/24] docs: add code snippets for async api (#18) * docs: add code snippets for async api * fix lint and add storage to requirements * iterator type annotation * fix typo and test * remove typing * add None return type Co-authored-by: Lo Ferris <50979514+loferris@users.noreply.github.com> --- optimization/snippets/async_api.py | 55 +++++++++ optimization/snippets/async_api_test.py | 48 ++++++++ optimization/snippets/requirements.txt | 1 + .../snippets/resources/async_request.json | 33 +++++ .../resources/async_request_model.json | 114 ++++++++++++++++++ 5 files changed, 251 insertions(+) create mode 100644 optimization/snippets/async_api.py create mode 100644 optimization/snippets/async_api_test.py create mode 100644 optimization/snippets/resources/async_request.json create mode 100644 optimization/snippets/resources/async_request_model.json diff --git a/optimization/snippets/async_api.py b/optimization/snippets/async_api.py new file mode 100644 index 000000000000..f181f287f87e --- /dev/null +++ b/optimization/snippets/async_api.py @@ -0,0 +1,55 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +# [START cloudoptimization_async_api] + +from google.api_core.exceptions import GoogleAPICallError +from google.cloud import optimization_v1 + +# TODO(developer): Uncomment these variables before running the sample. +# project_id= 'YOUR_PROJECT_ID' +# request_file_name = 'YOUR_REQUEST_FILE_NAME' +# request_model_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_REQUEST_MODEL_PATH' +# model_solution_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_SOLUCTION_PATH' + + +def call_async_api(project_id: str, request_model_gcs_path: str, model_solution_gcs_path_prefix: str) -> None: + """Call the async api for fleet routing.""" + # Use the default credentials for the environment to authenticate the client. + fleet_routing_client = optimization_v1.FleetRoutingClient() + request_file_name = "resources/async_request.json" + + with open(request_file_name, 'r') as f: + fleet_routing_request = optimization_v1.BatchOptimizeToursRequest.from_json(f.read()) + fleet_routing_request.parent = f"projects/{project_id}" + for idx, mc in enumerate(fleet_routing_request.model_configs): + mc.input_config.gcs_source.uri = request_model_gcs_path + model_solution_gcs_path = f'{model_solution_gcs_path_prefix}_{idx}' + mc.output_config.gcs_destination.uri = model_solution_gcs_path + + # The timeout argument for the gRPC call is independent from the `timeout` + # field in the request's OptimizeToursRequest message(s). + operation = fleet_routing_client.batch_optimize_tours(fleet_routing_request) + print(operation.operation.name) + + try: + # Block to wait for the job to finish. + result = operation.result() + print(result) + # Do you stuff. + except GoogleAPICallError: + print(operation.operation.error) + + +# [END cloudoptimization_async_api] diff --git a/optimization/snippets/async_api_test.py b/optimization/snippets/async_api_test.py new file mode 100644 index 000000000000..81a04a8d08e5 --- /dev/null +++ b/optimization/snippets/async_api_test.py @@ -0,0 +1,48 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +import uuid + +import google.auth +from google.cloud import storage +import pytest +from samples.snippets import async_api + + +# TODO(developer): Replace the variables in the file before use. +# A sample request model can be found at resources/async_request_model.json. +TEST_UUID = uuid.uuid4() +BUCKET = f'optimization-ai-{TEST_UUID}' +OUTPUT_PREFIX = f'code_snippets_test_output_{TEST_UUID}' +INPUT_URI = "gs://cloud-samples-data/optimization-ai/async_request_model.json" +BATCH_OUTPUT_URI_PREFIX = "gs://{}/{}/".format(BUCKET, OUTPUT_PREFIX) + + +@pytest.fixture(autouse=True) +def setup_teardown() -> None: + """Create a temporary bucket to store optimization output.""" + storage_client = storage.Client() + bucket = storage_client.create_bucket(BUCKET) + + yield + + bucket.delete(force=True) + + +def test_call_async_api(capsys: pytest.LogCaptureFixture) -> None: + _, project_id = google.auth.default() + async_api.call_async_api(project_id, INPUT_URI, BATCH_OUTPUT_URI_PREFIX) + out, _ = capsys.readouterr() + + assert "operations" in out diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index ba9c484035ad..f9727ac0fea2 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1 +1,2 @@ google-cloud-optimization==0.1.0 +google-cloud-storage==2.2.1 diff --git a/optimization/snippets/resources/async_request.json b/optimization/snippets/resources/async_request.json new file mode 100644 index 000000000000..10ae1b0cc254 --- /dev/null +++ b/optimization/snippets/resources/async_request.json @@ -0,0 +1,33 @@ +{ + "parent": "projects/${YOUR_GCP_PROJECT_ID}", + "model_configs":[ + { + "input_config":{ + "gcs_source":{ + "uri":"${REQUEST_MODEL_GCS_PATH}" + }, + "data_format":"JSON" + }, + "output_config":{ + "gcs_destination":{ + "uri":"${MODEL_SOLUTION_GCS_PATH}" + }, + "data_format":"JSON" + } + }, + { + "input_config":{ + "gcs_source":{ + "uri":"${REQUEST_MODEL_GCS_PATH}" + }, + "data_format":"JSON" + }, + "output_config":{ + "gcs_destination":{ + "uri":"${MODEL_SOLUTION_GCS_PATH}" + }, + "data_format":"JSON" + } + } + ] + } \ No newline at end of file diff --git a/optimization/snippets/resources/async_request_model.json b/optimization/snippets/resources/async_request_model.json new file mode 100644 index 000000000000..75c4b111be4e --- /dev/null +++ b/optimization/snippets/resources/async_request_model.json @@ -0,0 +1,114 @@ +{ + "parent":"${YOUR_GCP_PROJECT_ID}", + "allowLargeDeadlineDespiteInterruptionRisk":true, + "model":{ + "shipments":[ + { + "deliveries":[ + { + "arrivalLocation":{ + "latitude":48.880941999999997, + "longitude":2.3238660000000002 + }, + "duration":"250s", + "timeWindows":[ + { + "endTime":"1970-01-01T01:06:40Z", + "startTime":"1970-01-01T00:50:00Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "10" + } + }, + "pickups":[ + { + "arrivalLocation":{ + "latitude":48.874507000000001, + "longitude":2.3036099999999999 + }, + "duration":"150s", + "timeWindows":[ + { + "endTime":"1970-01-01T00:33:20Z", + "startTime":"1970-01-01T00:16:40Z" + } + ] + } + ] + }, + { + "deliveries":[ + { + "arrivalLocation":{ + "latitude":48.880940000000002, + "longitude":2.3238439999999998 + }, + "duration":"251s", + "timeWindows":[ + { + "endTime":"1970-01-01T01:06:41Z", + "startTime":"1970-01-01T00:50:01Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "20" + } + }, + "pickups":[ + { + "arrivalLocation":{ + "latitude":48.880943000000002, + "longitude":2.3238669999999999 + }, + "duration":"151s", + "timeWindows":[ + { + "endTime":"1970-01-01T00:33:21Z", + "startTime":"1970-01-01T00:16:41Z" + } + ] + } + ] + } + ], + "vehicles":[ + { + "loadLimits": { + "weight": { + "maxLoad": 50 + } + }, + "endLocation":{ + "latitude":48.863109999999999, + "longitude":2.341205 + }, + "startLocation":{ + "latitude":48.863101999999998, + "longitude":2.3412039999999998 + } + }, + { + "loadLimits": { + "weight": { + "maxLoad": 60 + } + }, + "endLocation":{ + "latitude":48.863120000000002, + "longitude":2.341215 + }, + "startLocation":{ + "latitude":48.863112000000001, + "longitude":2.3412139999999999 + } + } + ] + } + } \ No newline at end of file From f00be8e9510350ef002d39fe2708b1cad39cdcd0 Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Mon, 11 Apr 2022 13:52:43 -0700 Subject: [PATCH 04/24] docs: add long timeout code snippet (#20) * docs: add long timeout code snippet * fix lint * add timeout --- .../snippets/resources/sync_request.json | 215 +++++++++--------- .../snippets/sync_api_with_long_timeout.py | 48 ++++ .../sync_api_with_long_timeout_test.py | 29 +++ 3 files changed, 185 insertions(+), 107 deletions(-) create mode 100644 optimization/snippets/sync_api_with_long_timeout.py create mode 100644 optimization/snippets/sync_api_with_long_timeout_test.py diff --git a/optimization/snippets/resources/sync_request.json b/optimization/snippets/resources/sync_request.json index 46458981245b..cbdf7474ed73 100644 --- a/optimization/snippets/resources/sync_request.json +++ b/optimization/snippets/resources/sync_request.json @@ -1,113 +1,114 @@ { - "parent": "projects/${YOUR_GCP_PROJECT_ID}", - "model": { - "shipments": [ - { - "deliveries": [ - { - "arrivalLocation": { - "latitude": 48.880942, - "longitude": 2.323866 - }, - "duration": "250s", - "timeWindows": [ - { - "endTime": "1970-01-01T01:06:40Z", - "startTime": "1970-01-01T00:50:00Z" - } - ] - } - ], - "loadDemands": { - "weight": { - "amount": "10" - } - }, - "pickups": [ - { - "arrivalLocation": { - "latitude": 48.874507, - "longitude": 2.30361 - }, - "duration": "150s", - "timeWindows": [ - { - "endTime": "1970-01-01T00:33:20Z", - "startTime": "1970-01-01T00:16:40Z" - } - ] - } - ] + "parent": "projects/${YOUR_GCP_PROJECT_ID}", + "timeout": "15s", + "model": { + "shipments": [ + { + "deliveries": [ + { + "arrivalLocation": { + "latitude": 48.880942, + "longitude": 2.323866 + }, + "duration": "250s", + "timeWindows": [ + { + "endTime": "1970-01-01T01:06:40Z", + "startTime": "1970-01-01T00:50:00Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "10" + } }, - { - "deliveries": [ - { - "arrivalLocation": { - "latitude": 48.88094, - "longitude": 2.323844 - }, - "duration": "251s", - "timeWindows": [ - { - "endTime": "1970-01-01T01:06:41Z", - "startTime": "1970-01-01T00:50:01Z" - } - ] - } - ], - "loadDemands": { - "weight": { - "amount": "20" - } - }, - "pickups": [ - { - "arrivalLocation": { - "latitude": 48.880943, - "longitude": 2.323867 - }, - "duration": "151s", - "timeWindows": [ - { - "endTime": "1970-01-01T00:33:21Z", - "startTime": "1970-01-01T00:16:41Z" - } - ] - } - ] - } - ], - "vehicles": [ - { - "loadLimits": { - "weight": { - "maxLoad": 50 - } - }, - "endLocation": { - "latitude": 48.86311, - "longitude": 2.341205 - }, - "startLocation": { - "latitude": 48.863102, - "longitude": 2.341204 + "pickups": [ + { + "arrivalLocation": { + "latitude": 48.874507, + "longitude": 2.30361 + }, + "duration": "150s", + "timeWindows": [ + { + "endTime": "1970-01-01T00:33:20Z", + "startTime": "1970-01-01T00:16:40Z" + } + ] + } + ] + }, + { + "deliveries": [ + { + "arrivalLocation": { + "latitude": 48.88094, + "longitude": 2.323844 + }, + "duration": "251s", + "timeWindows": [ + { + "endTime": "1970-01-01T01:06:41Z", + "startTime": "1970-01-01T00:50:01Z" + } + ] + } + ], + "loadDemands": { + "weight": { + "amount": "20" + } + }, + "pickups": [ + { + "arrivalLocation": { + "latitude": 48.880943, + "longitude": 2.323867 + }, + "duration": "151s", + "timeWindows": [ + { + "endTime": "1970-01-01T00:33:21Z", + "startTime": "1970-01-01T00:16:41Z" + } + ] + } + ] + } + ], + "vehicles": [ + { + "loadLimits": { + "weight": { + "maxLoad": 50 } }, - { - "loadLimits": { - "weight": { - "maxLoad": 60 - } - }, - "endLocation": { - "latitude": 48.86312, - "longitude": 2.341215 - }, - "startLocation": { - "latitude": 48.863112, - "longitude": 2.341214 + "endLocation": { + "latitude": 48.86311, + "longitude": 2.341205 + }, + "startLocation": { + "latitude": 48.863102, + "longitude": 2.341204 + } + }, + { + "loadLimits": { + "weight": { + "maxLoad": 60 } + }, + "endLocation": { + "latitude": 48.86312, + "longitude": 2.341215 + }, + "startLocation": { + "latitude": 48.863112, + "longitude": 2.341214 } - ] - } - } \ No newline at end of file + } + ] + } +} \ No newline at end of file diff --git a/optimization/snippets/sync_api_with_long_timeout.py b/optimization/snippets/sync_api_with_long_timeout.py new file mode 100644 index 000000000000..8337c072b845 --- /dev/null +++ b/optimization/snippets/sync_api_with_long_timeout.py @@ -0,0 +1,48 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +# [START cloudoptimization_long_timeout] + +from google.cloud import optimization_v1 +from google.cloud.optimization_v1.services import fleet_routing +from google.cloud.optimization_v1.services.fleet_routing import transports +from google.cloud.optimization_v1.services.fleet_routing.transports import grpc as fleet_routing_grpc + +# TODO(developer): Uncomment these variables before running the sample. +# project_id= 'YOUR_PROJECT_ID' + + +def long_timeout(request_file_name: str, project_id: str) -> None: + with open(request_file_name, 'r') as f: + fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read()) + fleet_routing_request.parent = f"projects/{project_id}" + + # Create a channel to provide a connection to the Fleet Routing servers with + # custom behavior. The `grpc.keepalive_time_ms` channel argument modifies + # the channel behavior in order to send keep-alive pings every 5 minutes. + channel = fleet_routing_grpc.FleetRoutingGrpcTransport.create_channel( + options=[ + ('grpc.keepalive_time_ms', 500), + ('grpc.max_send_message_length', -1), + ('grpc.max_receive_message_length', -1), + ], + ) + # Keep-alive pings are sent on the transport. Create the transport using the + # custom channel The transport is essentially a wrapper to the channel. + transport = transports.FleetRoutingGrpcTransport(channel=channel) + client = fleet_routing.client.FleetRoutingClient(transport=transport) + fleet_routing_response = client.optimize_tours(fleet_routing_request) + print(fleet_routing_response) + +# [END cloudoptimization_long_timeout] diff --git a/optimization/snippets/sync_api_with_long_timeout_test.py b/optimization/snippets/sync_api_with_long_timeout_test.py new file mode 100644 index 000000000000..197edd34ba7e --- /dev/null +++ b/optimization/snippets/sync_api_with_long_timeout_test.py @@ -0,0 +1,29 @@ +# Copyright 2022 Google LLC +# +# Licensed 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. + +import google.auth +import pytest + +from samples.snippets import sync_api_with_long_timeout + + +def test_long_timeout(capsys: pytest.LogCaptureFixture) -> None: + request_file_name = "resources/sync_request.json" + _, project_id = google.auth.default() + sync_api_with_long_timeout.long_timeout(request_file_name, project_id) + out, _ = capsys.readouterr() + + expected_strings = ["routes", "visits", "transitions", "metrics"] + for expected_string in expected_strings: + assert expected_string in out From bd3825c74de37d0906daf34be98f1144f69db198 Mon Sep 17 00:00:00 2001 From: changsongd <101151583+changsongd@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:02:57 -0700 Subject: [PATCH 05/24] docs: update operation id (#23) --- optimization/snippets/get_operation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/get_operation.py b/optimization/snippets/get_operation.py index 8189f69ab700..ae5291623fa3 100644 --- a/optimization/snippets/get_operation.py +++ b/optimization/snippets/get_operation.py @@ -20,7 +20,7 @@ def get_operation(operation_full_id: str) -> None: """Get operation details and status.""" # TODO(developer): Uncomment and set the following variables # operation_full_id = \ - # "projects/[projectId]/locations/operations/[operationId]" + # "projects/[projectId]/operations/[operationId]" client = optimization_v1.FleetRoutingClient() # Get the latest state of a long-running operation. From ea2013d75da18ea5e6a1a3cd043aaee2c5edea1f Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Tue, 12 Apr 2022 19:27:40 -0400 Subject: [PATCH 06/24] chore: run blacken on all directories with a noxfile (#16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: run blacken on all directories with a noxfile * add missing import * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot --- optimization/snippets/async_api.py | 12 ++++++++---- optimization/snippets/async_api_test.py | 4 ++-- optimization/snippets/get_operation.py | 2 ++ optimization/snippets/get_operation_test.py | 4 +++- optimization/snippets/noxfile.py | 10 ++++------ optimization/snippets/sync_api.py | 6 ++++-- optimization/snippets/sync_api_with_long_timeout.py | 13 ++++++++----- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/optimization/snippets/async_api.py b/optimization/snippets/async_api.py index f181f287f87e..8ab95b939c4e 100644 --- a/optimization/snippets/async_api.py +++ b/optimization/snippets/async_api.py @@ -24,18 +24,22 @@ # model_solution_gcs_path = 'gs://YOUR_PROJECT/YOUR_BUCKET/YOUR_SOLUCTION_PATH' -def call_async_api(project_id: str, request_model_gcs_path: str, model_solution_gcs_path_prefix: str) -> None: +def call_async_api( + project_id: str, request_model_gcs_path: str, model_solution_gcs_path_prefix: str +) -> None: """Call the async api for fleet routing.""" # Use the default credentials for the environment to authenticate the client. fleet_routing_client = optimization_v1.FleetRoutingClient() request_file_name = "resources/async_request.json" - with open(request_file_name, 'r') as f: - fleet_routing_request = optimization_v1.BatchOptimizeToursRequest.from_json(f.read()) + with open(request_file_name, "r") as f: + fleet_routing_request = optimization_v1.BatchOptimizeToursRequest.from_json( + f.read() + ) fleet_routing_request.parent = f"projects/{project_id}" for idx, mc in enumerate(fleet_routing_request.model_configs): mc.input_config.gcs_source.uri = request_model_gcs_path - model_solution_gcs_path = f'{model_solution_gcs_path_prefix}_{idx}' + model_solution_gcs_path = f"{model_solution_gcs_path_prefix}_{idx}" mc.output_config.gcs_destination.uri = model_solution_gcs_path # The timeout argument for the gRPC call is independent from the `timeout` diff --git a/optimization/snippets/async_api_test.py b/optimization/snippets/async_api_test.py index 81a04a8d08e5..a400d2c5aede 100644 --- a/optimization/snippets/async_api_test.py +++ b/optimization/snippets/async_api_test.py @@ -23,8 +23,8 @@ # TODO(developer): Replace the variables in the file before use. # A sample request model can be found at resources/async_request_model.json. TEST_UUID = uuid.uuid4() -BUCKET = f'optimization-ai-{TEST_UUID}' -OUTPUT_PREFIX = f'code_snippets_test_output_{TEST_UUID}' +BUCKET = f"optimization-ai-{TEST_UUID}" +OUTPUT_PREFIX = f"code_snippets_test_output_{TEST_UUID}" INPUT_URI = "gs://cloud-samples-data/optimization-ai/async_request_model.json" BATCH_OUTPUT_URI_PREFIX = "gs://{}/{}/".format(BUCKET, OUTPUT_PREFIX) diff --git a/optimization/snippets/get_operation.py b/optimization/snippets/get_operation.py index ae5291623fa3..7f20b96c5153 100644 --- a/optimization/snippets/get_operation.py +++ b/optimization/snippets/get_operation.py @@ -29,4 +29,6 @@ def get_operation(operation_full_id: str) -> None: print("Name: {}".format(response.name)) print("Operation details:") print(response) + + # [END cloudoptimization_get_operation] diff --git a/optimization/snippets/get_operation_test.py b/optimization/snippets/get_operation_test.py index 378c99f0391f..e942e444bbd8 100644 --- a/optimization/snippets/get_operation_test.py +++ b/optimization/snippets/get_operation_test.py @@ -33,7 +33,9 @@ def operation_id() -> str: yield operation.operation.name -def test_get_operation_status(capsys: pytest.LogCaptureFixture, operation_id: str) -> None: +def test_get_operation_status( + capsys: pytest.LogCaptureFixture, operation_id: str +) -> None: get_operation.get_operation(operation_id) out, _ = capsys.readouterr() assert "Operation details" in out diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py index 949e0fde9ae1..25f87a215d4c 100644 --- a/optimization/snippets/noxfile.py +++ b/optimization/snippets/noxfile.py @@ -208,9 +208,7 @@ def _session_tests( if os.path.exists("requirements-test.txt"): if os.path.exists("constraints-test.txt"): - session.install( - "-r", "requirements-test.txt", "-c", "constraints-test.txt" - ) + session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") else: session.install("-r", "requirements-test.txt") with open("requirements-test.txt") as rtfile: @@ -223,9 +221,9 @@ def _session_tests( post_install(session) if "pytest-parallel" in packages: - concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto']) + concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"]) elif "pytest-xdist" in packages: - concurrent_args.extend(['-n', 'auto']) + concurrent_args.extend(["-n", "auto"]) session.run( "pytest", @@ -255,7 +253,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ + """Returns the root folder of the project.""" # Get root of this repository. Assume we don't have directories nested deeper than 10 items. p = Path(os.getcwd()) for i in range(10): diff --git a/optimization/snippets/sync_api.py b/optimization/snippets/sync_api.py index c139ea239cd6..187afd270996 100644 --- a/optimization/snippets/sync_api.py +++ b/optimization/snippets/sync_api.py @@ -27,7 +27,7 @@ def call_sync_api(project_id: str) -> None: request_file_name = "resources/sync_request.json" fleet_routing_client = optimization_v1.FleetRoutingClient() - with open(request_file_name, 'r') as f: + with open(request_file_name, "r") as f: # The request must include the `parent` field with the value set to # 'projects/{YOUR_GCP_PROJECT_ID}'. fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read()) @@ -36,10 +36,12 @@ def call_sync_api(project_id: str) -> None: # Fleet Routing will return a response by the earliest of the `timeout` # field in the request payload and the gRPC timeout specified below. fleet_routing_response = fleet_routing_client.optimize_tours( - fleet_routing_request, timeout=100) + fleet_routing_request, timeout=100 + ) print(fleet_routing_response) # If you want to format the response to JSON, you can do the following: # from google.protobuf.json_format import MessageToJson # json_obj = MessageToJson(fleet_routing_response._pb) + # [END cloudoptimization_sync_api] diff --git a/optimization/snippets/sync_api_with_long_timeout.py b/optimization/snippets/sync_api_with_long_timeout.py index 8337c072b845..ba658ff7cbde 100644 --- a/optimization/snippets/sync_api_with_long_timeout.py +++ b/optimization/snippets/sync_api_with_long_timeout.py @@ -17,14 +17,16 @@ from google.cloud import optimization_v1 from google.cloud.optimization_v1.services import fleet_routing from google.cloud.optimization_v1.services.fleet_routing import transports -from google.cloud.optimization_v1.services.fleet_routing.transports import grpc as fleet_routing_grpc +from google.cloud.optimization_v1.services.fleet_routing.transports import ( + grpc as fleet_routing_grpc, +) # TODO(developer): Uncomment these variables before running the sample. # project_id= 'YOUR_PROJECT_ID' def long_timeout(request_file_name: str, project_id: str) -> None: - with open(request_file_name, 'r') as f: + with open(request_file_name, "r") as f: fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read()) fleet_routing_request.parent = f"projects/{project_id}" @@ -33,9 +35,9 @@ def long_timeout(request_file_name: str, project_id: str) -> None: # the channel behavior in order to send keep-alive pings every 5 minutes. channel = fleet_routing_grpc.FleetRoutingGrpcTransport.create_channel( options=[ - ('grpc.keepalive_time_ms', 500), - ('grpc.max_send_message_length', -1), - ('grpc.max_receive_message_length', -1), + ("grpc.keepalive_time_ms", 500), + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), ], ) # Keep-alive pings are sent on the transport. Create the transport using the @@ -45,4 +47,5 @@ def long_timeout(request_file_name: str, project_id: str) -> None: fleet_routing_response = client.optimize_tours(fleet_routing_request) print(fleet_routing_response) + # [END cloudoptimization_long_timeout] From 5ea889d277382fc05a38dee8cf5dc0e3199afe6e Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Thu, 14 Apr 2022 01:56:24 +0200 Subject: [PATCH 07/24] chore(deps): update dependency google-cloud-storage to v2.3.0 (#24) --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index f9727ac0fea2..14af1a0bd92c 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ google-cloud-optimization==0.1.0 -google-cloud-storage==2.2.1 +google-cloud-storage==2.3.0 From 64a89343af1663ced1aa9be083f291a58ded7471 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Wed, 20 Apr 2022 21:15:32 -0400 Subject: [PATCH 08/24] chore(python): add nox session to sort python imports (#26) Source-Link: https://github.com/googleapis/synthtool/commit/1b71c10e20de7ed3f97f692f99a0e3399b67049f Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 Co-authored-by: Owl Bot --- optimization/snippets/noxfile.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py index 25f87a215d4c..a40410b56369 100644 --- a/optimization/snippets/noxfile.py +++ b/optimization/snippets/noxfile.py @@ -30,6 +30,7 @@ # WARNING - WARNING - WARNING - WARNING - WARNING BLACK_VERSION = "black==22.3.0" +ISORT_VERSION = "isort==5.10.1" # Copy `noxfile_config.py` to your directory and modify it instead. @@ -168,12 +169,33 @@ def lint(session: nox.sessions.Session) -> None: @nox.session def blacken(session: nox.sessions.Session) -> None: + """Run black. Format code to uniform standard.""" session.install(BLACK_VERSION) python_files = [path for path in os.listdir(".") if path.endswith(".py")] session.run("black", *python_files) +# +# format = isort + black +# + + +@nox.session +def format(session: nox.sessions.Session) -> None: + """ + Run isort to sort imports. Then run black + to format code to uniform standard. + """ + session.install(BLACK_VERSION, ISORT_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + # Use the --fss option to sort imports using strict alphabetical order. + # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections + session.run("isort", "--fss", *python_files) + session.run("black", *python_files) + + # # Sample Tests # From 576dc176d103a7f85443691e39feed1b0655d157 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Mon, 25 Apr 2022 17:03:39 +0200 Subject: [PATCH 09/24] chore(deps): update dependency pytest to v7.1.2 (#29) --- optimization/snippets/requirements-test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements-test.txt b/optimization/snippets/requirements-test.txt index 27eb6f29d8ad..9b2511d86d98 100644 --- a/optimization/snippets/requirements-test.txt +++ b/optimization/snippets/requirements-test.txt @@ -1,2 +1,2 @@ -pytest==7.1.1 +pytest==7.1.2 From 6fd5238eae90b703aeb29f10f72652bbfeb86a3e Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Tue, 26 Apr 2022 16:55:31 +0200 Subject: [PATCH 10/24] chore(deps): update dependency google-cloud-optimization to v0.1.1 (#32) --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 14af1a0bd92c..764b6dd65773 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==0.1.0 +google-cloud-optimization==0.1.1 google-cloud-storage==2.3.0 From 6dfe3400d9992991d0c9c5fcb590c358cfa89420 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Wed, 27 Apr 2022 15:36:34 +0200 Subject: [PATCH 11/24] chore(deps): update dependency google-cloud-optimization to v1 (#35) --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 764b6dd65773..20d7a0527a43 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==0.1.1 +google-cloud-optimization==1.0.0 google-cloud-storage==2.3.0 From a484038a5d64376cda6950f809dee68e58072415 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Sun, 10 Jul 2022 05:59:58 -0400 Subject: [PATCH 12/24] fix: require python 3.7+ (#50) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(python): drop python 3.6 Source-Link: https://github.com/googleapis/synthtool/commit/4f89b13af10d086458f9b379e56a614f9d6dab7b Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:e7bb19d47c13839fe8c147e50e02e8b6cf5da8edd1af8b82208cd6f66cc2829c * add api_description to .repo-metadata.json * require python 3.7+ in setup.py * remove python 3.6 sample configs * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix imports for prerelease session Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- optimization/snippets/async_api_test.py | 3 ++- optimization/snippets/noxfile.py | 2 +- optimization/snippets/sync_api_test.py | 3 ++- optimization/snippets/sync_api_with_long_timeout_test.py | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/optimization/snippets/async_api_test.py b/optimization/snippets/async_api_test.py index a400d2c5aede..fce1b585f7cb 100644 --- a/optimization/snippets/async_api_test.py +++ b/optimization/snippets/async_api_test.py @@ -17,7 +17,8 @@ import google.auth from google.cloud import storage import pytest -from samples.snippets import async_api + +import async_api # TODO(developer): Replace the variables in the file before use. diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py index a40410b56369..29b5bc852183 100644 --- a/optimization/snippets/noxfile.py +++ b/optimization/snippets/noxfile.py @@ -89,7 +89,7 @@ def get_pytest_env_vars() -> Dict[str, str]: # DO NOT EDIT - automatically generated. # All versions used to test samples. -ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] +ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10"] # Any default versions that should be ignored. IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] diff --git a/optimization/snippets/sync_api_test.py b/optimization/snippets/sync_api_test.py index a5354e18c3be..6bfffc0c7c86 100644 --- a/optimization/snippets/sync_api_test.py +++ b/optimization/snippets/sync_api_test.py @@ -15,7 +15,8 @@ import google.auth import pytest -from samples.snippets import sync_api + +import sync_api def test_call_sync_api(capsys: pytest.LogCaptureFixture) -> None: diff --git a/optimization/snippets/sync_api_with_long_timeout_test.py b/optimization/snippets/sync_api_with_long_timeout_test.py index 197edd34ba7e..4955fa47cb4e 100644 --- a/optimization/snippets/sync_api_with_long_timeout_test.py +++ b/optimization/snippets/sync_api_with_long_timeout_test.py @@ -15,7 +15,7 @@ import google.auth import pytest -from samples.snippets import sync_api_with_long_timeout +import sync_api_with_long_timeout def test_long_timeout(capsys: pytest.LogCaptureFixture) -> None: From 226f37eec24c5e0f4d69f6318c18a6679fe3604f Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Sun, 24 Jul 2022 02:30:42 +0200 Subject: [PATCH 13/24] chore(deps): update all dependencies (#45) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 20d7a0527a43..fff7f93d5d42 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ google-cloud-optimization==1.0.0 -google-cloud-storage==2.3.0 +google-cloud-storage==2.4.0 From a74b9eaecaf73829c6e9c7d41d311dd9a1e5d7b1 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Tue, 2 Aug 2022 16:00:36 +0200 Subject: [PATCH 14/24] chore(deps): update all dependencies (#55) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index fff7f93d5d42..30ef0c6607e8 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==1.0.0 +google-cloud-optimization==1.0.1 google-cloud-storage==2.4.0 From 3ec7ffddb8a3c4628ff3db5a59596299054f909d Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Fri, 5 Aug 2022 21:33:45 +0200 Subject: [PATCH 15/24] chore(deps): update all dependencies (#57) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update setup.py Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 30ef0c6607e8..aa7a6c899c72 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ google-cloud-optimization==1.0.1 -google-cloud-storage==2.4.0 +google-cloud-storage==2.5.0 From e902becda27cabbba7cc18ed4ffd4c887302b6a4 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Tue, 9 Aug 2022 17:28:36 +0200 Subject: [PATCH 16/24] chore(deps): update all dependencies (#58) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index aa7a6c899c72..10076977d89b 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==1.0.1 +google-cloud-optimization==1.1.0 google-cloud-storage==2.5.0 From f0e0b038e301ea95d3c808740c1ada9e11b0955d Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Fri, 2 Sep 2022 13:17:33 +0200 Subject: [PATCH 17/24] chore(deps): update dependency google-cloud-optimization to v1.1.1 (#70) Co-authored-by: Anthonios Partheniou --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 10076977d89b..31ddf120db90 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==1.1.0 +google-cloud-optimization==1.1.1 google-cloud-storage==2.5.0 From 6c81459fe16857f86a56b0f678b8493591e4a1b2 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Tue, 6 Sep 2022 17:20:06 +0200 Subject: [PATCH 18/24] chore(deps): update dependency pytest to v7.1.3 (#75) --- optimization/snippets/requirements-test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements-test.txt b/optimization/snippets/requirements-test.txt index 9b2511d86d98..4a4ea643576f 100644 --- a/optimization/snippets/requirements-test.txt +++ b/optimization/snippets/requirements-test.txt @@ -1,2 +1,2 @@ -pytest==7.1.2 +pytest==7.1.3 From e9ed704395c97b6233073d5e963ca311627c0514 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 12:17:12 -0400 Subject: [PATCH 19/24] chore: detect samples tests in nested directories (#79) Source-Link: https://github.com/googleapis/synthtool/commit/50db768f450a50d7c1fd62513c113c9bb96fd434 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:e09366bdf0fd9c8976592988390b24d53583dd9f002d476934da43725adbb978 Co-authored-by: Owl Bot --- optimization/snippets/noxfile.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py index 29b5bc852183..b053ca568f63 100644 --- a/optimization/snippets/noxfile.py +++ b/optimization/snippets/noxfile.py @@ -208,8 +208,10 @@ def _session_tests( session: nox.sessions.Session, post_install: Callable = None ) -> None: # check for presence of tests - test_list = glob.glob("*_test.py") + glob.glob("test_*.py") - test_list.extend(glob.glob("tests")) + test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob( + "**/test_*.py", recursive=True + ) + test_list.extend(glob.glob("**/tests", recursive=True)) if len(test_list) == 0: print("No tests found, skipping directory.") From a42b9f13dead72014cf6b9b45ff2981f120b6745 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Mon, 3 Oct 2022 19:08:09 +0200 Subject: [PATCH 20/24] chore(deps): update dependency google-cloud-optimization to v1.1.2 (#83) --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 31ddf120db90..1546c66a135b 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==1.1.1 +google-cloud-optimization==1.1.2 google-cloud-storage==2.5.0 From 06db1ee97d929f7c5e52181d3c4ec69d85b7c15b Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Mon, 10 Oct 2022 18:21:18 +0200 Subject: [PATCH 21/24] chore(deps): update dependency google-cloud-optimization to v1.1.3 (#86) --- optimization/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements.txt b/optimization/snippets/requirements.txt index 1546c66a135b..ae1d796f972e 100644 --- a/optimization/snippets/requirements.txt +++ b/optimization/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-optimization==1.1.2 +google-cloud-optimization==1.1.3 google-cloud-storage==2.5.0 From 60b0b2663ad3929bab2b944d033369ddd3ccfc07 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Wed, 26 Oct 2022 13:00:24 +0200 Subject: [PATCH 22/24] chore(deps): update dependency pytest to v7.2.0 (#87) --- optimization/snippets/requirements-test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimization/snippets/requirements-test.txt b/optimization/snippets/requirements-test.txt index 4a4ea643576f..19a897665b9c 100644 --- a/optimization/snippets/requirements-test.txt +++ b/optimization/snippets/requirements-test.txt @@ -1,2 +1,2 @@ -pytest==7.1.3 +pytest==7.2.0 From 696f38118be2f054228432a7cf99048fecdf7658 Mon Sep 17 00:00:00 2001 From: Don McCasland Date: Tue, 8 Nov 2022 15:54:49 -0800 Subject: [PATCH 23/24] removing noxfile.py --- optimization/snippets/noxfile.py | 313 ------------------------------- 1 file changed, 313 deletions(-) delete mode 100644 optimization/snippets/noxfile.py diff --git a/optimization/snippets/noxfile.py b/optimization/snippets/noxfile.py deleted file mode 100644 index b053ca568f63..000000000000 --- a/optimization/snippets/noxfile.py +++ /dev/null @@ -1,313 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed 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 print_function - -import glob -import os -from pathlib import Path -import sys -from typing import Callable, Dict, List, Optional - -import nox - - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -BLACK_VERSION = "black==22.3.0" -ISORT_VERSION = "isort==5.10.1" - -# Copy `noxfile_config.py` to your directory and modify it instead. - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - "ignored_versions": [], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - "enforce_type_hints": False, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # If you need to use a specific version of pip, - # change pip_version_override to the string representation - # of the version number, for example, "20.2.4" - "pip_version_override": None, - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append(".") - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars() -> Dict[str, str]: - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG["gcloud_project_env"] - # This should error out if not set. - ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG["envs"]) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to test samples. -ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( - "True", - "true", -) - -# Error if a python version is missing -nox.options.error_on_missing_interpreters = True - -# -# Style Checks -# - - -def _determine_local_import_names(start_dir: str) -> List[str]: - """Determines all import names that should be considered "local". - - This is used when running the linter to insure that import order is - properly checked. - """ - file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] - return [ - basename - for basename, extension in file_ext_pairs - if extension == ".py" - or os.path.isdir(os.path.join(start_dir, basename)) - and basename not in ("__pycache__") - ] - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--import-order-style=google", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session: nox.sessions.Session) -> None: - if not TEST_CONFIG["enforce_type_hints"]: - session.install("flake8", "flake8-import-order") - else: - session.install("flake8", "flake8-import-order", "flake8-annotations") - - local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ - "--application-import-names", - ",".join(local_names), - ".", - ] - session.run("flake8", *args) - - -# -# Black -# - - -@nox.session -def blacken(session: nox.sessions.Session) -> None: - """Run black. Format code to uniform standard.""" - session.install(BLACK_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - session.run("black", *python_files) - - -# -# format = isort + black -# - - -@nox.session -def format(session: nox.sessions.Session) -> None: - """ - Run isort to sort imports. Then run black - to format code to uniform standard. - """ - session.install(BLACK_VERSION, ISORT_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - # Use the --fss option to sort imports using strict alphabetical order. - # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections - session.run("isort", "--fss", *python_files) - session.run("black", *python_files) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests( - session: nox.sessions.Session, post_install: Callable = None -) -> None: - # check for presence of tests - test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob( - "**/test_*.py", recursive=True - ) - test_list.extend(glob.glob("**/tests", recursive=True)) - - if len(test_list) == 0: - print("No tests found, skipping directory.") - return - - if TEST_CONFIG["pip_version_override"]: - pip_version = TEST_CONFIG["pip_version_override"] - session.install(f"pip=={pip_version}") - """Runs py.test for a particular project.""" - concurrent_args = [] - if os.path.exists("requirements.txt"): - if os.path.exists("constraints.txt"): - session.install("-r", "requirements.txt", "-c", "constraints.txt") - else: - session.install("-r", "requirements.txt") - with open("requirements.txt") as rfile: - packages = rfile.read() - - if os.path.exists("requirements-test.txt"): - if os.path.exists("constraints-test.txt"): - session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") - else: - session.install("-r", "requirements-test.txt") - with open("requirements-test.txt") as rtfile: - packages += rtfile.read() - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - if "pytest-parallel" in packages: - concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"]) - elif "pytest-xdist" in packages: - concurrent_args.extend(["-n", "auto"]) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars(), - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session: nox.sessions.Session) -> None: - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip( - "SKIPPED: {} tests are disabled for this sample.".format(session.python) - ) - - -# -# Readmegen -# - - -def _get_repo_root() -> Optional[str]: - """Returns the root folder of the project.""" - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - # .git is not available in repos cloned via Cloud Build - # setup.py is always in the library's root, so use that instead - # https://github.com/googleapis/synthtool/issues/792 - if Path(p / "setup.py").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session: nox.sessions.Session, path: str) -> None: - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) From fccca2dffbbde739c40d417a85d7816e91f691cf Mon Sep 17 00:00:00 2001 From: Don McCasland Date: Tue, 8 Nov 2022 17:16:19 -0800 Subject: [PATCH 24/24] Updating CODEOWNERS and blunderbuss.yml --- .github/CODEOWNERS | 1 + .github/blunderbuss.yml | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 6d84c22da6ae..4361eab55315 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -53,6 +53,7 @@ /monitoring/opencensus @yuriatgoogle @GoogleCloudPlatform/python-samples-reviewers /monitoring/prometheus @yuriatgoogle @GoogleCloudPlatform/python-samples-reviewers /notebooks/**/* @alixhami @GoogleCloudPlatform/python-samples-reviewers +/optimization/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers /opencensus/**/* @GoogleCloudPlatform/python-samples-reviewers /people-and-planet-ai/**/* @davidcavazos @GoogleCloudPlatform/python-samples-reviewers /profiler/**/* @GoogleCloudPlatform/python-samples-reviewers diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index bc1059f6faa8..5d80213d9a37 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -82,6 +82,10 @@ assign_issues_by: - 'api: notebooks' to: - alixhami +- labels: + - 'api: optimization' + to: + - GoogleCloudPlatform/dee-data-ai - labels: - 'api: people-and-planet-ai' to: