`copy.deepcopy(drawing)` fails

Issue #56 resolved
Toon Verstraelen created an issue

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)

  1. Toon Verstraelen 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.

  2. Toon Verstraelen 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 with d.fig = None.

  3. Toon Verstraelen 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?

  4. cdelker 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')
    

  5. Log in to comment