freegsnke.copying module

Useful functions for copying attributes between FreeGSNKE objects.

Copyright 2025 UKAEA, UKRI-STFC, and The Authors, as per the COPYRIGHT and README files.

This file is part of FreeGSNKE.

FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.

freegsnke.copying.copy_into(obj: object, new_obj: object, attr: str, *, mutable: bool = False, strict: bool = True, allow_deepcopy: bool = False) → None[source]

Copy an attribute from one object into another.

This function transfers a named attribute from obj to new_obj, optionally performing a deep copy for mutable objects.

Parameters:
  • obj (object) – Source object to copy the attribute from.

  • new_obj (object) – Destination object to copy the attribute into.

  • attr (str) – Name of the attribute to copy (equivalent to obj.attr).

  • mutable (bool, optional) – If True, treat the attribute as mutable and ensure it is copied rather than referenced. For NumPy arrays, a shallow copy is used when possible.

  • strict (bool, optional) – If True, raise an AttributeError (from getattr) when obj does not have attr. If False, silently return without copying.

  • allow_deepcopy (bool, optional) – If True, allow deepcopying of non-NumPy mutable objects when required. If False, raise a TypeError when deepcopy would be needed.

Raises:

TypeError – If mutable=True, the attribute is not a safe NumPy array copy, and allow_deepcopy=False.