Skip to content

Commit 15adcfa

Browse files
authored
Merge branch 'main' into python-documentai-migration
2 parents 4b3f800 + 4207f09 commit 15adcfa

22 files changed

+383
-24
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ please open an issue for discussion first.
99

1010
1. Submit an issue describing your proposed change to this repository.
1111
2. A repo owner will respond to your issue promptly. If you don't see a response within
12-
a few days, please ping the owner assignd to your issue.
12+
a few days, please ping the owner assigned to your issue.
1313
3. If your proposed change is accepted, and you haven't already done so, sign a
1414
Contributor License Agreement (see details above).
1515
4. Fork this repo, develop and test your code changes. Tests are required for all

cloud-sql/mysql/sqlalchemy/connect_connector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import sqlalchemy
2222

2323

24-
# connect_with_connector initializes a connection pool for a
25-
# Cloud SQL instance of MySQL using the Cloud SQL Python Connector.
2624
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
25+
"""
26+
Initializes a connection pool for a Cloud SQL instance of MySQL.
27+
28+
Uses the Cloud SQL Python Connector package.
29+
"""
2730
# Note: Saving credentials in environment variables is convenient, but not
2831
# secure - consider a more secure solution such as
2932
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/mysql/sqlalchemy/connect_connector_auto_iam_authn.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import sqlalchemy
2222

2323

24-
# connect_with_connector_auto_iam_authn initializes a connection pool for
25-
# a Cloud SQL instance of MySQL using the Cloud SQL Python Connector
26-
# with Automatic IAM Database Authentication.
2724
def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
25+
"""
26+
Initializes a connection pool for a Cloud SQL instance of MySQL.
27+
28+
Uses the Cloud SQL Python Connector with Automatic IAM Database Authentication.
29+
"""
2830
# Note: Saving credentials in environment variables is convenient, but not
2931
# secure - consider a more secure solution such as
3032
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/mysql/sqlalchemy/connect_tcp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
import sqlalchemy
2222

2323

24-
# connect_tcp_socket initializes a TCP connection pool
25-
# for a Cloud SQL instance of MySQL.
2624
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
25+
""" Initializes a TCP connection pool for a Cloud SQL instance of MySQL. """
2726
# Note: Saving credentials in environment variables is convenient, but not
2827
# secure - consider a more secure solution such as
2928
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/mysql/sqlalchemy/connect_unix.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import sqlalchemy
1919

2020

21-
# connect_unix_socket initializes a Unix socket connection pool for
22-
# a Cloud SQL instance of MySQL.
2321
def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
22+
""" Initializes a Unix socket connection pool for a Cloud SQL instance of MySQL. """
2423
# Note: Saving credentials in environment variables is convenient, but not
2524
# secure - consider a more secure solution such as
2625
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/postgres/sqlalchemy/connect_connector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import sqlalchemy
2222

2323

24-
# connect_with_connector initializes a connection pool for a
25-
# Cloud SQL instance of Postgres using the Cloud SQL Python Connector.
2624
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
25+
"""
26+
Initializes a connection pool for a Cloud SQL instance of Postgres.
27+
28+
Uses the Cloud SQL Python Connector package.
29+
"""
2730
# Note: Saving credentials in environment variables is convenient, but not
2831
# secure - consider a more secure solution such as
2932
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/postgres/sqlalchemy/connect_connector_auto_iam_authn.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import sqlalchemy
2222

2323

24-
# connect_with_connector_auto_iam_authn initializes a connection pool for
25-
# a Cloud SQL instance of Postgres using the Cloud SQL Python Connector
26-
# with Automatic IAM Database Authentication.
2724
def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
25+
"""
26+
Initializes a connection pool for a Cloud SQL instance of Postgres.
27+
28+
Uses the Cloud SQL Python Connector with Automatic IAM Database Authentication.
29+
"""
2830
# Note: Saving credentials in environment variables is convenient, but not
2931
# secure - consider a more secure solution such as
3032
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/postgres/sqlalchemy/connect_tcp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import sqlalchemy
2323

2424

25-
# connect_tcp_socket initializes a TCP connection pool
26-
# for a Cloud SQL instance of Postgres.
2725
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
26+
""" Initializes a TCP connection pool for a Cloud SQL instance of Postgres. """
2827
# Note: Saving credentials in environment variables is convenient, but not
2928
# secure - consider a more secure solution such as
3029
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/postgres/sqlalchemy/connect_unix.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import sqlalchemy
1919

2020

21-
# connect_unix_socket initializes a Unix socket connection pool for
22-
# a Cloud SQL instance of Postgres.
2321
def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
22+
""" Initializes a Unix socket connection pool for a Cloud SQL instance of Postgres. """
2423
# Note: Saving credentials in environment variables is convenient, but not
2524
# secure - consider a more secure solution such as
2625
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/sql-server/sqlalchemy/connect_connector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import sqlalchemy
2222

2323

24-
# connect_with_connector initializes a connection pool for a
25-
# Cloud SQL instance of SQL Server using the Cloud SQL Python Connector.
2624
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
25+
"""
26+
Initializes a connection pool for a Cloud SQL instance of SQL Server.
27+
28+
Uses the Cloud SQL Python Connector package.
29+
"""
2730
# Note: Saving credentials in environment variables is convenient, but not
2831
# secure - consider a more secure solution such as
2932
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

cloud-sql/sql-server/sqlalchemy/connect_tcp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
import sqlalchemy
2222

2323

24-
# connect_tcp_socket initializes a TCP connection pool
25-
# for a Cloud SQL instance of SQL Server.
2624
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
25+
""" Initializes a TCP connection pool for a Cloud SQL instance of SQL Server. """
2726
# Note: Saving credentials in environment variables is convenient, but not
2827
# secure - consider a more secure solution such as
2928
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help

monitoring/snippets/v3/cloud-client/snippets_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import backoff
1919
from google.api_core.exceptions import InternalServerError
2020
from google.api_core.exceptions import NotFound
21+
from google.api_core.exceptions import ServiceUnavailable
2122
import pytest
2223

2324
import snippets
@@ -67,36 +68,42 @@ def eventually_consistent_test():
6768
assert "Deleted metric" in out
6869

6970

71+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
7072
def test_list_metric_descriptors(capsys):
7173
snippets.list_metric_descriptors(PROJECT_ID)
7274
out, _ = capsys.readouterr()
7375
assert "logging.googleapis.com/byte_count" in out
7476

7577

78+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
7679
def test_list_resources(capsys):
7780
snippets.list_monitored_resources(PROJECT_ID)
7881
out, _ = capsys.readouterr()
7982
assert "pubsub_topic" in out
8083

8184

85+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
8286
def test_get_resources(capsys):
8387
snippets.get_monitored_resource_descriptor(PROJECT_ID, "pubsub_topic")
8488
out, _ = capsys.readouterr()
8589
assert "A topic in Google Cloud Pub/Sub" in out
8690

8791

92+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
8893
def test_list_time_series(capsys, write_time_series):
8994
snippets.list_time_series(PROJECT_ID)
9095
out, _ = capsys.readouterr()
9196
assert "gce_instance" in out
9297

9398

99+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
94100
def test_list_time_series_header(capsys, write_time_series):
95101
snippets.list_time_series_header(PROJECT_ID)
96102
out, _ = capsys.readouterr()
97103
assert "gce_instance" in out
98104

99105

106+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
100107
def test_list_time_series_aggregate(capsys, write_time_series):
101108
snippets.list_time_series_aggregate(PROJECT_ID)
102109
out, _ = capsys.readouterr()
@@ -106,6 +113,7 @@ def test_list_time_series_aggregate(capsys, write_time_series):
106113
assert "end_time" in out
107114

108115

116+
@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
109117
def test_list_time_series_reduce(capsys, write_time_series):
110118
snippets.list_time_series_reduce(PROJECT_ID)
111119
out, _ = capsys.readouterr()

vision/snippets/face_detection/faces.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def main(input_filename, output_filename, max_results):
9797
'--out', dest='output', default='out.jpg',
9898
help='the name of the output file.')
9999
parser.add_argument(
100-
'--max-results', dest='max_results', default=4,
100+
'--max-results', dest='max_results', default=4, type=int,
101101
help='the max results of face detection.')
102102
args = parser.parse_args()
103103

webrisk/snippets/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START webrisk_compute_threatlist_diff]
16+
from google.cloud import webrisk_v1
17+
18+
19+
def compute_threatlist_diff(
20+
threat_type: webrisk_v1.ThreatType,
21+
version_token: bytes,
22+
max_diff_entries: int,
23+
max_database_entries: int,
24+
compression_type: webrisk_v1.CompressionType,
25+
) -> None:
26+
"""Gets the most recent threat list diffs. These diffs should be applied to a local database of
27+
hashes to keep it up-to-date.
28+
29+
If the local database is empty or excessively out-of-date,
30+
a complete snapshot of the database will be returned. This Method only updates a
31+
single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
32+
called once for each list.
33+
34+
Args:
35+
threat_type: The threat list to update. Only a single ThreatType should be specified per request.
36+
threat_type = webrisk_v1.ThreatType.MALWARE
37+
38+
version_token: The current version token of the client for the requested list. If the
39+
client does not have a version token (this is the first time calling ComputeThreatListDiff),
40+
this may be left empty and a full database snapshot will be returned.
41+
42+
max_diff_entries: The maximum size in number of entries. The diff will not contain more entries
43+
than this value. This should be a power of 2 between 2**10 and 2**20.
44+
If zero, no diff size limit is set.
45+
max_diff_entries = 1024
46+
47+
max_database_entries: Sets the maximum number of entries that the client is willing to have in the local database.
48+
This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
49+
max_database_entries = 1024
50+
51+
compression_type: The compression type supported by the client.
52+
compression_type = webrisk_v1.CompressionType.RAW
53+
"""
54+
55+
webrisk_client = webrisk_v1.WebRiskServiceClient()
56+
57+
constraints = webrisk_v1.ComputeThreatListDiffRequest.Constraints()
58+
constraints.max_diff_entries = max_diff_entries
59+
constraints.max_database_entries = max_database_entries
60+
constraints.supported_compressions = [compression_type]
61+
62+
request = webrisk_v1.ComputeThreatListDiffRequest()
63+
request.threat_type = threat_type
64+
request.version_token = version_token
65+
request.constraints = constraints
66+
67+
response = webrisk_client.compute_threat_list_diff(request)
68+
69+
# The returned response contains the following information:
70+
# https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
71+
# Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
72+
print(response.response_type)
73+
74+
# New version token to be used the next time when querying.
75+
print(response.new_version_token)
76+
77+
# Recommended next diff timestamp.
78+
print(response.recommended_next_diff)
79+
80+
print("Obtained threat list diff.")
81+
# [END webrisk_compute_threatlist_diff]

webrisk/snippets/noxfile_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.6"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==7.2.0

webrisk/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-webrisk==1.9.0

0 commit comments

Comments
 (0)