Skip to content
Snippets Groups Projects
Commit 662a47d3 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added DomainError

parent 67b3ee54
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ from __future__ import print_function, absolute_import, division ...@@ -4,6 +4,7 @@ from __future__ import print_function, absolute_import, division
__all__ = [ __all__ = [
'FirankaError', 'FirankaError',
'NotInDomainError', 'NotInDomainError',
'DomainError',
] ]
...@@ -13,7 +14,11 @@ class FirankaError(Exception): ...@@ -13,7 +14,11 @@ class FirankaError(Exception):
""" """
class NotInDomainError(FirankaError, ValueError): class DomainError(FirankaError):
"""Has something to do with the domain :)"""
class NotInDomainError(DomainError, ValueError):
""" """
Requested index is beyond this domain Requested index is beyond this domain
""" """
...@@ -117,27 +117,32 @@ class Interval(object): ...@@ -117,27 +117,32 @@ class Interval(object):
self.start, self.stop, self.left_inc, self.right_inc = args self.start, self.stop, self.left_inc, self.right_inc = args
def _contains_point(self, x):
if x == self.start:
return self.left_inc
if x == self.stop:
return self.right_inc
return self.start < x < self.stop
@_pre_range
def _contains_interval(self, x):
if ((x.start == self.start) and (x.left_inc ^ self.left_inc)) \
or ((x.stop == self.stop) and (x.right_inc ^ self.right_inc)):
return False
return (x.start >= self.start) and (x.stop <= self.stop)
def __contains__(self, x): def __contains__(self, x):
""" """
:type x: index or a Interval :type x: index or a Interval
""" """
if isinstance(x, six.string_types): if isinstance(x, six.string_types) or isinstance(x, Interval):
x = Interval(x) return self._contains_interval(x)
if isinstance(x, Interval):
if ((x.start == self.start) and (x.left_inc ^ self.left_inc)) \
or ((x.stop == self.stop) and (x.right_inc ^ self.right_inc)):
return False
return (x.start >= self.start) and (x.stop <= self.stop)
else: else:
if x == self.start: return self._contains_point(x)
return self.left_inc
if x == self.stop:
return self.right_inc
return self.start < x < self.stop
def is_empty(self): def is_empty(self):
return (self.start == self.stop) and not ( return (self.start == self.stop) and not (
...@@ -153,7 +158,7 @@ class Interval(object): ...@@ -153,7 +158,7 @@ class Interval(object):
def __getitem__(self, item): def __getitem__(self, item):
if not isinstance(item, slice): if not isinstance(item, slice):
raise ValueError('must be a slice') raise TypeError('must be a slice')
return self.intersection(Interval(item)) return self.intersection(Interval(item))
......
...@@ -5,7 +5,7 @@ import inspect ...@@ -5,7 +5,7 @@ import inspect
from sortedcontainers import SortedList from sortedcontainers import SortedList
from firanka.exceptions import NotInDomainError from firanka.exceptions import NotInDomainError, DomainError
from firanka.intervals import Interval, EMPTY_SET from firanka.intervals import Interval, EMPTY_SET
...@@ -127,8 +127,7 @@ class DiscreteSeries(Series): ...@@ -127,8 +127,7 @@ class DiscreteSeries(Series):
if len(data) > 0: if len(data) > 0:
if self.domain.start < data[0][0]: if self.domain.start < data[0][0]:
raise ValueError( raise DomainError('some domain space is not covered by definition!')
'some domain space is not covered by definition!')
def apply(self, fun): def apply(self, fun):
assert _has_arguments(fun, 2), 'fun must have at least 2 arguments' assert _has_arguments(fun, 2), 'fun must have at least 2 arguments'
......
...@@ -4,7 +4,7 @@ from __future__ import print_function, absolute_import, division ...@@ -4,7 +4,7 @@ from __future__ import print_function, absolute_import, division
import math import math
import unittest import unittest
from firanka.exceptions import NotInDomainError from firanka.exceptions import NotInDomainError, DomainError
from firanka.intervals import Interval from firanka.intervals import Interval
from firanka.series import DiscreteSeries, FunctionSeries, ModuloSeries, \ from firanka.series import DiscreteSeries, FunctionSeries, ModuloSeries, \
LinearInterpolationSeries, Series LinearInterpolationSeries, Series
...@@ -27,7 +27,7 @@ class TestDiscreteSeries(unittest.TestCase): ...@@ -27,7 +27,7 @@ class TestDiscreteSeries(unittest.TestCase):
a.join(b, lambda i, x, y: x + y) a.join(b, lambda i, x, y: x + y)
def test_uncov(self): def test_uncov(self):
self.assertRaises(ValueError, self.assertRaises(DomainError,
lambda: DiscreteSeries([[0, 0], [1, 1], [2, 2]], lambda: DiscreteSeries([[0, 0], [1, 1], [2, 2]],
'<-5;2>')) '<-5;2>'))
...@@ -124,7 +124,7 @@ class TestDiscreteSeries(unittest.TestCase): ...@@ -124,7 +124,7 @@ class TestDiscreteSeries(unittest.TestCase):
def test_discretize(self): def test_discretize(self):
# note the invalid data for covering this domain # note the invalid data for covering this domain
self.assertRaises(ValueError, lambda: FunctionSeries(lambda x: x ** 2, self.assertRaises(DomainError, lambda: FunctionSeries(lambda x: x ** 2,
'<-10;10)').discretize( '<-10;10)').discretize(
[0, 1, 2, 3, 4, 5], '(-1;6)')) [0, 1, 2, 3, 4, 5], '(-1;6)'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment