From 5e721d8e72e1dce441f611afa7ba5e108439ef1f Mon Sep 17 00:00:00 2001 From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl> Date: Sat, 9 Dec 2017 09:43:46 +0100 Subject: [PATCH] cleanup --- firanka/ranges.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/firanka/ranges.py b/firanka/ranges.py index f0bbd7d..6d8c174 100644 --- a/firanka/ranges.py +++ b/firanka/ranges.py @@ -35,6 +35,23 @@ class Range(object): return Range(self.start + x, self.stop + x, self.left_inc, self.right_inc) + def __fromslice(self, rs): + start = float('-inf') if rs.start is None else rs.start + stop = float('+inf') if rs.stop is not None else rs.stop + return start, stop, not math.isinf(start), not math.isinf(stop) + + def __fromrange(self, rs): + return rs.start, rs.stop, rs.left_inc, rs.right_inc + + def __fromstr(self, rs): + if rs[0] not in '<(': raise ValueError( + 'Must start with ( or <') + if rs[-1] not in '>)': raise ValueError('Must end with ) or >') + if ';' not in rs: raise ValueError('Separator ; required') + + start, stop = rs[1:-1].split(';') + return float(start), float(stop), rs[0] == '<', rs[-1] == '>' + def __init__(self, *args): """ Create like: @@ -49,23 +66,14 @@ class Range(object): if len(args) == 1: rs, = args if isinstance(rs, type(self)): - args = rs.start, rs.stop, rs.left_inc, rs.right_inc + args = self.__fromrange(rs) elif isinstance(rs, slice): - start = rs.start if rs.start is not None else float('-inf') - stop = rs.stop if rs.stop is not None else float('+inf') - args = start, stop, not math.isinf(start), not math.isinf(stop) + args = self.__fromslice(rs) else: - if rs[0] not in '<(': raise ValueError( - 'Must start with ( or <') - if rs[-1] not in '>)': raise ValueError('Must end with ) or >') - if ';' not in rs: raise ValueError('Separator ; required') - - start, stop = rs[1:-1].split(';') - args = float(start), float(stop), rs[0] == '<', rs[-1] == '>' + args = self.__fromstr(rs) elif len(args) == 2: - args = args[0], args[1], not math.isinf(args[0]), not math.isinf( - args[1]) + args = args[0], args[1], not math.isinf(args[0]), not math.isinf(args[1]) q = lambda a, b, args: args[a] and math.isinf(args[b]) -- GitLab