Skip to content

Commit

Permalink
fixit: Add type-hints to Python sample at functions/ocr/app (#9990)
Browse files Browse the repository at this point in the history
* fixit: Add type-hints to Python sample at functions/ocr/app

* Fixing stuff

* Fixing stuff

* Enforcing type checks

* Enforcing type checks

* Fixing any typing

* chore(deps): update dependency shapely to v2 (#10004)

* Adding deletion of rows with NULL dag_id (#10002)

Co-authored-by: Charles Engelke <engelke@google.com>

* fixit: add type-hints to functions/v2/ocr (#9980)

b/280879671

## Description

Fixes b/280879671

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

## Checklist
- [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md)
- [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file)
- [ ] **Tests** pass:   `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] **Lint** pass:   `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones)
- [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones)
- [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample
- [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample
- [ ] Please **merge** this PR for me once it is approved

* [DLP] Implemenetd dlp_inspect_image_listed_infotypes with unit test cases (#9872)

## Description
Implemenetd dlp_inspect_image_listed_infotypes with unit test cases. Java equivalent: https://cloud.google.com/dlp/docs/samples/dlp-inspect-image-listed-infotypes#dlp_inspect_image_listed_infotypes-java

Fixes #<ISSUE-NUMBER>

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

## Checklist
- [X] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md)
- [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file)
- [X] **Tests** pass:   `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup))
- [X] **Lint** pass:   `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup))
- [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones)
- [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones)
- [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample
- [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample
- [X] Please **merge** this PR for me once it is approved

* chore(deps): update dependency structlog to v22.3.0 (#8540)

Co-authored-by: Charles Engelke <engelke@google.com>

* fixit: update docstrings for Cloud Run samples (#10007)

* chore: update docstrings for Cloud Run samples

* lint

* Update render.py

* fix: Add type hints for KMS snippets (#9979)

* fix: Add type hints for KMS snippets

* fix: type

* fix: even more types

* black

* fix: remove debugging note

* isort

* manual sort, apparently

* Move all imports to top of files, sort, move region tags

* even more import fixes

* black, isort

* black, again

* Fixing typing info

* Trying to fix typing

---------

Co-authored-by: Charles Engelke <engelke@google.com>
Co-authored-by: Mend Renovate <bot@renovateapp.com>
Co-authored-by: kubasieron <89135874+kubasieron@users.noreply.github.com>
Co-authored-by: Avani-Thakker-Crest <129363704+Avani-Thakker-Crest@users.noreply.github.com>
Co-authored-by: Averi Kitsch <akitsch@google.com>
Co-authored-by: Katie McLaughlin <katie@glasnt.com>
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
  • Loading branch information
8 people committed May 27, 2023
1 parent 2ec9a04 commit 8b4947d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
90 changes: 72 additions & 18 deletions functions/ocr/app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# 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
#
Expand All @@ -16,6 +16,7 @@
import base64
import json
import os
from typing import Dict, TypeVar

from google.cloud import pubsub_v1
from google.cloud import storage
Expand All @@ -32,8 +33,22 @@


# [START functions_ocr_detect]
def detect_text(bucket, filename):
print(f"Looking for text in image {filename}")
def detect_text(bucket: str, filename: str) -> None:
"""
Extract the text from an image uploaded to Cloud Storage.
Extract the text from an image uploaded to Cloud Storage, then
publish messages requesting subscribing services translate the text
to each target language and save the result.
Args:
bucket: name of GCS bucket in which the file is stored.
filename: name of the file to be read.
Returns:
None; the output is written to stdout and Stackdriver Logging.
"""
print("Looking for text in image {}".format(filename))

futures = []

Expand Down Expand Up @@ -74,9 +89,22 @@ def detect_text(bucket, filename):

# [END functions_ocr_detect]

T = TypeVar('T')


# [START message_validatation_helper]
def validate_message(message, param):
def validate_message(message: Dict[str, T], param: str) -> T:
"""
Placeholder function for validating message parts.
Args:
message: message to be validated.
param: name of the message parameter to be validated.
Returns:
The value of message['param'] if it's valid. Throws ValueError
if it's not valid.
"""
var = message.get(param)
if not var:
raise ValueError(
Expand All @@ -86,34 +114,49 @@ def validate_message(message, param):
)
)
return var


# [END message_validatation_helper]


# [START functions_ocr_process]
def process_image(file, context):
def process_image(file_info: dict, context: dict) -> None:
"""Cloud Function triggered by Cloud Storage when a file is changed.
Args:
file (dict): Metadata of the changed file, provided by the triggering
Cloud Storage event.
context (google.cloud.functions.Context): Metadata of triggering event.
file_info: Metadata of the changed file, provided by the
triggering Cloud Storage event.
context: a dictionary containing metadata about the event.
Returns:
None; the output is written to stdout and Stackdriver Logging
None; the output is written to stdout and Stackdriver Logging.
"""
bucket = validate_message(file, "bucket")
name = validate_message(file, "name")
bucket = validate_message(file_info, "bucket")
name = validate_message(file_info, "name")

detect_text(bucket, name)

print("File {} processed.".format(file["name"]))
print("File {} processed.".format(file_info["name"]))


# [END functions_ocr_process]


# [START functions_ocr_translate]
def translate_text(event, context):
def translate_text(event: dict, context: dict) -> None:
"""
Cloud Function triggered by PubSub when a message is received from
a subscription.
Translates the text in the message from the specified source language
to the requested target language, then sends a message requesting another
service save the result.
Args:
event: dictionary containing the PubSub event.
context: a dictionary containing metadata about the event.
Returns:
None; the output is written to stdout and Stackdriver Logging.
"""
if event.get("data"):
message_data = base64.b64decode(event["data"]).decode("utf-8")
message = json.loads(message_data)
Expand All @@ -135,17 +178,28 @@ def translate_text(event, context):
"filename": filename,
"lang": target_lang,
}
message_data = json.dumps(message).encode("utf-8")
encoded_message = json.dumps(message).encode("utf-8")
topic_path = publisher.topic_path(project_id, topic_name)
future = publisher.publish(topic_path, data=message_data)
future = publisher.publish(topic_path, data=encoded_message)
future.result()


# [END functions_ocr_translate]


# [START functions_ocr_save]
def save_result(event, context):
def save_result(event: dict, context: dict) -> None:
"""
Cloud Function triggered by PubSub when a message is received from
a subscription.
Args:
event: dictionary containing the PubSub event.
context: a dictionary containing metadata about the event.
Returns:
None; the output is written to stdout and Stackdriver Logging.
"""
if event.get("data"):
message_data = base64.b64decode(event["data"]).decode("utf-8")
message = json.loads(message_data)
Expand Down
2 changes: 2 additions & 0 deletions functions/ocr/app/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import main

# flake8: noqa


class TestGCFPyOCRSample():
@mock.patch.object(main, 'publisher')
Expand Down
1 change: 1 addition & 0 deletions functions/ocr/app/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
"RESULT_TOPIC": "result-topic",
"RESULT_BUCKET": "result-bucket",
},
"enforce_type_hints": True,
}

0 comments on commit 8b4947d

Please sign in to comment.