`copy.deepcopy(drawing)` fails
Example:
import schemdraw
import schemdraw.elements as elm
import copy
d = schemdraw.Drawing()
d.add(elm.Resistor())
copy.deepcopy(d)
This raises an exception: RecursionError: maximum recursion depth exceeded
.
This for example useful when starting from an initial schematic and algorithmically adding current labels to it. In such cases, it is useful to keep also the original without labels, which is something that could be achieved by adding the labels only to a copy of the drawing.
The problem is most likely related to the Element
class. Also the following raises an exception, continued from the first:
copy.copy(d.elements[0])
Would there be an easy workaround for this? Pickling fails for the same reason, e.g. try
import pickle
pickle.loads(pickle.dumps(d))
Comments (8)
-
repo owner -
reporter Thanks for the quick fix! I’ve tested locally and can confirm the example now also works for me. I’ll do some more elaborate testing soon.
-
reporter In the fixed version, I found a way to produce a similar exception, with a slightly different example.
import schemdraw import schemdraw.elements as elm import copy d = schemdraw.Drawing() d.add(elm.Resistor()) d.draw() # <-- This line is new. copy.deepcopy(d)
This raises an exception
ValueError: 'Spines' object does not contain a 'deepcopy' spine
, which is related to matplotlib. A workaround is to delete the figure before making he copy withd.fig = None
. -
repo owner - changed status to resolved
-
reporter I just tried it with 0.12 and could still reproduce the issue with the Spines. Would it help to open a new issue for the problem with the spines?
-
repo owner That issue should be filed with the Matplotlib team. It can be reproduced, without Schemdraw, with:
import matplotlib.pyplot as plt import copy fig, ax = plt.subplots() copy.deepcopy(fig)
Alternatively, you can switch Schemdraw to draw directly to SVG, which works around this problem:
schemdraw.use('svg')
-
reporter Thanks for point this out. I’ll open an issue for matplotlib.
-
reporter - Log in to comment
Should be fixed in
dc6fd61
now.