diff --git a/firanka/ranges.py b/firanka/ranges.py index f0bbd7d1bb7d6b8f6badfd8c36ee5f147542aa54..6d8c174c10455d79f9cf14e86599cb188a0f0f51 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])