Skip to content

Commit

Permalink
Implement __slots__
Browse files Browse the repository at this point in the history
Add __slots__ declarations and move instance variables into __init__
  • Loading branch information
Luke Pflibsen-Jones committed Jun 28, 2021
1 parent 7ca3341 commit 628bfa6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
15 changes: 11 additions & 4 deletions chturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ def declination(self):
class CHTurtle(object):
"""3D turtle implementation for use in both L-Systems and Parametric tree
generation schemes"""
dir = Vector([0.0, 0.0, 1.0])
pos = Vector([0.0, 0.0, 0.0])
right = Vector([1.0, 0.0, 0.0])
width = 0.0

__slots__ = (
'dir', 'pos', 'right', 'width'
)

def __init__(self, other=None):
"""Copy Constructor"""

if other is not None:
self.dir = other.dir.copy()
self.pos = other.pos.copy()
self.right = other.right.copy()
self.width = other.width

else:
self.dir = Vector([0.0, 0.0, 1.0])
self.pos = Vector([0.0, 0.0, 0.0])
self.right = Vector([1.0, 0.0, 0.0])
self.width = 0.0

def __str__(self):
return 'Turtle at %s, direction %s, right %s' % (self.pos, self.dir, self.right)

Expand Down
7 changes: 4 additions & 3 deletions leaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

class Leaf(object):
"""Class to store data for each leaf in the system"""
position = None
direction = None
right = None

__slots__ = (
'position', 'direction', 'right'
)

def __init__(self, pos, direction, right):
"""Init method for leaf with position, direction and relative x axis"""
Expand Down
41 changes: 24 additions & 17 deletions parametric/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,26 @@ class BranchMode(Enum):
class Stem(object):
"""Class to store data for each stem (branch) in the system, primarily to
be accessed by its children in calculating their own parameters"""
depth = 0
children = []
parent = None
curve = None
length = 0
offset = 0
radius = 0
length_child_max = 0
radius_limit = 0

__slots__ = (
'depth', 'curve', 'parent', 'offset', 'radius_limit', 'children', 'length', 'radius',
'length_child_max'
)

def __init__(self, depth, curve, parent=None, offset=0, radius_limit=-1):
"""Init with at depth with curve, possibly parent and offset (for depth > 0)"""

self.depth = depth
self.curve = curve
self.parent = parent
self.offset = offset
self.radius_limit = radius_limit

self.children = []
self.length = 0
self.radius = 0
self.length_child_max = 0

def copy(self):
"""Copy method for stems"""
new_stem = Stem(self.depth, self.curve, self.parent, self.offset, self.radius_limit)
Expand All @@ -148,21 +150,26 @@ def __str__(self):

class Tree(object):
"""Class to store data for the tree"""
tree_scale = 0
param = None
leaves_array = None
branches_curve = None
base_length = 0
split_num_error = [0, 0, 0, 0, 0, 0, 0]
tree_obj = None
trunk_length = 0

__slots__ = (
'param', 'generate_leaves', 'leaves_array', 'stem_index', 'tree_scale', 'branches_curve',
'base_length', 'split_num_error', 'tree_obj', 'trunk_length'
)

def __init__(self, param, generate_leaves=True):
"""initialize tree with specified parameters"""

self.param = param
self.generate_leaves = generate_leaves
self.leaves_array = []

self.stem_index = 0
self.tree_scale = 0
self.branches_curve = None
self.base_length = 0
self.split_num_error = [0, 0, 0, 0, 0, 0, 0]
self.tree_obj = None
self.trunk_length = 0

# Disable leaf generation
if not generate_leaves:
Expand Down

0 comments on commit 628bfa6

Please sign in to comment.