From f2105b140fb2b33f2ad9c7ce272861d336296e11 Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 9 Feb 2014 22:43:26 +0200 Subject: [PATCH 1/6] Add tests for initialization with parentheses and square brackets --- tests/test_initialization.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_initialization.py b/tests/test_initialization.py index 6840bb8..f5ce480 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -88,6 +88,12 @@ def test_supports_integers(self): assert interval.lower_inc assert interval.upper_inc + def test_uses_two_numbers_with_parentheses_as_open_interval(self): + assert IntInterval(1, 2) == IntInterval((1, 2)) + + def test_uses_two_numbers_with_closed_brackets_as_closed_interval(self): + assert IntInterval[1, 2] == IntInterval([1, 2]) + @mark.parametrize('number_range', ( (3, 2), From 9b49e62ab257d2dc043be1e07e29949db4f96c96 Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 9 Feb 2014 23:08:51 +0200 Subject: [PATCH 2/6] Add support for parentheses as open intervals --- intervals/__init__.py | 6 ++++++ tests/test_initialization.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/intervals/__init__.py b/intervals/__init__.py index 2cc88b6..42f365c 100644 --- a/intervals/__init__.py +++ b/intervals/__init__.py @@ -151,6 +151,12 @@ def __init__( 30 """ + + if type(bounds) == type(lower_inc) == int and not upper_inc: + bounds = (bounds, lower_inc) + lower_inc = None + upper_inc = None + self.lower, self.upper, self.lower_inc, self.upper_inc = ( self.parser(bounds, lower_inc, upper_inc) ) diff --git a/tests/test_initialization.py b/tests/test_initialization.py index f5ce480..eaac817 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -91,7 +91,7 @@ def test_supports_integers(self): def test_uses_two_numbers_with_parentheses_as_open_interval(self): assert IntInterval(1, 2) == IntInterval((1, 2)) - def test_uses_two_numbers_with_closed_brackets_as_closed_interval(self): + def test_uses_two_numbers_with_square_brackets_as_closed_interval(self): assert IntInterval[1, 2] == IntInterval([1, 2]) @mark.parametrize('number_range', From 524dfe12575d50486942cd024357d9a88f31198d Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 9 Feb 2014 23:08:51 +0200 Subject: [PATCH 3/6] Add support for parentheses as open intervals --- intervals/__init__.py | 6 ++++++ tests/test_initialization.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/intervals/__init__.py b/intervals/__init__.py index 2cc88b6..42f365c 100644 --- a/intervals/__init__.py +++ b/intervals/__init__.py @@ -151,6 +151,12 @@ def __init__( 30 """ + + if type(bounds) == type(lower_inc) == int and not upper_inc: + bounds = (bounds, lower_inc) + lower_inc = None + upper_inc = None + self.lower, self.upper, self.lower_inc, self.upper_inc = ( self.parser(bounds, lower_inc, upper_inc) ) diff --git a/tests/test_initialization.py b/tests/test_initialization.py index f5ce480..eaac817 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -91,7 +91,7 @@ def test_supports_integers(self): def test_uses_two_numbers_with_parentheses_as_open_interval(self): assert IntInterval(1, 2) == IntInterval((1, 2)) - def test_uses_two_numbers_with_closed_brackets_as_closed_interval(self): + def test_uses_two_numbers_with_square_brackets_as_closed_interval(self): assert IntInterval[1, 2] == IntInterval([1, 2]) @mark.parametrize('number_range', From 35842e4f6d60a33257ed81a30ee20848a5e1e5c4 Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 9 Feb 2014 23:17:12 +0200 Subject: [PATCH 4/6] Make corrections to code for support of parentheses --- intervals/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/intervals/__init__.py b/intervals/__init__.py index 42f365c..b909a43 100644 --- a/intervals/__init__.py +++ b/intervals/__init__.py @@ -151,11 +151,18 @@ def __init__( 30 """ - - if type(bounds) == type(lower_inc) == int and not upper_inc: + # This if-block adds support for parentheses as open intervals. + # Note: If the interval is initialized with the parentheses with two + # objects of same type, eg. + # IntInterval(1, 4) + # the bounds and lower_inc are received of that type and + # upper_inc is None. + # + # eg. + # IntInterval(1, 4) == IntInterval((1, 4)) + if type(bounds) == type(lower_inc) and not upper_inc: bounds = (bounds, lower_inc) - lower_inc = None - upper_inc = None + lower_inc = upper_inc = None self.lower, self.upper, self.lower_inc, self.upper_inc = ( self.parser(bounds, lower_inc, upper_inc) From 08db3aa9f412fc7b54180bc202562d9f10bb6fd0 Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 9 Feb 2014 23:39:31 +0200 Subject: [PATCH 5/6] Add support for square brackets as closed intervals --- intervals/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/intervals/__init__.py b/intervals/__init__.py index b909a43..1b664de 100644 --- a/intervals/__init__.py +++ b/intervals/__init__.py @@ -72,8 +72,24 @@ def wrapper(self, arg): return wrapper +class ClosedInterval(type): + """ + Supports initialization of intervals using square brackets and makes them + closed intervals. + + eg. + + IntInterval[1, 4] == IntInterval([1, 4]) + """ + def __getitem__(self, bounds): + lower_inc = upper_inc = True + return self(bounds, lower_inc, upper_inc) + + @total_ordering class AbstractInterval(object): + __metaclass__ = ClosedInterval + step = None type = None parser = IntervalParser() @@ -151,6 +167,7 @@ def __init__( 30 """ + # This if-block adds support for parentheses as open intervals. # Note: If the interval is initialized with the parentheses with two # objects of same type, eg. From 08e729244b009b1a63c0c709e27f893e12e49261 Mon Sep 17 00:00:00 2001 From: Adarsh Krishnan Date: Sun, 30 Mar 2014 17:15:12 +0300 Subject: [PATCH 6/6] Use six and add support for python3 --- intervals/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/intervals/__init__.py b/intervals/__init__.py index 1b664de..4c949ad 100644 --- a/intervals/__init__.py +++ b/intervals/__init__.py @@ -86,10 +86,9 @@ def __getitem__(self, bounds): return self(bounds, lower_inc, upper_inc) +@six.add_metaclass(ClosedInterval) @total_ordering class AbstractInterval(object): - __metaclass__ = ClosedInterval - step = None type = None parser = IntervalParser()