I have some code and in it there are functions which can be run in one of two modes (in my case server mode and local mode). For example, most functions look something like this:
def path_join(path_elements,run_type='local'):
if run_type == 'local':
path = os.path.join(*path_elements).replace("/", os.path.sep)
elif run_type == 'blob':
path = clean_path('/'.join(path_elements))
return path
def clean_path(path: str, run_type='local') -> str:
"""
Replaces repeated path separators in a path.
"""
if run_type == 'local':
separator = get_os_path_sep()
elif run_type == 'blob':
separator = constants.BLOB_PATH_SEP
separator = separator * 2 if separator == constants.WINDOWS_PATH_SEP else separator
path = re.sub(fr"{separator}+", f"{separator}", path)
return path
def read_text(filename,run_type,container):
root = get_root_path()
if run_type == 'local':
with open(filename, 'r') as file:
return file.read()
elif run_type == 'blob':
stream = read_blob(container, blob_path, run_type, use_lease=True)
return stream.read().decode('utf-8')
I'm trying to refactor this code because it feels redundant to check for case in all these functions. One option I guess is to break down the functions and have the and create a class to decide which variation of the function to call. The problem is that all the functions don't have too much in common so I think in the end I would just have a lot of small classes which actually may be more complex (but maybe I'm wrong OOP is still fresh to me). Does anyone have any advice on how to make this better?