diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/aio/sample_analyze_custom_documents_async.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/aio/sample_analyze_custom_documents_async.py index 327004db2ecf8..c394ee1dfe45a 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/samples/aio/sample_analyze_custom_documents_async.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/samples/aio/sample_analyze_custom_documents_async.py @@ -29,20 +29,43 @@ import os import asyncio -import sys + +def print_table(header_names, table_data): + """Print a two-dimensional array like a table. + + Based on provided column header names and two two-dimensional array data, print the strings like table. + + Args: + header_names: An array of string, it's the column header names. e.g. ["name", "gender", "age"] + table_data: A two-dimensional array, they're the table data. e.g. [["Mike", "M", 25], ["John", "M", 19], ["Lily", "F", 23]] + Return: None + It's will print the string like table in output window. e.g. + Name Gender Age + Mike M 25 + John M 19 + Lily F 23 + """ + max_len_list = [] + for i in range(len(header_names)): + col_values = list(map(lambda row: len(str(row[i])), table_data)) + col_values.append(len(str(header_names[i]))) + max_len_list.append(max(col_values)) + + row_format_str = "".join(map(lambda len: f"{{:<{len + 4}}}", max_len_list)) + + print(row_format_str.format(*header_names)) + for row in table_data: + print(row_format_str.format(*row)) + async def analyze_custom_documents(custom_model_id): - path_of_parents = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") path_to_sample_documents = os.path.abspath( - os.path.join(path_of_parents, "./sample_forms/forms/Form_1.jpg") + os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Form_1.jpg") ) - sys.path.append(path_of_parents) - # [START analyze_custom_documents] from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence.aio import DocumentIntelligenceClient from azure.ai.documentintelligence.models import AnalyzeResult - from helper import utils endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"] key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"] @@ -101,7 +124,7 @@ async def analyze_custom_documents(custom_model_id): ) row_data = list(map(extract_value_by_col_name, col_names)) table_rows.append(row_data) - utils.print_table(col_names, table_rows) + print_table(col_names, table_rows) print("-----------------------------------") # [END analyze_custom_documents] diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/helper/__init__.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/helper/__init__.py deleted file mode 100644 index e69de29bb2d1d..0000000000000 diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/helper/utils.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/helper/utils.py deleted file mode 100644 index f863ef8d5adaa..0000000000000 --- a/sdk/documentintelligence/azure-ai-documentintelligence/samples/helper/utils.py +++ /dev/null @@ -1,41 +0,0 @@ -# coding: utf-8 - -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# -------------------------------------------------------------------------- - -""" -FILE: utils.py - -DESCRIPTION: - These util functions provide an intuitionistic way to organize data. Make sample code more concise. -""" - -def print_table(header_names, table_data): - """Print a two-dimensional array like a table. - - Based on provided column header names and two two-dimensional array data, print the strings like table. - - Args: - header_names: An array of string, it's the column header names. e.g. ["name", "gender", "age"] - table_data: A two-dimensional array, they're the table data. e.g. [["Mike", "M", 25], ["John", "M", 19], ["Lily", "F", 23]] - Return: None - It's will print the string like table in output window. e.g. - Name Gender Age - Mike M 25 - John M 19 - Lily F 23 - """ - max_len_list = [] - for i in range(len(header_names)): - col_values = list(map(lambda row: len(str(row[i])), table_data)) - col_values.append(len(str(header_names[i]))) - max_len_list.append(max(col_values)) - - row_format_str = "".join(map(lambda len: f"{{:<{len + 4}}}", max_len_list)) - - print(row_format_str.format(*header_names)) - for row in table_data: - print(row_format_str.format(*row)) diff --git a/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_analyze_custom_documents.py b/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_analyze_custom_documents.py index 0982849e0cd09..19c21c42400ff 100644 --- a/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_analyze_custom_documents.py +++ b/sdk/documentintelligence/azure-ai-documentintelligence/samples/sample_analyze_custom_documents.py @@ -29,6 +29,33 @@ import os +def print_table(header_names, table_data): + """Print a two-dimensional array like a table. + + Based on provided column header names and two two-dimensional array data, print the strings like table. + + Args: + header_names: An array of string, it's the column header names. e.g. ["name", "gender", "age"] + table_data: A two-dimensional array, they're the table data. e.g. [["Mike", "M", 25], ["John", "M", 19], ["Lily", "F", 23]] + Return: None + It's will print the string like table in output window. e.g. + Name Gender Age + Mike M 25 + John M 19 + Lily F 23 + """ + max_len_list = [] + for i in range(len(header_names)): + col_values = list(map(lambda row: len(str(row[i])), table_data)) + col_values.append(len(str(header_names[i]))) + max_len_list.append(max(col_values)) + + row_format_str = "".join(map(lambda len: f"{{:<{len + 4}}}", max_len_list)) + + print(row_format_str.format(*header_names)) + for row in table_data: + print(row_format_str.format(*row)) + def analyze_custom_documents(custom_model_id): path_to_sample_documents = os.path.abspath( @@ -38,7 +65,6 @@ def analyze_custom_documents(custom_model_id): from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import AnalyzeResult - from helper import utils endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"] key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"] @@ -97,7 +123,7 @@ def analyze_custom_documents(custom_model_id): ) row_data = list(map(extract_value_by_col_name, col_names)) table_rows.append(row_data) - utils.print_table(col_names, table_rows) + print_table(col_names, table_rows) print("------------------------------------") # [END analyze_custom_documents]