Skip to content

Commit 9ebf063

Browse files
sptramerlmazuel
authored andcommitted
Run PEP8 on content (#57)
* Updated Python content to comply with PEP8 (via autopep8) * Update autosuggest_samples.py
1 parent 86ee9cf commit 9ebf063

32 files changed

+901
-514
lines changed

example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@
1212

1313
import samples.tools
1414

15+
1516
def run_all_samples():
1617
for _, section_name_name, ispkg in pkgutil.walk_packages(samples.__path__):
1718
if not ispkg:
1819
continue
1920
section_package_name = "samples."+section_name_name
2021
section_package = importlib.import_module(section_package_name)
2122
for _, sample_name, _ in pkgutil.iter_modules(section_package.__path__):
22-
sample_module = importlib.import_module(section_package_name+"."+sample_name)
23-
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
23+
sample_module = importlib.import_module(
24+
section_package_name+"."+sample_name)
25+
subkey_env_name = getattr(
26+
sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
2427
if not subkey_env_name:
2528
continue
2629
print("Executing sample from ", sample_name)
2730
try:
28-
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)
31+
samples.tools.execute_samples(
32+
sample_module.__dict__, subkey_env_name)
2933
except samples.tools.SubscriptionKeyError as err:
3034
print("{}\n".format(err))
3135

36+
3237
if __name__ == "__main__":
3338
run_all_samples()

samples/anomaly_detector_samples.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import os
88

99
SUBSCRIPTION_KEY_ENV_NAME = "ANOMALYDETECTOR_SUBSCRIPTION_KEY"
10-
ANOMALYDETECTOR_LOCATION = os.environ.get("ANOMALYDETECTOR_LOCATION", "westcentralus")
10+
ANOMALYDETECTOR_LOCATION = os.environ.get(
11+
"ANOMALYDETECTOR_LOCATION", "westcentralus")
12+
13+
CSV_FOLDER = os.path.join(os.path.dirname(
14+
os.path.realpath(__file__)), "csv_files")
1115

12-
CSV_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csv_files")
1316

1417
def get_series_from_file(path):
1518
df = pd.read_csv(path, header=None, encoding="utf-8", parse_dates=[0])
@@ -18,16 +21,21 @@ def get_series_from_file(path):
1821
series.append(Point(timestamp=row[0], value=row[1]))
1922
return series
2023

24+
2125
def get_request():
22-
series = get_series_from_file(os.path.join(CSV_FOLDER, "anomaly_detector_daily_series.csv"))
26+
series = get_series_from_file(os.path.join(
27+
CSV_FOLDER, "anomaly_detector_daily_series.csv"))
2328
return Request(series=series, granularity=Granularity.daily)
2429

30+
2531
def entire_detect(subscription_key):
2632
print("Sample of detecting anomalies in the entire series.")
2733

28-
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
34+
endpoint = "https://{}.api.cognitive.microsoft.com".format(
35+
ANOMALYDETECTOR_LOCATION)
2936
try:
30-
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
37+
client = AnomalyDetectorClient(
38+
endpoint, CognitiveServicesCredentials(subscription_key))
3139
request = get_request()
3240
response = client.entire_detect(request)
3341
if True in response.is_anomaly:
@@ -44,12 +52,15 @@ def entire_detect(subscription_key):
4452
else:
4553
print(e)
4654

55+
4756
def last_detect(subscription_key):
4857
print("Sample of detecting whether the latest point in series is anomaly.")
4958

50-
endpoint = "https://{}.api.cognitive.microsoft.com".format(ANOMALYDETECTOR_LOCATION)
59+
endpoint = "https://{}.api.cognitive.microsoft.com".format(
60+
ANOMALYDETECTOR_LOCATION)
5161
try:
52-
client = AnomalyDetectorClient(endpoint, CognitiveServicesCredentials(subscription_key))
62+
client = AnomalyDetectorClient(
63+
endpoint, CognitiveServicesCredentials(subscription_key))
5364
request = get_request()
5465
response = client.last_detect(request)
5566
if response.is_anomaly:
@@ -63,8 +74,10 @@ def last_detect(subscription_key):
6374
else:
6475
print(e)
6576

77+
6678
if __name__ == "__main__":
67-
import sys, os.path
79+
import sys
80+
import os.path
6881
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
6982
from tools import execute_samples
7083
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

samples/knowledge/qna_maker_samples.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
SUBSCRIPTION_KEY_ENV_NAME = "QNA_SUBSCRIPTION_KEY"
99
QNA_LOCATION = os.environ.get("QNA_LOCATION", "westus")
1010

11+
1112
def knowledge_based_crud_sample(subscription_key):
1213
"""KnowledgeBasedCRUDSample.
1314
@@ -23,17 +24,18 @@ def _create_sample_kb(client):
2324
questions=["How do I manage my knowledgebase?"],
2425
metadata=[MetadataDTO(name="Category", value="api")]
2526
)
26-
urls = ["https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
27+
urls = [
28+
"https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"]
2729
create_kb_dto = CreateKbDTO(
2830
name="QnA Maker FAQ from quickstart",
2931
qna_list=[qna],
3032
urls=urls
3133
)
32-
create_op = client.knowledgebase.create(create_kb_payload=create_kb_dto)
34+
create_op = client.knowledgebase.create(
35+
create_kb_payload=create_kb_dto)
3336
create_op = _monitor_operation(client=client, operation=create_op)
3437
return create_op.resource_location.replace("/knowledgebases/", "")
3538

36-
3739
def _monitor_operation(client, operation):
3840
"""Helper function for knowledge_based_crud_sample.
3941
@@ -42,17 +44,20 @@ def _monitor_operation(client, operation):
4244
"""
4345
for i in range(20):
4446
if operation.operation_state in [OperationStateType.not_started, OperationStateType.running]:
45-
print("Waiting for operation: {} to complete.".format(operation.operation_id))
47+
print("Waiting for operation: {} to complete.".format(
48+
operation.operation_id))
4649
time.sleep(5)
47-
operation = client.operations.get_details(operation_id=operation.operation_id)
50+
operation = client.operations.get_details(
51+
operation_id=operation.operation_id)
4852
else:
4953
break
5054
if operation.operation_state != OperationStateType.succeeded:
51-
raise Exception("Operation {} failed to complete.".format(operation.operation_id))
55+
raise Exception("Operation {} failed to complete.".format(
56+
operation.operation_id))
5257
return operation
5358

54-
55-
client = QnAMakerClient(endpoint="https://{}.api.cognitive.microsoft.com".format(QNA_LOCATION), credentials=CognitiveServicesCredentials(subscription_key))
59+
client = QnAMakerClient(endpoint="https://{}.api.cognitive.microsoft.com".format(
60+
QNA_LOCATION), credentials=CognitiveServicesCredentials(subscription_key))
5661

5762
# Create a KB
5863
print("Creating KB...")
@@ -68,7 +73,8 @@ def _monitor_operation(client, operation):
6873
]
6974
)
7075
)
71-
update_op = client.knowledgebase.update(kb_id=kb_id, update_kb=update_kb_operation_dto)
76+
update_op = client.knowledgebase.update(
77+
kb_id=kb_id, update_kb=update_kb_operation_dto)
7278
_monitor_operation(client=client, operation=update_op)
7379

7480
# Publish the KB
@@ -88,7 +94,8 @@ def _monitor_operation(client, operation):
8894

8995

9096
if __name__ == "__main__":
91-
import sys, os.path
97+
import sys
98+
import os.path
9299
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
93100
from tools import execute_samples
94-
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
101+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

samples/language/luis/luis_authoring_samples.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
SUBSCRIPTION_KEY_ENV_NAME = "LUIS_SUBSCRIPTION_KEY"
1111

12+
1213
def booking_app(subscription_kuy):
1314
"""Authoring.
1415
@@ -24,7 +25,8 @@ def booking_app(subscription_kuy):
2425
default_app_name = "Contoso-{}".format(datetime.datetime.now())
2526
version_id = "0.1"
2627

27-
print("Creating App {}, version {}".format(default_app_name, version_id))
28+
print("Creating App {}, version {}".format(
29+
default_app_name, version_id))
2830

2931
app_id = client.apps.add({
3032
'name': default_app_name,
@@ -111,17 +113,21 @@ def get_example_label(utterance, entity_name, value):
111113
utterances = [{
112114
'text': find_economy_to_madrid,
113115
'intent_name': intent_name,
114-
'entity_labels':[
115-
get_example_label(find_economy_to_madrid, "Flight", "economy to madrid"),
116-
get_example_label(find_economy_to_madrid, "Destination", "Madrid"),
116+
'entity_labels': [
117+
get_example_label(find_economy_to_madrid,
118+
"Flight", "economy to madrid"),
119+
get_example_label(find_economy_to_madrid,
120+
"Destination", "Madrid"),
117121
get_example_label(find_economy_to_madrid, "Class", "economy"),
118122
]
119123
}, {
120124
'text': find_first_to_london,
121125
'intent_name': intent_name,
122-
'entity_labels':[
123-
get_example_label(find_first_to_london, "Flight", "London in first class"),
124-
get_example_label(find_first_to_london, "Destination", "London"),
126+
'entity_labels': [
127+
get_example_label(find_first_to_london,
128+
"Flight", "London in first class"),
129+
get_example_label(find_first_to_london,
130+
"Destination", "London"),
125131
get_example_label(find_first_to_london, "Class", "first"),
126132
]
127133
}]
@@ -143,7 +149,8 @@ def get_example_label(utterance, entity_name, value):
143149
while not is_trained:
144150
time.sleep(1)
145151
status = client.train.get_status(app_id, version_id)
146-
is_trained = all(m.details.status in trained_status for m in status)
152+
is_trained = all(
153+
m.details.status in trained_status for m in status)
147154

148155
print("Your app is trained. You can now go to the LUIS portal and test it!")
149156

@@ -158,12 +165,14 @@ def get_example_label(utterance, entity_name, value):
158165
'region': 'westus'
159166
}
160167
)
161-
endpoint = publish_result.endpoint_url + "?subscription-key=" + subscription_key + "&q="
168+
endpoint = publish_result.endpoint_url + \
169+
"?subscription-key=" + subscription_key + "&q="
162170
print("Your app is published. You can now go to test it on\n{}".format(endpoint))
163171

164172
except Exception as err:
165173
print("Encountered exception. {}".format(err))
166174

175+
167176
def management(subscription_key):
168177
"""Managing
169178
@@ -179,7 +188,8 @@ def management(subscription_key):
179188
default_app_name = "Contoso-{}".format(datetime.datetime.now())
180189
version_id = "0.1"
181190

182-
print("Creating App {}, version {}".format(default_app_name, version_id))
191+
print("Creating App {}, version {}".format(
192+
default_app_name, version_id))
183193

184194
app_id = client.apps.add({
185195
'name': default_app_name,
@@ -224,17 +234,20 @@ def management(subscription_key):
224234
# Listing versions
225235
print("\nList all versions in this app")
226236
for version in client.versions.list(app_id):
227-
print("\t->Version: '{}', training status: {}".format(version.version, version.training_status))
237+
print("\t->Version: '{}', training status: {}".format(version.version,
238+
version.training_status))
228239

229240
# Print app details
230241
print("\nPrint app '{}' details".format(default_app_name))
231242
details = client.apps.get(app_id)
232-
pprint(details.as_dict()) # as_dict "dictify" the object, by default it's attribute based. e.g. details.name
243+
# as_dict "dictify" the object, by default it's attribute based. e.g. details.name
244+
pprint(details.as_dict())
233245

234246
# Print version details
235247
print("\nPrint version '{}' details".format(version_id))
236248
details = client.versions.get(app_id, version_id)
237-
pprint(details.as_dict()) # as_dict "dictify" the object, by default it's attribute based. e.g. details.name
249+
# as_dict "dictify" the object, by default it's attribute based. e.g. details.name
250+
pprint(details.as_dict())
238251

239252
# Delete an app
240253
print("\nDelete app '{}'".format(default_app_name))
@@ -244,8 +257,10 @@ def management(subscription_key):
244257
except Exception as err:
245258
print("Encountered exception. {}".format(err))
246259

260+
247261
if __name__ == "__main__":
248-
import sys, os.path
262+
import sys
263+
import os.path
249264
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
250265
from tools import execute_samples
251266
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

samples/language/luis/luis_runtime_samples.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
CWD = os.path.dirname(__file__)
1212

13+
1314
def runtime(subscription_key):
1415
"""Resolve.
1516
@@ -47,7 +48,8 @@ def runtime(subscription_key):
4748

4849

4950
if __name__ == "__main__":
50-
import sys, os.path
51+
import sys
52+
import os.path
5153
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
5254
from tools import execute_samples
5355
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

samples/language/spellcheck_samples.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
SUBSCRIPTION_KEY_ENV_NAME = "SPELLCHECK_SUBSCRIPTION_KEY"
55

6+
67
def spellcheck(subscription_key):
78
"""SpellCheck.
89
@@ -17,15 +18,21 @@ def spellcheck(subscription_key):
1718
if result.flagged_tokens:
1819
first_spellcheck_result = result.flagged_tokens[0]
1920

20-
print("SpellCheck result count: {}".format(len(result.flagged_tokens)))
21-
print("First SpellCheck token: {}".format(first_spellcheck_result.token))
22-
print("First SpellCheck type: {}".format(first_spellcheck_result.type))
23-
print("First SpellCheck suggestion count: {}".format(len(first_spellcheck_result.suggestions)))
21+
print("SpellCheck result count: {}".format(
22+
len(result.flagged_tokens)))
23+
print("First SpellCheck token: {}".format(
24+
first_spellcheck_result.token))
25+
print("First SpellCheck type: {}".format(
26+
first_spellcheck_result.type))
27+
print("First SpellCheck suggestion count: {}".format(
28+
len(first_spellcheck_result.suggestions)))
2429

2530
if first_spellcheck_result.suggestions:
2631
first_suggestion = first_spellcheck_result.suggestions[0]
27-
print("First SpellCheck suggestion score: {}".format(first_suggestion.score))
28-
print("First SpellCheck suggestion: {}".format(first_suggestion.suggestion))
32+
print("First SpellCheck suggestion score: {}".format(
33+
first_suggestion.score))
34+
print("First SpellCheck suggestion: {}".format(
35+
first_suggestion.suggestion))
2936
else:
3037
print("Couldn't get any Spell check results!")
3138

@@ -37,7 +44,8 @@ def spellcheck(subscription_key):
3744

3845

3946
if __name__ == "__main__":
40-
import sys, os.path
41-
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
47+
import sys
48+
import os.path
49+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
4250
from tools import execute_samples
4351
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)