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

make Metadata a dataclass #109

Closed
wants to merge 2 commits into from
Closed

make Metadata a dataclass #109

wants to merge 2 commits into from

Conversation

edsaac
Copy link
Contributor

@edsaac edsaac commented Jul 26, 2023

Currently, the Metadata class has no __init__ function but only class variables.

class Metadata:
    """Custom class for metadata.
    """
    url = None
    query_time = None
    site_info = None
    header = None
    variable_info = None
    comment = None

    # note sure what statistic_info is
    statistic_info = None
    # disclaimer seems to be only part of importWaterML1
    disclaimer = None

I think these are not meant to be class variables but instance variables, as different data requests will return different metadata. Check an example of the difference in https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables. Only utils.set_metadata creates Metadata objects and assigns values to the instance object variables, without ever modifying the default None of the class variables. nwis._set_metadata behaves similarly in that it only modify objects but never the class itself.

Making the Metadata a dataclass helps with making those (url, site_info, etc) instance attributes, without the need to write a long __init__ function. Dataclasses have been part of the standard library since 3.7 so there should not be compatibility issues.


  • All tests passing from the test folder using pytest.

Copy link
Contributor

@elbeejay elbeejay left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution and detailed explanation @edsaac - I agree that as implemented the Metadata class does not follow best practices.

This relates to #75, and is a good solution I think. Will let @thodson-usgs weigh in too as he opened #75 and likely has some thoughts on the Metadata class.

Comment on lines 237 to 239

if __name__ == "__main__":
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this at the end of the script? We don't have any handling for direct calls of any other other .py files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, that idiom is not really necessary unless the file was run as a script.

@elbeejay elbeejay requested a review from thodson-usgs August 14, 2023 16:30
Copy link
Contributor

@elbeejay elbeejay left a comment

Choose a reason for hiding this comment

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

This works for me but @thodson-usgs it'd be great to get your approval too before we merge.

@thodson-usgs
Copy link
Collaborator

I agree that existing implementation was poor. That said, I'm not sure changing to a dataclass really fixes anything.

It may be better to use an init function here to abstract how the Metadata is initialized. Something like Metadata(response) seems ideal, but I haven't thought through all the implications yet

@edsaac
Copy link
Contributor Author

edsaac commented Aug 17, 2023

I agree, making it a dataclass just removes those class variables that are initialized to None and never used. But it does not bring any big improvement to the rest of the code. Constructing the object with Metadata(response) makes much more sense, so I will close this PR and give it a try to that idea.

@edsaac edsaac closed this Aug 17, 2023
@thodson-usgs
Copy link
Collaborator

Thanks @edsaac,
I'm a preoccupied with another deadline, but if you're interested, you could proceed like this:
Submit a PR that implements a basic Metadata class, then you and I can work together on that PR to create children classes for the different services.

@edsaac edsaac mentioned this pull request Aug 22, 2023
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.

3 participants