Skip to content

Commit

Permalink
Move deepcopy out of the lower level to the higher level
Browse files Browse the repository at this point in the history
  • Loading branch information
jgray-19 committed Jan 24, 2024
1 parent b30811f commit 55baeb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/pymadng/madp_classes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]:
Expand Down
13 changes: 1 addition & 12 deletions src/pymadng/madp_pymad.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 ----------------------------------------------------------------------- #
Expand Down

0 comments on commit 55baeb6

Please sign in to comment.