Skip to content

Commit

Permalink
Improved exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Busboom committed Mar 14, 2017
1 parent c52aeb6 commit 769de86
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion rowpipe/_meta.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.1.0'
__version__ = '0.1.1'
__author__ = 'eric@civicknowledge.com'
8 changes: 7 additions & 1 deletion rowpipe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ def __init__(self, type_target, field_header, value, message, *args, **kwargs):
# Call the base class constructor with the parameters it needs
Exception.__init__(self, textwrap.fill(message, 120), *args, **kwargs)


class TooManyCastingErrors(RowPipeError):
pass

def __init__(self, *args, errors = None, **kwargs):
self.errors = errors if errors is not None else {}
Exception.__init__(self, *args, **kwargs)



class SchemaError(RowPipeError):
pass
6 changes: 5 additions & 1 deletion rowpipe/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ def __init__(self, name, datatype=None, valuetype=None, transform=None):
self.valuetype = resolve_value_type(valuetype)
elif datatype is not None:
self.valuetype = resolve_value_type(datatype)
else:
self.valuetype = None

if self.valuetype is None:
raise SchemaError("Could not resovle type for datatype='{}' and valuetype='{}' ".format(datatype, valuetype))
raise SchemaError("Could not resovle type for for column '{}' datatype='{}' and valuetype='{}' "
.format(name, datatype, valuetype))



self.datatype = self.valuetype.python_type()
Expand Down
15 changes: 12 additions & 3 deletions rowpipe/valuetype/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def valuetype(func, *args, **kw):


def count_errors(errors):
if sum(len(e) for e in errors.values()) > 50:
raise TooManyCastingErrors("Too many casting errors")

# Count at most 10 errors for each column
c = sum(len(e) if len(e) < 10 else 10 for e in errors.values())

if c > 50:
raise TooManyCastingErrors("Too many casting errors ({})".format(c), errors=errors)


class TimeMixin(object):
Expand Down Expand Up @@ -203,7 +207,9 @@ def __getattr__(self, item):


def cast_int(v, header_d, errors):
from rowpipe.valuetype import IntMeasure
if isinstance(v, FailedValue):

errors[header_d].add(u"Failed to cast '{}' ({}) to int in '{}': {}".format(v, type(v), header_d, v.exc))
count_errors(errors)
return None
Expand Down Expand Up @@ -522,7 +528,10 @@ def robust_int(v):
if not v:
return None

return int(v)
try:
return int(v)
except ValueError:
return int(float(v))


def print_value(row_n, header_d, v):
Expand Down

0 comments on commit 769de86

Please sign in to comment.