Skip to content

Commit 4486c6a

Browse files
author
Mohamed Shaban
authored
add filter samples for list methods (Azure#18480)
* copy samples from tests branch * added samples for list submitted jobs both sync and async * fix parameters issues * create samples for sync client 'list all doc statuses' * added async test * move samples to proper directory * simplify sample name * expose job_id as env variable * update sample file name * update description * removed unused imports * update sample docs * update filter notes * update filter comment message
1 parent 8213288 commit 4486c6a

File tree

4 files changed

+357
-0
lines changed

4 files changed

+357
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
FILE: sample_list_document_statuses_with_filters_async.py
9+
10+
DESCRIPTION:
11+
This sample demonstrates how to list all the document in a translation job for the resource
12+
using different kind of filters/sorting/paging options
13+
14+
To set up your containers for translation and generate SAS tokens to your containers (or files)
15+
with the appropriate permissions, see the README.
16+
17+
USAGE:
18+
python sample_list_document_statuses_with_filters_async.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
22+
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
23+
3) JOB_ID - The ID of the translation job
24+
"""
25+
26+
import os
27+
import asyncio
28+
29+
async def sample_list_document_statuses_with_filters_async(self, client):
30+
# import libraries
31+
from azure.core.credentials import AzureKeyCredential
32+
from azure.ai.translation.document import (
33+
DocumentTranslationClient,
34+
)
35+
from datetime import datetime
36+
37+
# obtain client secrets
38+
endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
39+
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
40+
job_id = os.environ["JOB_ID"] # this should be the id for the job you'd like to list docs for!
41+
42+
# authorize client
43+
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
44+
45+
# set your filters
46+
'''
47+
Note:
48+
these are just sample values for the filters!
49+
please comment/uncomment/change what you are interested in using.
50+
'''
51+
start = datetime(2021, 4, 12)
52+
end = datetime(2021, 4, 14)
53+
statuses = ["Cancelled", "Failed"]
54+
order_by = ["createdDateTimeUtc desc"]
55+
results_per_page = 2
56+
skip = 3
57+
58+
# list jobs
59+
async with client:
60+
filtered_docs = client.list_all_document_statuses(
61+
job_id,
62+
# filters
63+
statuses=statuses,
64+
translated_after=start,
65+
translated_before=end,
66+
# ordering
67+
order_by=order_by,
68+
# paging
69+
skip=skip,
70+
results_per_page=results_per_page
71+
).by_page()
72+
73+
# check statuses
74+
async for page in filtered_docs:
75+
async for job in page:
76+
display_doc_info(job)
77+
78+
def display_doc_info(document):
79+
print("Document ID: {}".format(document.id))
80+
print("Document status: {}".format(document.status))
81+
if document.status == "Succeeded":
82+
print("Source document location: {}".format(document.source_document_url))
83+
print("Translated document location: {}".format(document.translated_document_url))
84+
print("Translated to language: {}\n".format(document.translate_to))
85+
86+
87+
async def main():
88+
await sample_list_document_statuses_with_filters_async()
89+
90+
if __name__ == '__main__':
91+
loop = asyncio.get_event_loop()
92+
loop.run_until_complete(main())
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
FILE: sample_list_submitted_jobs_with_filters_async.py
9+
10+
DESCRIPTION:
11+
This sample demonstrates how to list all the submitted translation jobs for the resource
12+
using different kind of filters/sorting/paging options
13+
14+
To set up your containers for translation and generate SAS tokens to your containers (or files)
15+
with the appropriate permissions, see the README.
16+
17+
USAGE:
18+
python sample_list_submitted_jobs_with_filters_async.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
22+
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
23+
"""
24+
25+
import os
26+
import asyncio
27+
28+
async def sample_list_submitted_jobs_with_filters_async():
29+
# import libraries
30+
from azure.core.credentials import AzureKeyCredential
31+
from azure.ai.translation.document.aio import DocumentTranslationClient
32+
33+
from datetime import datetime
34+
35+
# obtain client secrets
36+
endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
37+
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
38+
39+
# authorize client
40+
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
41+
async with client:
42+
# set your filters
43+
'''
44+
Note:
45+
these are just sample values for the filters!
46+
please comment/uncomment/change what you are interested in using.
47+
'''
48+
start = datetime(2021, 4, 12)
49+
end = datetime(2021, 4, 14)
50+
statuses = ["Cancelled", "Failed"]
51+
order_by = ["createdDateTimeUtc desc"]
52+
results_per_page = 2
53+
skip = 3
54+
55+
# list jobs
56+
submitted_jobs = client.list_submitted_jobs(
57+
# filters
58+
statuses=statuses,
59+
created_after=start,
60+
created_before=end,
61+
# ordering
62+
order_by=order_by,
63+
# paging
64+
skip=skip,
65+
results_per_page=results_per_page
66+
).by_page()
67+
68+
# check statuses
69+
async for page in submitted_jobs:
70+
async for job in page:
71+
display_job_info(job)
72+
73+
74+
def display_job_info(job):
75+
print("Job ID: {}".format(job.id))
76+
print("Job status: {}".format(job.status))
77+
print("Job created on: {}".format(job.created_on))
78+
print("Job last updated on: {}".format(job.last_updated_on))
79+
print("Total number of translations on documents: {}".format(job.documents_total_count))
80+
print("Total number of characters charged: {}".format(job.total_characters_charged))
81+
print("\nOf total documents...")
82+
print("{} failed".format(job.documents_failed_count))
83+
print("{} succeeded".format(job.documents_succeeded_count))
84+
print("{} cancelled\n".format(job.documents_cancelled_count))
85+
86+
87+
async def main():
88+
await sample_list_submitted_jobs_with_filters_async()
89+
90+
if __name__ == '__main__':
91+
loop = asyncio.get_event_loop()
92+
loop.run_until_complete(main())
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
FILE: sample_list_document_statuses_with_filters.py
9+
10+
DESCRIPTION:
11+
This sample demonstrates how to list all the document in a translation job for the resource
12+
using different kind of filters/sorting/paging options
13+
14+
To set up your containers for translation and generate SAS tokens to your containers (or files)
15+
with the appropriate permissions, see the README.
16+
17+
USAGE:
18+
python sample_list_document_statuses_with_filters.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
22+
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
23+
3) JOB_ID - The ID of the translation job
24+
"""
25+
26+
def sample_list_document_statuses_with_filters(self, client):
27+
# import libraries
28+
from azure.core.credentials import AzureKeyCredential
29+
from azure.ai.translation.document import (
30+
DocumentTranslationClient,
31+
)
32+
import os
33+
from datetime import datetime
34+
35+
# obtain client secrets
36+
endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
37+
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
38+
job_id = os.environ["JOB_ID"] # this should be the id for the job you'd like to list docs for!
39+
40+
# authorize client
41+
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
42+
43+
# set your filters
44+
'''
45+
Note:
46+
these are just sample values for the filters!
47+
please comment/uncomment/change what you are interested in using.
48+
'''
49+
start = datetime(2021, 4, 12)
50+
end = datetime(2021, 4, 14)
51+
statuses = ["Cancelled", "Failed"]
52+
order_by = ["createdDateTimeUtc desc"]
53+
results_per_page = 2
54+
skip = 3
55+
56+
# list jobs
57+
filtered_docs = client.list_all_document_statuses(
58+
job_id,
59+
# filters
60+
statuses=statuses,
61+
translated_after=start,
62+
translated_before=end,
63+
# ordering
64+
order_by=order_by,
65+
# paging
66+
skip=skip,
67+
results_per_page=results_per_page
68+
).by_page()
69+
70+
# check statuses
71+
for page in filtered_docs:
72+
for job in page:
73+
display_doc_info(job)
74+
75+
def display_doc_info(document):
76+
print("Document ID: {}".format(document.id))
77+
print("Document status: {}".format(document.status))
78+
if document.status == "Succeeded":
79+
print("Source document location: {}".format(document.source_document_url))
80+
print("Translated document location: {}".format(document.translated_document_url))
81+
print("Translated to language: {}\n".format(document.translate_to))
82+
83+
if __name__ == '__main__':
84+
sample_list_document_statuses_with_filters()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# coding=utf-8
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
FILE: sample_list_submitted_jobs_with_filters.py
9+
10+
DESCRIPTION:
11+
This sample demonstrates how to list all the submitted translation jobs for the resource
12+
using different kind of filters/sorting/paging options
13+
14+
To set up your containers for translation and generate SAS tokens to your containers (or files)
15+
with the appropriate permissions, see the README.
16+
17+
USAGE:
18+
python sample_list_submitted_jobs_with_filters.py
19+
20+
Set the environment variables with your own values before running the sample:
21+
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
22+
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
23+
"""
24+
25+
26+
27+
def sample_list_submitted_jobs_with_filters():
28+
# import libraries
29+
from azure.core.credentials import AzureKeyCredential
30+
from azure.ai.translation.document import (
31+
DocumentTranslationClient,
32+
)
33+
import os
34+
from datetime import datetime
35+
36+
# obtain client secrets
37+
endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
38+
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
39+
40+
# authorize client
41+
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
42+
43+
# set your filters
44+
'''
45+
Note:
46+
these are just sample values for the filters!
47+
please comment/uncomment/change what you are interested in using.
48+
'''
49+
start = datetime(2021, 4, 12)
50+
end = datetime(2021, 4, 14)
51+
statuses = ["Cancelled", "Failed"]
52+
order_by = ["createdDateTimeUtc desc"]
53+
results_per_page = 2
54+
skip = 3
55+
56+
# list jobs
57+
submitted_jobs = client.list_submitted_jobs(
58+
# filters
59+
statuses=statuses,
60+
created_after=start,
61+
created_before=end,
62+
# ordering
63+
order_by=order_by,
64+
# paging
65+
skip=skip,
66+
results_per_page=results_per_page
67+
).by_page()
68+
69+
# check statuses
70+
for page in submitted_jobs:
71+
for job in page:
72+
display_job_info(job)
73+
74+
75+
def display_job_info(job):
76+
print("Job ID: {}".format(job.id))
77+
print("Job status: {}".format(job.status))
78+
print("Job created on: {}".format(job.created_on))
79+
print("Job last updated on: {}".format(job.last_updated_on))
80+
print("Total number of translations on documents: {}".format(job.documents_total_count))
81+
print("Total number of characters charged: {}".format(job.total_characters_charged))
82+
print("\nOf total documents...")
83+
print("{} failed".format(job.documents_failed_count))
84+
print("{} succeeded".format(job.documents_succeeded_count))
85+
print("{} cancelled\n".format(job.documents_cancelled_count))
86+
87+
88+
if __name__ == '__main__':
89+
sample_list_submitted_jobs_with_filters()

0 commit comments

Comments
 (0)