Skip to content

Commit

Permalink
Merge branch 'main' into python-documentai-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dandhlee authored Jan 3, 2023
2 parents 4b3f800 + 4207f09 commit 15adcfa
Show file tree
Hide file tree
Showing 22 changed files with 383 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ please open an issue for discussion first.

1. Submit an issue describing your proposed change to this repository.
2. A repo owner will respond to your issue promptly. If you don't see a response within
a few days, please ping the owner assignd to your issue.
a few days, please ping the owner assigned to your issue.
3. If your proposed change is accepted, and you haven't already done so, sign a
Contributor License Agreement (see details above).
4. Fork this repo, develop and test your code changes. Tests are required for all
Expand Down
7 changes: 5 additions & 2 deletions cloud-sql/mysql/sqlalchemy/connect_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import sqlalchemy


# connect_with_connector initializes a connection pool for a
# Cloud SQL instance of MySQL using the Cloud SQL Python Connector.
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of MySQL.
Uses the Cloud SQL Python Connector package.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import sqlalchemy


# connect_with_connector_auto_iam_authn initializes a connection pool for
# a Cloud SQL instance of MySQL using the Cloud SQL Python Connector
# with Automatic IAM Database Authentication.
def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of MySQL.
Uses the Cloud SQL Python Connector with Automatic IAM Database Authentication.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
3 changes: 1 addition & 2 deletions cloud-sql/mysql/sqlalchemy/connect_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import sqlalchemy


# connect_tcp_socket initializes a TCP connection pool
# for a Cloud SQL instance of MySQL.
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a TCP connection pool for a Cloud SQL instance of MySQL. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
3 changes: 1 addition & 2 deletions cloud-sql/mysql/sqlalchemy/connect_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import sqlalchemy


# connect_unix_socket initializes a Unix socket connection pool for
# a Cloud SQL instance of MySQL.
def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a Unix socket connection pool for a Cloud SQL instance of MySQL. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
7 changes: 5 additions & 2 deletions cloud-sql/postgres/sqlalchemy/connect_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import sqlalchemy


# connect_with_connector initializes a connection pool for a
# Cloud SQL instance of Postgres using the Cloud SQL Python Connector.
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of Postgres.
Uses the Cloud SQL Python Connector package.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import sqlalchemy


# connect_with_connector_auto_iam_authn initializes a connection pool for
# a Cloud SQL instance of Postgres using the Cloud SQL Python Connector
# with Automatic IAM Database Authentication.
def connect_with_connector_auto_iam_authn() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of Postgres.
Uses the Cloud SQL Python Connector with Automatic IAM Database Authentication.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
3 changes: 1 addition & 2 deletions cloud-sql/postgres/sqlalchemy/connect_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import sqlalchemy


# connect_tcp_socket initializes a TCP connection pool
# for a Cloud SQL instance of Postgres.
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a TCP connection pool for a Cloud SQL instance of Postgres. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
3 changes: 1 addition & 2 deletions cloud-sql/postgres/sqlalchemy/connect_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import sqlalchemy


# connect_unix_socket initializes a Unix socket connection pool for
# a Cloud SQL instance of Postgres.
def connect_unix_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a Unix socket connection pool for a Cloud SQL instance of Postgres. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
7 changes: 5 additions & 2 deletions cloud-sql/sql-server/sqlalchemy/connect_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import sqlalchemy


# connect_with_connector initializes a connection pool for a
# Cloud SQL instance of SQL Server using the Cloud SQL Python Connector.
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of SQL Server.
Uses the Cloud SQL Python Connector package.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
3 changes: 1 addition & 2 deletions cloud-sql/sql-server/sqlalchemy/connect_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import sqlalchemy


# connect_tcp_socket initializes a TCP connection pool
# for a Cloud SQL instance of SQL Server.
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
""" Initializes a TCP connection pool for a Cloud SQL instance of SQL Server. """
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
Expand Down
8 changes: 8 additions & 0 deletions monitoring/snippets/v3/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import backoff
from google.api_core.exceptions import InternalServerError
from google.api_core.exceptions import NotFound
from google.api_core.exceptions import ServiceUnavailable
import pytest

import snippets
Expand Down Expand Up @@ -67,36 +68,42 @@ def eventually_consistent_test():
assert "Deleted metric" in out


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


@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
def test_list_resources(capsys):
snippets.list_monitored_resources(PROJECT_ID)
out, _ = capsys.readouterr()
assert "pubsub_topic" in out


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


@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
def test_list_time_series(capsys, write_time_series):
snippets.list_time_series(PROJECT_ID)
out, _ = capsys.readouterr()
assert "gce_instance" in out


@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
def test_list_time_series_header(capsys, write_time_series):
snippets.list_time_series_header(PROJECT_ID)
out, _ = capsys.readouterr()
assert "gce_instance" in out


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


@backoff.on_exception(backoff.expo, (ServiceUnavailable), max_tries=3)
def test_list_time_series_reduce(capsys, write_time_series):
snippets.list_time_series_reduce(PROJECT_ID)
out, _ = capsys.readouterr()
Expand Down
2 changes: 1 addition & 1 deletion vision/snippets/face_detection/faces.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def main(input_filename, output_filename, max_results):
'--out', dest='output', default='out.jpg',
help='the name of the output file.')
parser.add_argument(
'--max-results', dest='max_results', default=4,
'--max-results', dest='max_results', default=4, type=int,
help='the max results of face detection.')
args = parser.parse_args()

Expand Down
13 changes: 13 additions & 0 deletions webrisk/snippets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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.
81 changes: 81 additions & 0 deletions webrisk/snippets/compute_threatlist_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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 webrisk_compute_threatlist_diff]
from google.cloud import webrisk_v1


def compute_threatlist_diff(
threat_type: webrisk_v1.ThreatType,
version_token: bytes,
max_diff_entries: int,
max_database_entries: int,
compression_type: webrisk_v1.CompressionType,
) -> None:
"""Gets the most recent threat list diffs. These diffs should be applied to a local database of
hashes to keep it up-to-date.
If the local database is empty or excessively out-of-date,
a complete snapshot of the database will be returned. This Method only updates a
single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
called once for each list.
Args:
threat_type: The threat list to update. Only a single ThreatType should be specified per request.
threat_type = webrisk_v1.ThreatType.MALWARE
version_token: The current version token of the client for the requested list. If the
client does not have a version token (this is the first time calling ComputeThreatListDiff),
this may be left empty and a full database snapshot will be returned.
max_diff_entries: The maximum size in number of entries. The diff will not contain more entries
than this value. This should be a power of 2 between 2**10 and 2**20.
If zero, no diff size limit is set.
max_diff_entries = 1024
max_database_entries: Sets the maximum number of entries that the client is willing to have in the local database.
This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
max_database_entries = 1024
compression_type: The compression type supported by the client.
compression_type = webrisk_v1.CompressionType.RAW
"""

webrisk_client = webrisk_v1.WebRiskServiceClient()

constraints = webrisk_v1.ComputeThreatListDiffRequest.Constraints()
constraints.max_diff_entries = max_diff_entries
constraints.max_database_entries = max_database_entries
constraints.supported_compressions = [compression_type]

request = webrisk_v1.ComputeThreatListDiffRequest()
request.threat_type = threat_type
request.version_token = version_token
request.constraints = constraints

response = webrisk_client.compute_threat_list_diff(request)

# The returned response contains the following information:
# https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
# Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
print(response.response_type)

# New version token to be used the next time when querying.
print(response.new_version_token)

# Recommended next diff timestamp.
print(response.recommended_next_diff)

print("Obtained threat list diff.")
# [END webrisk_compute_threatlist_diff]
42 changes: 42 additions & 0 deletions webrisk/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

# 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": {},
}
1 change: 1 addition & 0 deletions webrisk/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.2.0
1 change: 1 addition & 0 deletions webrisk/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-webrisk==1.9.0
Loading

0 comments on commit 15adcfa

Please sign in to comment.