Skip to content
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

Remove unnecessary try except #104

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Tjalling-dejong
Copy link
Collaborator

@Tjalling-dejong Tjalling-dejong commented Nov 19, 2024

Linked issue: #102

I started out by removing unnecessary try except statements in the test. These statements would except any error and then call pytest.fail with the message that the error is unexpected with the respective error message. Seems quite redundant to me.

I had to fix some tests after removing the try except statement and I ended up re-writing test_maskoutputfile.py and changing quite some code MaskOutputFile.py

@Tjalling-dejong Tjalling-dejong marked this pull request as ready for review November 19, 2024 15:55
class MaskOutputFile:
@staticmethod
def create_mask_point(coords: geojson.coords, properties: dict):
def create_mask_point(coords: geojson.coords, properties: Optional[dict] = None) -> geojson.Feature:
"""Creates a Point based on the properties and coordinates given.

Arguments:
coords {geojson.coords} --
Coordinates tuple (x,y) for the mask point.
properties {dict} -- Dictionary of properties

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
properties {dict} -- Dictionary of properties
properties {dict} -- Dictionary of properties
properties {dict, optional} -- Dictionary of properties. Defaults to None.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contributing page of the documentation suggets to use google style docstrings when writing new code. The docstrings throughout the code are not consistent at the moment. I'm working on a PR that will address this issue.

)

@staticmethod
def read_mask_output_file(file_path: str):
def read_mask_output_file(file_path: Union[str, Path]) -> dict:
"""Imports a GeoJson from a given json file path.

Arguments:
file_path {str} -- Location of the json file
Copy link

@tim-vd-aardweg tim-vd-aardweg Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
file_path {str} -- Location of the json file
file_path {str} -- Location of the json file
file_path {Union[str, Path]} -- Location of the json file


with file_path.open("r") as geojson_file:
geojson_data = geojson.load(geojson_file)
if not isinstance(geojson_data, geojson.FeatureCollection):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check actually needed? Can geojson_data be something else than a geojson.FeatureCollection?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, geojson will load a json as a dict. If the file is a geojson it will return a geojson.FeatureCollection

return geojson_data

@staticmethod
def write_mask_output_file(file_path: str, mask_points: list):
def write_mask_output_file(file_path: Union[Path, str], mask_points: list) -> None:
"""Writes a .geojson file with a Feature collection containing
the mask_points list given as input.

Arguments:
file_path {str} -- file_path where to store the geojson.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
file_path {str} -- file_path where to store the geojson.
file_path {str} -- file_path where to store the geojson.
file_path {Union[Path, str]} -- file_path where to store the geojson.

feature_collection = geojson.FeatureCollection(mask_points)
with open(file_path, "w") as f:
geojson.dump(feature_collection, f, indent=4)
file_path = Path(file_path)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should first check if the given file_path is not None. If it is, Path(file_path) may (or may not, depending on the python version) raise an error. So better to do something like:

        if not file_path:
            raise ValueError("file_path is required.")
        if not mask_points:
            raise ValueError("mask_points cannot be empty.")
        file_path = Path(file_path)

Copy link
Collaborator Author

@Tjalling-dejong Tjalling-dejong Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I added the check.

Copy link

@tim-vd-aardweg tim-vd-aardweg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Much better with all the try/except blocks gone from the tests! :)
I have added some minor suggestions for you to look at.

@Tjalling-dejong
Copy link
Collaborator Author

@tim-vd-aardweg Thanks again for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants