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

Add a copying function to reduce boilerplate code #168

Open
ph-kev opened this issue Nov 15, 2024 · 1 comment · May be fixed by #186
Open

Add a copying function to reduce boilerplate code #168

ph-kev opened this issue Nov 15, 2024 · 1 comment · May be fixed by #186
Assignees
Labels
good first issue Good for newcomers

Comments

@ph-kev
Copy link
Member

ph-kev commented Nov 15, 2024

Currently, there is a lot of function that manipulate one thing of the OutputVar (like dims or data) and remake the OutputVar with that one thing changed. Because we cannot mutate the OutputVar (since otherwise the interpolant will not change), there is boilerplate code that which involves doing copy or deepcopy of the object to remake the object. Similarly, a lot of the tests involving constructing OutputVar with a single change.

To reduce the amount of boilerplate code, a copy function can be made that takes in var::OutputVar and the keyword arguments attributes, dims, dim_attributes, and data. If no keyword arguments are supplied, then the function return a deepcopy of the OutputVar. If a keyword argument is supplied, then it makes a copy of that object, but with whatever is supplied for the keyword. As such, instead of writing

...
ret_attribs = deepcopy(var.attributes)
ret_attribs["start_date"] = string(start_date)
ret_dims = deepcopy(var.dims)
ret_dims["time"] = time_arr
ret_dim_attributes = deepcopy(var.dim_attributes)
ret_data = copy(var.data)
return OutputVar(ret_attribs, ret_dims, ret_dim_attributes, ret_data)

you can write instead

...
ret_attribs = deepcopy(var.attributes)
ret_attribs["start_date"] = string(start_date)
ret_dims = deepcopy(var.dims)
ret_dims["time"] = time_arr
return copy_var(var; attributes = ret_attribs, dims = ret_dims)
@ph-kev ph-kev added the good first issue Good for newcomers label Nov 15, 2024
@ph-kev
Copy link
Member Author

ph-kev commented Nov 17, 2024

A good name for the function could be rebuild or remake.

@ph-kev ph-kev self-assigned this Dec 13, 2024
@ph-kev ph-kev linked a pull request Dec 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant