3
\$\begingroup\$

BACKGROUND

I have a function where i cache file from aws s3. I provide it the path on s3 and also the local path of where i want the cached file to live. Sometimes if the file does not exist on s3 it will return a 404 error. That makes sense but i want to handle it gracefully. What i want to do is if the file i am trying to cache does not exist I want my get_overview_stream function to return None.

CODE

def get_overview_stream(*, environment, proxy_key):
 s3_overview_file_path = f"s3://{FOO_TRACK_BUCKET}/{environment}"
 overview_file = f"{proxy_key}.csv"
 local_path = cache_directory(environment)
 try:
 cache.cache_file(s3_overview_file_path, local_path, overview_file)
 overview_file_cache = local_path / f"{proxy_key}.csv"
 return overview_file_cache.open("r")
 except botocore.exceptions.ClientError as e:
 if e.response["Error"]["Code"] == "404":
 return None
 else:
 raise

Attempted Solution Issue

I am very new to python so i am not sure if this the best way to handle this exception and if there is a more cleaner way. If so i would love to hear feedback.

RootTwo
10.6k1 gold badge14 silver badges30 bronze badges
asked Aug 15, 2020 at 16:31
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Honestly, I've never used to libraries involved here, but I don't see anything wrong with this approach. The exception handling seems fine to me. \$\endgroup\$ Commented Aug 15, 2020 at 17:57
  • \$\begingroup\$ The only thing I would change here is adding a docstring and type information. You could move the last 2 lines in the try block outside. raising a custom exception instead of returning the sentinel value None might also be an improvement, but that last point id a matter of taste /habit \$\endgroup\$ Commented Aug 15, 2020 at 20:05
  • \$\begingroup\$ Also, always open a file with a context manager instead of a bare open() \$\endgroup\$ Commented Aug 15, 2020 at 20:06
  • \$\begingroup\$ @MaartenFabré can you show example of what you mean by custom exception? \$\endgroup\$ Commented Aug 18, 2020 at 11:51

1 Answer 1

1
\$\begingroup\$

I would suggest not opening the file in your function but instead returning the path or None, such that the caller can open the file using a with statement.

answered Aug 18, 2020 at 3:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.