Skip to content

Commit

Permalink
extract deep copy utility func
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Sep 24, 2021
1 parent 9cc758e commit de2b3b8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyphare/pyphare/core/gridlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import numpy as np

from .phare_utilities import is_scalar, listify, np_array_ify
from .phare_utilities import is_scalar, listify
from .box import Box

directions = ["x", "y", "z"]
Expand Down
16 changes: 16 additions & 0 deletions pyphare/pyphare/core/phare_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ def print_trace():
import sys, traceback
_, _, tb = sys.exc_info()
traceback.print_tb(tb)


def deep_copy(item, memo, excludes=[]):
from copy import deepcopy

clazz = item.__class__
that = clazz.__new__(clazz)
memo[id(item)] = that
for key, value in item.__dict__.items():
# some objects are not picklable so cannot be deepcopied eg. h5py datasets
if key in excludes:
setattr(that, key, value)
else:
setattr(that, key, deepcopy(value, memo))
return that

16 changes: 4 additions & 12 deletions pyphare/pyphare/pharesee/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import os
import numpy as np
from copy import deepcopy

from .particles import Particles

from ..core import box as boxm
from ..core.box import Box
from ..core.gridlayout import GridLayout
import matplotlib.pyplot as plt
from ..core.phare_utilities import refinement_ratio
from ..core.phare_utilities import refinement_ratio, deep_copy



Expand All @@ -33,17 +32,8 @@ def __init__(self, layout, quantity):


def __deepcopy__(self, memo):
# some objects are not picklable eg. h5py datasets
no_copy_keys = ["dataset"] # do not copy these things
clazz = self.__class__
that = clazz.__new__(clazz)
memo[id(self)] = that
for key, value in self.__dict__.items():
if key in no_copy_keys:
setattr(that, key, value)
else:
setattr(that, key, deepcopy(value, memo))
return that
return deep_copy(self, memo, no_copy_keys)



Expand Down Expand Up @@ -214,6 +204,8 @@ def __repr__(self):


def copy(self):
"""does not copy patchdatas.datasets (see class PatchData)"""
from copy import deepcopy
return deepcopy(self)


Expand Down

0 comments on commit de2b3b8

Please sign in to comment.