Skip to content

Commit

Permalink
Smarter error handling for nulls in domain. #77
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Nov 15, 2016
1 parent fad6a80 commit 4929eae
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
13 changes: 10 additions & 3 deletions leather/scales/ordinal.py
Expand Up @@ -28,7 +28,11 @@ def project(self, value, range_min, range_max):

segments = len(self._domain)
segment_size = (range_max - range_min) / segments
pos = range_min + (self._domain.index(value) * segment_size) + (segment_size / 2)

try:
pos = range_min + (self._domain.index(value) * segment_size) + (segment_size / 2)
except ValueError:
raise ValueError('Value "%s" is not present in Ordinal scale domain' % value)

return pos

Expand All @@ -44,8 +48,11 @@ def project_interval(self, value, range_min, range_max):
segment_size = (range_max - range_min) / segments
gap = segment_size / Decimal(20)

a = range_min + (self._domain.index(value) * segment_size) + gap
b = range_min + ((self._domain.index(value) + 1) * segment_size) - gap
try:
a = range_min + (self._domain.index(value) * segment_size) + gap
b = range_min + ((self._domain.index(value) + 1) * segment_size) - gap
except ValueError:
raise ValueError('Value "%s" is not present in Ordinal scale domain' % value)

return (a, b)

Expand Down
12 changes: 7 additions & 5 deletions leather/series/base.py
Expand Up @@ -6,7 +6,7 @@
import six

from leather.data_types import DataType
from leather.utils import X, Y, Datum
from leather.utils import DIMENSION_NAMES, X, Y, Datum


class Series(object):
Expand Down Expand Up @@ -51,8 +51,8 @@ def __init__(self, data, x=None, y=None, name=None):
]

self._types = [
self._infer_type(self._keys[X]),
self._infer_type(self._keys[Y])
self._infer_type(X),
self._infer_type(Y)
]

def _make_key(self, key):
Expand All @@ -64,18 +64,20 @@ def _make_key(self, key):
else:
return lambda row, index: row[key]

def _infer_type(self, key):
def _infer_type(self, dimension):
"""
Infer the datatype of this column by sampling the data.
"""
key = self._keys[dimension]

for i, row in enumerate(self._data):
v = key(row, i)

if v is not None:
break

if v is None:
raise ValueError('All values in dimension were null.')
raise ValueError('All values in %s dimension are null.' % DIMENSION_NAMES[dimension])

return DataType.infer(v)

Expand Down
6 changes: 3 additions & 3 deletions leather/series/category.py
Expand Up @@ -56,9 +56,9 @@ def __init__(self, data, x=None, y=None, z=None, name=None):
]

self._types = [
self._infer_type(self._keys[X]),
self._infer_type(self._keys[Y]),
self._infer_type(self._keys[Z])
self._infer_type(X),
self._infer_type(Y),
self._infer_type(Z)
]

def data(self):
Expand Down
3 changes: 1 addition & 2 deletions test.py
Expand Up @@ -22,8 +22,7 @@

data1 = [
(2, None),
(6, None),
(9, None)
(3, None)
]

chart = leather.Chart()
Expand Down

0 comments on commit 4929eae

Please sign in to comment.