Skip to content

Commit

Permalink
feat(point): Added automatic casting to float for coordinate values
Browse files Browse the repository at this point in the history
As noted in other discussions, I realized that this did not significantly affect the performance of initializing objects with a lot of points.  So we might as well implement this and avoid having to check it elsewhere.
  • Loading branch information
chriswmackey authored and Chris Mackey committed Jul 16, 2019
1 parent ad72845 commit 7e4267b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 11 additions & 2 deletions ladybug_geometry/geometry2d/pointvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Vector2D(object):

def __init__(self, x=0, y=0):
"""Initialize 2D Vector."""
self._x = x
self._y = y
self._x = self._cast_to_float(x)
self._y = self._cast_to_float(y)

@classmethod
def from_dict(cls, data):
Expand Down Expand Up @@ -130,6 +130,15 @@ def to_dict(self):
return {'x': self.x,
'y': self.y}

def _cast_to_float(self, value):
"""Ensure that an input coordinate value is a float."""
try:
number = float(value)
except (ValueError, TypeError):
raise TypeError(
'Coordinates must be numbers. Got {}: {}.'.format(type(value), value))
return number

@staticmethod
def _rotate(vec, angle):
"""Hidden rotation method used by both Point2D and Vector2D."""
Expand Down
15 changes: 12 additions & 3 deletions ladybug_geometry/geometry3d/pointvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Vector3D(object):

def __init__(self, x=0, y=0, z=0):
"""Initialize 3D Vector."""
self._x = x
self._y = y
self._z = z
self._x = self._cast_to_float(x)
self._y = self._cast_to_float(y)
self._z = self._cast_to_float(z)

@classmethod
def from_dict(cls, data):
Expand Down Expand Up @@ -135,6 +135,15 @@ def to_dict(self):
'y': self.y,
'z': self.z}

def _cast_to_float(self, value):
"""Ensure that an input coordinate value is a float."""
try:
number = float(value)
except (ValueError, TypeError):
raise TypeError(
'Coordinates must be numbers. Got {}: {}.'.format(type(value), value))
return number

@staticmethod
def _reflect(vec, normal):
"""Hidden reflection method used by both Point3D and Vector3D."""
Expand Down

0 comments on commit 7e4267b

Please sign in to comment.