-
Notifications
You must be signed in to change notification settings - Fork 31
Add more file types to file_exists
for checking '_SUCCESS'
#486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ist of file paths.
gnomad/utils/file_utils.py
Outdated
Check whether all files in the list exist and raise an exception if any of them exist and overwrite is False. | ||
:param fname_list: List of file paths to check the existence of. | ||
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources exist. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources exist. | |
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources doesn't exist. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above
gnomad/utils/file_utils.py
Outdated
:param fname: File name | ||
:return: Whether the file exists | ||
:param fname: File name. | ||
:param overwrite: Optional boolean that will raise an error if set to False and file exists. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:param overwrite: Optional boolean that will raise an error if set to False and file exists. | |
:param overwrite: Optional boolean that will raise an error if set to False and file doesn't exist. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually in this case I want to raise the error if it does exist. Use case is when the file exists and we are trying to overwrite it, but we forgot to specify overwrite=True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I just want to check if any file in the list doesn't exist and which are they? Setting overwrite
to False to figure out make thing complicated. This question may be asked more, because the function doesn't overwrite anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function isn't for a list, it's just for a single file. By default the overwrite
parameter is None and will not be used at all, this will then raise no error, it will simply do what it did before and return True or False. I will change to make another function that calls this one and has a clearer name that includes overwrite, I was just trying to put it into a single function. For the other function you again don't need to pass in overwrite.
) -> bool: | ||
""" | ||
Check whether all files in the list exist and raise an exception if any of them exist and overwrite is False. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that the overwrite
parameter needs use case explanation, since overwrite is kind of out of the functions scope (just to check if file in list exists) and it causes some confusion. Maybe just me, so it's up to you :)
The function will optionally raise an error if `overwrite` is set to False and any file exists, when the the function is called to check if the new created file paths exist. |
file_exists
for checking '_SUCCESS' and add an 'overwrite' option to throw and exception if set to False and file exists..file_exists
for checking '_SUCCESS'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add some suggestions so that the functions can be used in other cases as well. It's up to you if you want to change it :)
gnomad/utils/file_utils.py
Outdated
|
||
def check_file_exists_no_overwrite( | ||
fname: Union[str, List[str]], | ||
overwrite: bool = True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are good reasons to raise errors when file exists or not, maybe it's good to let the user choose when to raise error. For example, raise_error
can be "exist" or "not exist".
overwrite: bool = True, | |
def file_exists(fname: str, raise_error: Optional[bool] ="exist") -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried adding in something with more flexibility, let me know if that works
gnomad/utils/file_utils.py
Outdated
for f in fname: | ||
exists = file_exists(f) | ||
all_exist &= exists | ||
if exists and overwrite is False: | ||
data_exceptions.append(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for f in fname: | |
exists = file_exists(f) | |
all_exist &= exists | |
if exists and overwrite is False: | |
data_exceptions.append(f) | |
for f in fname: | |
exists = file_exists(f) | |
all_exist &= exists | |
if exists and raise_error == "exist": | |
data_exceptions.append(f) | |
elif not exist and raise_error == "not exist": | |
data_exceptions.append(f) |
gnomad/utils/file_utils.py
Outdated
if data_exceptions: | ||
raise DataException( | ||
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if data_exceptions: | |
raise DataException( | |
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}" | |
) | |
if data_exceptions: | |
if raise_error == "exist": | |
raise DataException( | |
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}" | |
) | |
elif raise_error == "not exist": | |
raise DataException( | |
f"The following files doesn't exist! {','.join(data_exceptions)}" | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one single comment!
Add
check_file_exists_raise_error
to usefile_exists
to check the existence of all files in a list of file paths with options to raise an error.