Skip to content

Add copy() method to ThermalNetwork class #161

@jnettels

Description

@jnettels

I had cases where I wanted to copy a dhnx.network.ThermalNetwork() object.

I would be able to call new_network = copy.copy(network) on a network object. But then the DataFrames inside the network.components dictionary would not actually be copied. Instead both instances of the dict would point to the same DataFrames. (This is called a shallow copy, I think?)

This is bad if we want to manipulate the DataFrames but keep a backup of the whole network to restore later.
I solved this by writing a custom copy method:

def copy_dhnx_network(network):
    """Create a 'useful' copy of the ThermalNetwork object."""
    new_network = dhnx.network.ThermalNetwork()
    new_network.components = dhnx.helpers.Dict({
        key: df.copy() for key, df in network.components.items()
    })
    new_network.sequences = network.sequences.copy()
    new_network.results = network.results.copy()
    if network.timeindex is not None:
        new_network.timeindex = network.timeindex.copy()
    if network.graph is not None:
        new_network.graph = network.graph.copy()
    return new_network

It works for me, so I would like to propose it as a proper copy method implemented in the code. But I am not sure if that is the right way, and if that would have to be called copy(), __copy__(), or if it would actually be a deepcopy()?

Any suggestions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions