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

More convenient updated copy of nested fields #1590

Closed
tylerflex opened this issue Apr 9, 2024 · 1 comment · Fixed by #1594
Closed

More convenient updated copy of nested fields #1590

tylerflex opened this issue Apr 9, 2024 · 1 comment · Fixed by #1594

Comments

@tylerflex
Copy link
Collaborator

tylerflex commented Apr 9, 2024

I think a major disadvantage of our immutability choice is that it is quite tedious to update fields deeply nested within components. For example, say I want to update the size in z of a box within the Simulation.structures. The current API requires something like

new_box = simulation.structures[i].geometry.updated_copy(size=new_size)
new_structure = simulation.structures[i].updated_copy(geometry=geometry)
new_structures = list(simulation.structures)
new_structures[i] = new_structure
new_simulation = simulation.updated_copy(structures=new_structures)

what if instead we provided a short cut, such as

new_simulation = simulation.updated_copy(size=new_size, path=f"structures/{i}/geometry")

that handled this for us?

the code would look something vaguely like

def updated_copy(self, path: str = None, **kwargs):
    if not path:
        return self.updated_copy_original(**kwargs)

    path_components = path.split("/")

    field_name = path_components[0]

    # special handling for if field name is an integer index into a tuple

    sub_component = self.getattr(field_name)
    sub_path = "/".join(path_components[1:])

    new_component = sub_component.updated_copy(path=sub_path, **kwargs)
    return self.updated_copy_original(field_name=new_component)

any comments / tools that can do this for us / concerns?

@tylerflex tylerflex linked a pull request Apr 10, 2024 that will close this issue
@momchil-flex
Copy link
Collaborator

Done in #1594

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 a pull request may close this issue.

2 participants