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 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': def append(self, other_pattern: 'Pattern') -> 'Pattern':
""" """
Appends all shapes, labels and subpatterns from other_pattern to self's shapes, 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. instances of a Pattern in the same parent Pattern.
""" """
from typing import Union, List from typing import Union, List, Dict
import copy import copy
import numpy import numpy
@ -86,6 +86,16 @@ class GridRepetition:
mirrored = [False, False] mirrored = [False, False]
self.mirrored = mirrored 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 # offset property
@property @property
def offset(self) -> numpy.ndarray: 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 math
import numpy import numpy
from numpy import pi from numpy import pi
@ -159,6 +159,14 @@ class Arc(Shape):
self.poly_num_points = poly_num_points self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen 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]: def to_polygons(self, poly_num_points: int=None, poly_max_arclen: float=None) -> List[Polygon]:
if poly_num_points is None: if poly_num_points is None:
poly_num_points = self.poly_num_points poly_num_points = self.poly_num_points

View File

@ -1,4 +1,4 @@
from typing import List from typing import List, Dict
import numpy import numpy
from numpy import pi from numpy import pi
@ -53,6 +53,12 @@ class Circle(Shape):
self.poly_num_points = poly_num_points self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen 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]: def to_polygons(self, poly_num_points: int=None, poly_max_arclen: float=None) -> List[Polygon]:
if poly_num_points is None: if poly_num_points is None:
poly_num_points = self.poly_num_points 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 math
import numpy import numpy
from numpy import pi from numpy import pi
@ -98,6 +98,13 @@ class Ellipse(Shape):
self.poly_num_points = poly_num_points self.poly_num_points = poly_num_points
self.poly_max_arclen = poly_max_arclen 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, def to_polygons(self,
poly_num_points: int=None, poly_num_points: int=None,
poly_max_arclen: float=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 import copy
from enum import Enum from enum import Enum
import numpy import numpy
@ -162,6 +162,15 @@ class Path(Shape):
self.rotate(rotation) self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do] [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 @staticmethod
def travel(travel_pairs: Tuple[Tuple[float, float]], def travel(travel_pairs: Tuple[Tuple[float, float]],
width: float = 0.0, width: float = 0.0,

View File

@ -1,4 +1,4 @@
from typing import List, Tuple from typing import List, Tuple, Dict
import copy import copy
import numpy import numpy
from numpy import pi from numpy import pi
@ -84,6 +84,13 @@ class Polygon(Shape):
self.rotate(rotation) self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do] [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 @staticmethod
def square(side_length: float, def square(side_length: float,
rotation: float=0.0, 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 import numpy
from numpy import pi, inf from numpy import pi, inf
@ -81,6 +82,13 @@ class Text(Shape):
self.font_path = font_path self.font_path = font_path
self.mirrored = mirrored 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, def to_polygons(self,
_poly_num_points: int=None, _poly_num_points: int=None,
_poly_max_arclen: float=None _poly_max_arclen: float=None

View File

@ -3,7 +3,7 @@
offset, rotation, scaling, and other such properties to the reference. offset, rotation, scaling, and other such properties to the reference.
""" """
from typing import Union, List from typing import Union, List, Dict
import copy import copy
import numpy import numpy
@ -45,6 +45,14 @@ class SubPattern:
mirrored = [False, False] mirrored = [False, False]
self.mirrored = mirrored 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 # offset property
@property @property
def offset(self) -> numpy.ndarray: def offset(self) -> numpy.ndarray: