1

How to Reduce Code Duplication of If-Else Statements in Python from below function. Also, which specific exception can i raise here?

def get_table_id_and_gcp_path(source, database, table, dc, env, date_folder):
 try:
 table_id=f"{database}_stg.{table}"
 gcp_path = f"gs://{env}/{database}/{table}/"
 if dc != '-1':
 table_id = table_id + '_' + dc + '_stg'
 gcp_path = gcp_path + dc + "/dt=" + date_folder + "/*.parquet"
 else:
 table_id = table_id + '_stg'
 gcp_path = gcp_path + "dt=" + date_folder + "/*.parquet"
 except Exception as e:
 print(e)
 raise
 return table_id, gcp_path
asked Feb 8, 2022 at 22:04
6
  • 1
    Nothing in there raises an exception you should catch. That's are all ordinary string manipulation. If you did it wrong, you have a logic error and you should allow it to crash. Commented Feb 8, 2022 at 22:07
  • What code duplication? Commented Feb 8, 2022 at 22:07
  • Too much going on in your try clause to come up with a succinct list of exceptions. Generally best to only include what you want to guard against breaking in the try and do the rest after. maybe just gcp_path def in try/except, then test some different ways of breaking the string subs to see what exceptions to include? Commented Feb 8, 2022 at 22:21
  • thanks @frederick-douglas-pearce. I am always confused as in which generic exception to raise. Because of this confusion, i always raise the generic exception only. How we will know if we need to raise an exception for a particular block of code? Commented Feb 8, 2022 at 22:26
  • You could figure out specific exceptions by anticipating an input, or program state, that would cause an error in the code, running the code with that input/state, and seeing what exception gets printed. In many cases, it is easy to anticipate, e.g. ValueError, KeyError, etc. For your problem, I'm actually not sure what input you could provide to break a simple string substitution as just about all objects have a __repr__ and/or __str__ method that will be printed, including functions, classes, ints, etc. I may pose this as an SO question because I'm curious Commented Feb 9, 2022 at 2:46

2 Answers 2

1

def get_table_id_and_gcp_path(source, database, table, dc, env, date_folder):
 try:
 table_id=f"{database}_stg.{table}"
 gcp_path = f"gs://{env}/{database}/{table}/"
 table_id = table_id + '_' + dc + '_stg' if dc != '-1' else table_id + '_stg'
 gcp_path = gcp_path + dc + "/dt=" + date_folder + "/*.parquet" if dc != '-1' else gcp_path + "dt=" + date_folder + "/*.parquet"
 except Exception as e:
 print(e)
 raise
 return table_id, gcp_path

or in one line


def get_table_id_and_gcp_path(source, database, table, dc, env, date_folder):
 try:
 table_id=f"{database}_stg.{table}"
 gcp_path = f"gs://{env}/{database}/{table}/"
 gcp_path,table_id = (gcp_path + dc + "/dt=" + date_folder + "/*.parquet" , table_id + '_' + dc + '_stg') if dc != '-1' else (gcp_path + "dt=" + date_folder + "/*.parquet",table_id + '_stg')
 except Exception as e:
 print(e)
 raise
 return table_id, gcp_path
answered Feb 8, 2022 at 22:08
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend adding parentheses around the conditional expressions to make the boundaries clear.
1

Answer to the first part

 if dc != '-1':
 sep = '_'
 else:
 sep = ''
 dc = '/'
 table_id = table_id + sep + dc + '_stg'
 gcp_path = gcp_path + dc + "dt=" + date_folder + "/*.parquet"

I don't know what type of exception you can raise. You could have anything from dc not being a string to gcp path no existing.

answered Feb 8, 2022 at 22:11

1 Comment

This looks like the right idea, just missing / in front of dt for gcp_path when dc != '-1'

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.