Given a method that is widely used and has a void return type:
from somepackage import download_model
from somepackage import get_filename
def download(name, download_path):
response = download_model(name, download_path)
file_name = response.filename
file_path = model_download_folder + file_name
if file_name.endswith(".tar.gz"):
with tarfile.open(file_path, "r:gz") as tar_file:
tar_file.extractall(path=download_path)
This method downloads a folder or a tar file. Then, if the downloaded file is a tar, it extracts it, and the suffix had the file name. For example, if download is called with download_path=/some/path/ and response.filename is 'model.tar.gz' the actual content of the tar will be in /some/path/model/.
This method is currently hiding the filename in the response. So far, the filename was hard-coded and fixed for all downloads. Recently new models was added and now the filename might be anything. The caller to download in this case, should be able to get the path where the files were extracted (e.g. /some/path/model/).
Alternatives:
- Change
downloadto return theresponse/filename - Store the response in a global field and then add another method to get it
- Call to
get_filename(which wasn't used so far) to get thefilename
Assume that backwards compatibility is a constraint. Which one of these options would you recommend and why.
responseallows for), and given that the filename is already extracted inresponse.filename, isn't DRY. Also, it doesn't conceptually correspond to what the code is doing at this level (it's not trying to extract a filename from some path, but to do something related to this particular download).