Skip to content

Commit ed3a3c2

Browse files
Merge pull request #60 from rosette-api/RCB-495_example_exception_handling
Rcb 495 example exception handling
2 parents c581263 + 22fe618 commit ed3a3c2

18 files changed

+94
-39
lines changed

examples/categories.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters
11+
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -20,7 +20,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
2020

2121
# Use a URL to input data instead of a string
2222
params["contentUri"] = url
23-
return api.categories(params)
23+
try:
24+
return api.categories(params)
25+
except RosetteException as e:
26+
print(e)
2427

2528

2629
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/entities.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters
11+
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
params = DocumentParameters()
1919
params["content"] = entities_text_data
2020
params["genre"] = "social-media"
21-
return api.entities(params)
21+
try:
22+
return api.entities(params)
23+
except RosetteException as e:
24+
print(e)
2225

2326
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')
2427
parser.add_argument('-k', '--key', help='Rosette API Key', required=True)

examples/info.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters
11+
from rosette.api import API, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1515
# Create an API instance
1616
api = API(user_key=key, service_url=altUrl)
1717

18-
return api.info()
18+
try:
19+
return api.info()
20+
except RosetteException as e:
21+
print(e)
1922

2023

2124
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/language.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters
11+
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -19,7 +19,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1919
params = DocumentParameters()
2020
params["content"] = language_data
2121
api.setCustomHeaders("X-RosetteAPI-App", "python-app")
22-
return api.language(params)
22+
try:
23+
return api.language(params)
24+
except RosetteException as e:
25+
print(e)
2326

2427

2528
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/morphology_complete.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters
11+
from rosette.api import API, DocumentParameters, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
morphology_complete_data = "The quick brown fox jumped over the lazy dog. Yes he did."
1919
params = DocumentParameters()
2020
params["content"] = morphology_complete_data
21-
return api.morphology(params)
21+
try:
22+
return api.morphology(params)
23+
except RosetteException as e:
24+
print(e)
2225

2326

2427
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/morphology_compound-components.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters, MorphologyOutput
11+
from rosette.api import API, DocumentParameters, MorphologyOutput, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
morphology_compound_components_data = "Rechtsschutzversicherungsgesellschaften"
1919
params = DocumentParameters()
2020
params["content"] = morphology_compound_components_data
21-
return api.morphology(params, MorphologyOutput.COMPOUND_COMPONENTS)
21+
try:
22+
return api.morphology(params, MorphologyOutput.COMPOUND_COMPONENTS)
23+
except RosetteException as e:
24+
print(e)
2225

2326

2427
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/morphology_han-readings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters, MorphologyOutput
11+
from rosette.api import API, DocumentParameters, MorphologyOutput, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
morphology_han_readings_data = "北京大学生物系主任办公室内部会议"
1919
params = DocumentParameters()
2020
params["content"] = morphology_han_readings_data
21-
return api.morphology(params, MorphologyOutput.HAN_READINGS)
21+
try:
22+
return api.morphology(params, MorphologyOutput.HAN_READINGS)
23+
except RosetteException as e:
24+
print(e)
2225

2326

2427
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/morphology_lemmas.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters, MorphologyOutput
11+
from rosette.api import API, DocumentParameters, MorphologyOutput, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
morphology_lemmas_data = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
1919
params = DocumentParameters()
2020
params["content"] = morphology_lemmas_data
21-
return api.morphology(params, MorphologyOutput.LEMMAS)
21+
try:
22+
return api.morphology(params, MorphologyOutput.LEMMAS)
23+
except RosetteException as e:
24+
print(e)
2225

2326

2427
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/morphology_parts-of-speech.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, DocumentParameters, MorphologyOutput
11+
from rosette.api import API, DocumentParameters, MorphologyOutput, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -18,7 +18,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
1818
morphology_parts_of_speech_data = "The fact is that the geese just went back to get a rest and I'm not banking on their return soon"
1919
params = DocumentParameters()
2020
params["content"] = morphology_parts_of_speech_data
21-
return api.morphology(params, MorphologyOutput.PARTS_OF_SPEECH)
21+
try:
22+
return api.morphology(params, MorphologyOutput.PARTS_OF_SPEECH)
23+
except RosetteException as e:
24+
print(e)
2225

2326

2427
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

examples/name_similarity.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import os
1010

11-
from rosette.api import API, NameSimilarityParameters
11+
from rosette.api import API, NameSimilarityParameters, RosetteException
1212

1313

1414
def run(key, altUrl='https://api.rosette.com/rest/v1/'):
@@ -20,7 +20,10 @@ def run(key, altUrl='https://api.rosette.com/rest/v1/'):
2020
params = NameSimilarityParameters()
2121
params["name1"] = {"text": matched_name_data1, "language": "eng", "entityType": "PERSON"}
2222
params["name2"] = {"text": matched_name_data2, "entityType": "PERSON"}
23-
return api.name_similarity(params)
23+
try:
24+
return api.name_similarity(params)
25+
except RosetteException as e:
26+
print(e)
2427

2528

2629
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Calls the ' + os.path.splitext(os.path.basename(__file__))[0] + ' endpoint')

0 commit comments

Comments
 (0)