Created
September 1, 2022 14:09
-
-
Save martin-martin/cb7d7ad10ea42956d110d23309d18db8 to your computer and use it in GitHub Desktop.
Potential code for answering the question (errors bubble up)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How can I make sure that the exception messages | |
# of Exceptions that occur in the sub-function `load_model()` | |
# show up in the stack trace of the `main()` function? | |
class UnsupportedInputError(Exception): pass | |
class SourceEqualsTargetError(Exception): pass | |
supported_inputs = ["a", "b", ...] | |
def load_model(source, target): | |
if source not in supported_inputs or target not in supported_inputs: | |
raise UnsupportedInputError("not supported pair") | |
elif source == target: | |
raise SourceEqualsTargetError("source and target shouldn't be same") | |
else: # Load the model | |
print("Loading model...") | |
model = "loaded model" # Replace with code logic | |
return model | |
def main(user_input): | |
model = load_model(user_input["source"], user_input["target"]) | |
# try: | |
# model = load_model(user_input["source"], user_input["target"]) | |
# except (UnsupportedInputError, SourceEqualsTargetError) as e: | |
# print(e) | |
# # return sys.exit(0) # Do you want to quit when theres an error? | |
# else: # Do something with the model | |
# print(model) | |
print("Run #1") | |
valid_input = {"source": "a", "target": "b"} | |
main(valid_input) # Should raise no error | |
print("Run #2") | |
invalid_input_1 = {"source": "a", "target": "a"} | |
main(invalid_input_1) # Should raise SourceEqualsTargetError | |
print("Run #3") | |
invalid_input_2 = {"source": "a", "target": "unsupported"} | |
main(invalid_input_2) # Should raise UnsupportedInputError |
Updated code based on inputs on Bandcamp:
# How can I make sure that the exception messages
# of Exceptions that occur in the sub-function `load_model()`
# show up in the stack trace of the `main()` function?
class UnsupportedPairError(Exception):
pass
class SourceEqualsTargetError(Exception):
pass
SUPPORTED_PARAMS = ["a", "b", ...]
def main(user_input):
model = load_model(user_input["source"], user_input["target"])
# try:
# model = load_model(user_input["source"], user_input["target"])
# except (UnsupportedPairError, SourceEqualsTargetError) as e:
# print(e)
# # return sys.exit(0) # Do you want to quit when theres an error?
# else: # Do something with the model
# print(model)
def validate(source, target, supported_params):
if source not in supported_params or target not in supported_params:
raise UnsupportedPairError("not supported pair")
elif source == target:
raise SourceEqualsTargetError("source and target shouldn't be same")
def load_model(source, target):
validate(source, target, supported_params=SUPPORTED_PARAMS)
print("Loading model...")
model = "loaded model" # Replace with code logic
return model
print("Run #1")
valid_input = {"source": "a", "target": "b"}
main(valid_input) # Should raise no error
print("Run #2")
invalid_input_1 = {"source": "a", "target": "a"}
main(invalid_input_1) # Should raise SourceEqualsTargetError
print("Run #3")
invalid_input_2 = {"source": "a", "target": "unsupported"}
main(invalid_input_2) # Should raise UnsupportedPairError
What actually ended up being the final code:
# How can I make sure that the exception messages
# of Exceptions that occur in the sub-function `validate()`
# show up in the stack trace of the main function `main()`?
class SourceEqualsTargetError(Exception):
"""Source and target shouldn't be same."""
def main(user_input):
try:
validate(user_input["source"], user_input["target"])
except SourceEqualsTargetError:
print("Source and target are the same.")
else:
print("Done")
def validate(source, target):
if source == target:
raise SourceEqualsTargetError()
print("Run #1")
valid_input = {"source": 0, "target": 1}
main(valid_input)
print("Run #2")
invalid_input = {"source": 0, "target": 0}
main(invalid_input)
# Remove the `try...except` block in `validate()`.
With an intermediate state to answer the question that looked like this:
# How can I make sure that the exception messages
# of Exceptions that occur in the sub-function `load_model()`
# show up in the stack trace of the main function `main()`?
def load_model(source, target):
if source == target:
raise AssertionError("source and target shouldn't be same")
def main(X):
load_model(X['s'], X['t'])
print("Done")
X = {"s": 0, "t": 0}
main(X)
# Remove the `try...except` block in `load_model()`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference, we'll start the CoCo with the original code that the OH user posted: