diff --git a/packages/google-cloud-optimization/owlbot.py b/packages/google-cloud-optimization/owlbot.py index 60d092a0a3e0..468a49ff9fdb 100644 --- a/packages/google-cloud-optimization/owlbot.py +++ b/packages/google-cloud-optimization/owlbot.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from pathlib import Path + import synthtool as s import synthtool.gcp as gcp from synthtool.languages import python @@ -67,4 +69,6 @@ # Run blacken session # ---------------------------------------------------------------------------- -s.shell.run(["nox", "-s", "blacken"], hide_output=False) +# run blacken session for all directories which have a noxfile +for noxfile in Path(".").glob("**/noxfile.py"): + s.shell.run(["nox", "-s", "blacken"], cwd=noxfile.parent, hide_output=False) diff --git a/packages/google-cloud-optimization/samples/snippets/async_api.py b/packages/google-cloud-optimization/samples/snippets/async_api.py index f181f287f87e..8ab95b939c4e 100644 --- a/packages/google-cloud-optimization/samples/snippets/async_api.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/async_api_test.py b/packages/google-cloud-optimization/samples/snippets/async_api_test.py index 81a04a8d08e5..a400d2c5aede 100644 --- a/packages/google-cloud-optimization/samples/snippets/async_api_test.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/get_operation.py b/packages/google-cloud-optimization/samples/snippets/get_operation.py index ae5291623fa3..7f20b96c5153 100644 --- a/packages/google-cloud-optimization/samples/snippets/get_operation.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/get_operation_test.py b/packages/google-cloud-optimization/samples/snippets/get_operation_test.py index 378c99f0391f..e942e444bbd8 100644 --- a/packages/google-cloud-optimization/samples/snippets/get_operation_test.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/noxfile.py b/packages/google-cloud-optimization/samples/snippets/noxfile.py index 949e0fde9ae1..25f87a215d4c 100644 --- a/packages/google-cloud-optimization/samples/snippets/noxfile.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/sync_api.py b/packages/google-cloud-optimization/samples/snippets/sync_api.py index c139ea239cd6..187afd270996 100644 --- a/packages/google-cloud-optimization/samples/snippets/sync_api.py +++ b/packages/google-cloud-optimization/samples/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/packages/google-cloud-optimization/samples/snippets/sync_api_with_long_timeout.py b/packages/google-cloud-optimization/samples/snippets/sync_api_with_long_timeout.py index 8337c072b845..ba658ff7cbde 100644 --- a/packages/google-cloud-optimization/samples/snippets/sync_api_with_long_timeout.py +++ b/packages/google-cloud-optimization/samples/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]