Skip to content

Commit

Permalink
chore: run blacken on all directories with a noxfile (#16)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
parthea and gcf-owl-bot[bot] authored Apr 12, 2022
1 parent bd3825c commit ea2013d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
12 changes: 8 additions & 4 deletions optimization/snippets/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions optimization/snippets/async_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions optimization/snippets/get_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 3 additions & 1 deletion optimization/snippets/get_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 4 additions & 6 deletions optimization/snippets/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions optimization/snippets/sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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]
13 changes: 8 additions & 5 deletions optimization/snippets/sync_api_with_long_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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
Expand All @@ -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]

0 comments on commit ea2013d

Please sign in to comment.