From 55baeb65691a503c3c08ee89a7e11704bb78d749 Mon Sep 17 00:00:00 2001 From: jgray-19 Date: Wed, 24 Jan 2024 23:17:09 +0000 Subject: [PATCH] Move deepcopy out of the lower level to the higher level --- src/pymadng/madp_classes.py | 12 ++++++++++++ src/pymadng/madp_pymad.py | 13 +------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/pymadng/madp_classes.py b/src/pymadng/madp_classes.py index 402f0bb..d66bb82 100644 --- a/src/pymadng/madp_classes.py +++ b/src/pymadng/madp_classes.py @@ -1,3 +1,4 @@ +import warnings # To warn the user when they try to deepcopy a mad_ref from typing import Iterable, Union, Any # To make stuff look nicer import numpy as np from .madp_pymad import mad_process, mad_ref, type_str, is_private @@ -94,6 +95,17 @@ def __dir__(self) -> Iterable[str]: """) return [x for x in self._mad.recv() if isinstance(x, str) and x[0] != "_"] + def __deepcopy__(self, memo): + val = self.eval() + if isinstance(val, list): + for i, v in enumerate(val): + if isinstance(v, mad_ref): + val[i] = v.__deepcopy__(memo) + elif isinstance(val, type(self)) and val._name == self._name: + warnings.warn("An attempt to deepcopy a mad_ref has been made, this is not supported and will result in a copy of the reference.") + return val + + class madhl_obj(madhl_ref): def __dir__(self) -> Iterable[str]: diff --git a/src/pymadng/madp_pymad.py b/src/pymadng/madp_pymad.py index 4b4015c..826c628 100644 --- a/src/pymadng/madp_pymad.py +++ b/src/pymadng/madp_pymad.py @@ -1,4 +1,4 @@ -import struct, os, subprocess, sys, select, warnings, io +import struct, os, subprocess, sys, select from typing import Union, Callable, Any import numpy as np @@ -178,17 +178,6 @@ def __getitem__(self, item: Union[str, int]): def eval(self): return self._mad.recv_vars(self._name) - def __deepcopy__(self, memo): - - val = self.eval() - if isinstance(val, list): - for i, v in enumerate(val): - if isinstance(v, mad_ref): - val[i] = v.__deepcopy__(memo) - elif isinstance(val, type(self)) and val._name == self._name: - warnings.warn("An attempt to deepcopy a mad_ref has been made, this is not supported and will result in a copy of the reference.") - return val - # data transfer -------------------------------------------------------------- # # Data ----------------------------------------------------------------------- #