Custom deepcopy() implementations to help speed things up

This commit is contained in:
Jan Petykiewicz 2019-05-15 00:19:37 -07:00
parent 94410dffc9
commit a461446059
9 changed files with 80 additions and 8 deletions

View File

@ -70,6 +70,15 @@ class Pattern:
self.name = name
def __deepcopy__(self, memo: Dict = None) -> 'Pattern':
memo = {} if memo is None else memo
new = copy.copy(self)
new.name = self.name
new.shapes = copy.deepcopy(self.shapes, memo)
new.labels = copy.deepcopy(self.labels, memo)
new.subpatterns = copy.deepcopy(self.subpatterns, memo)
return new
def append(self, other_pattern: 'Pattern') -> 'Pattern':
"""
Appends all shapes, labels and subpatterns from other_pattern to self's shapes,

View File

@ -3,7 +3,7 @@
instances of a Pattern in the same parent Pattern.
"""
from typing import Union, List
from typing import Union, List, Dict
import copy
import numpy
@ -86,6 +86,16 @@ class GridRepetition:
mirrored = [False, False]
self.mirrored = mirrored
def __deepcopy__(self, memo: Dict = None) -> 'GridReptition':
memo = {} if memo is None else memo
new = copy.copy(self)
new.pattern = copy.deepcopy(self.pattern, memo)
new._offset = self._offset.copy()
new._mirrored = copy.deepcopy(self._mirrored, memo)
new._a_vector = self._a_vector.copy()
new._b_vector = copy.copy(self._b_vector) # ndarray or None so don't need deepcopy
return new
# offset property
@property
def offset(self) -> numpy.ndarray:

View File

@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Tuple, Dict
import math
import numpy
from numpy import pi
@ -159,6 +159,14 @@ class Arc(Shape):
self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen
def __deepcopy__(self, memo: Dict = None) -> 'Arc':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
new._radii = self._radii.copy()
new._angles = self._angles.copy()
return new
def to_polygons(self, poly_num_points: int=None, poly_max_arclen: float=None) -> List[Polygon]:
if poly_num_points is None:
poly_num_points = self.poly_num_points

View File

@ -1,4 +1,4 @@
from typing import List
from typing import List, Dict
import numpy
from numpy import pi
@ -53,6 +53,12 @@ class Circle(Shape):
self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen
def __deepcopy__(self, memo: Dict = None) -> 'Circle':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
return new
def to_polygons(self, poly_num_points: int=None, poly_max_arclen: float=None) -> List[Polygon]:
if poly_num_points is None:
poly_num_points = self.poly_num_points

View File

@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Tuple, Dict
import math
import numpy
from numpy import pi
@ -98,6 +98,13 @@ class Ellipse(Shape):
self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen
def __deepcopy__(self, memo: Dict = None) -> 'Ellipse':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
new._radii = self._radii.copy()
return new
def to_polygons(self,
poly_num_points: int=None,
poly_max_arclen: float=None

View File

@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Tuple, Dict
import copy
from enum import Enum
import numpy
@ -162,6 +162,15 @@ class Path(Shape):
self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Dict = None) -> 'Path':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
new._vertices = self._vertices.copy()
new._cap = copy.deepcopy(self._cap, memo)
new._cap_extensions = copy.deepcopy(self._cap_extensions, memo)
return new
@staticmethod
def travel(travel_pairs: Tuple[Tuple[float, float]],
width: float = 0.0,

View File

@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Tuple, Dict
import copy
import numpy
from numpy import pi
@ -84,6 +84,13 @@ class Polygon(Shape):
self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Dict = None) -> 'Polygon':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
new._vertices = self._vertices.copy()
return new
@staticmethod
def square(side_length: float,
rotation: float=0.0,

View File

@ -1,4 +1,5 @@
from typing import List, Tuple
from typing import List, Tuple, Dict
import copy
import numpy
from numpy import pi, inf
@ -81,6 +82,13 @@ class Text(Shape):
self.font_path = font_path
self.mirrored = mirrored
def __deepcopy__(self, memo: Dict = None) -> 'Text':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
new._mirrored = copy.deepcopy(self._mirrored, memo)
return new
def to_polygons(self,
_poly_num_points: int=None,
_poly_max_arclen: float=None

View File

@ -3,7 +3,7 @@
offset, rotation, scaling, and other such properties to the reference.
"""
from typing import Union, List
from typing import Union, List, Dict
import copy
import numpy
@ -45,6 +45,14 @@ class SubPattern:
mirrored = [False, False]
self.mirrored = mirrored
def __deepcopy__(self, memo: Dict = None) -> 'SubPattern':
memo = {} if memo is None else memo
new = copy.copy(self)
new.pattern = copy.deepcopy(self.pattern, memo)
new._offset = self._offset.copy()
new._mirrored = copy.deepcopy(self._mirrored, memo)
return new
# offset property
@property
def offset(self) -> numpy.ndarray: