def test():
    '''Test the basic workings of `parse_slice`.'''
    
    r1 = range(5)
    r2 = range(2, 10)
    r3 = range(100, 3, -7)
    ranges = [r1, r2, r3]
    
    slices = [slice(3), slice(5), slice(9), slice(1, 4), slice(4, 7),
              slice(6, 2), slice(1, 4, 1), slice(1, 5, 3), slice(6, 2, 3),
              slice(6, 2, -3),  slice(8, 2, -1), slice(2, 5, -2),
              slice(None, 5, -2), slice(6, None, -2), slice(8, 4, None),
              slice(None, None, -2)]
    
    for slice_ in slices:
        (start, stop, step) = parse_slice(slice_)
        
        # Replacing `infinity` with huge number cause Python's lists can't
        # handle `infinity`:
        if abs(start) == infinity: start = 10**10 * math_tools.get_sign(start)
        if abs(stop) == infinity: stop = 10**10 * math_tools.get_sign(stop)
        if abs(step) == infinity: step = 10**10 * math_tools.get_sign(step)
        #######################################################################
            
        assert [start, stop, step].count(None) == 0
        
        parsed_slice = slice(start, stop, step)
        for range_ in ranges:
            assert range_[slice_] == range_[parsed_slice]
def test():
    
    r1 = list(range(5))
    r2 = list(range(2, 10))
    r3 = list(range(100, 3, -7))
    ranges = [r1, r2, r3]
    
    slices = [slice(3), slice(5), slice(9), slice(1, 4), slice(4, 7),
              slice(6, 2), slice(1, 4, 1), slice(1, 5, 3), slice(6, 2, 3),
              slice(6, 2, -3),  slice(8, 2, -1), slice(2, 5, -2),
              slice(None, 5, -2), slice(6, None, -2), slice(8, 4, None),
              slice(None, None, -2)]
    
    for slice_ in slices:
        canonical_slice = CanonicalSlice(slice_)
        
        # Replacing `infinity` with huge number cause Python's lists can't
        # handle `infinity`:
        if abs(canonical_slice.start) == infinity:
            start = 10**10 * math_tools.get_sign(canonical_slice.start)
        if abs(canonical_slice.stop) == infinity:
            stop = 10**10 * math_tools.get_sign(canonical_slice.stop)
        if abs(canonical_slice.step) == infinity:
            step = 10**10 * math_tools.get_sign(canonical_slice.step)
        #######################################################################
            
        assert [canonical_slice.start, canonical_slice.stop,
                canonical_slice.step].count(None) == 0
        
        for range_ in ranges:
            assert range_[slice_] == range_[canonical_slice.slice_]
Beispiel #3
0
    def length(self):
        '''
        The length of the `CuteRange`.

        We're using a property `.length` rather than the built-in `__len__`
        because `__len__` can't handle infinite values or floats.
        '''
        from python_toolbox import math_tools

        if math_tools.get_sign(self.distance_to_cover) != \
                                                math_tools.get_sign(self.step):
            return 0
        else:
            raw_length, remainder = math_tools.cute_divmod(
                self.distance_to_cover, self.step)
            raw_length += (remainder != 0)
            return raw_length
def test():

    r1 = list(range(5))
    r2 = list(range(2, 10))
    r3 = list(range(100, 3, -7))
    ranges = [r1, r2, r3]

    slices = [
        slice(3),
        slice(5),
        slice(9),
        slice(1, 4),
        slice(4, 7),
        slice(6, 2),
        slice(1, 4, 1),
        slice(1, 5, 3),
        slice(6, 2, 3),
        slice(6, 2, -3),
        slice(8, 2, -1),
        slice(2, 5, -2),
        slice(None, 5, -2),
        slice(6, None, -2),
        slice(8, 4, None),
        slice(None, None, -2)
    ]

    for slice_ in slices:
        canonical_slice = CanonicalSlice(slice_)

        # Replacing `infinity` with huge number cause Python's lists can't
        # handle `infinity`:
        if abs(canonical_slice.start) == infinity:
            start = 10**10 * math_tools.get_sign(canonical_slice.start)
        if abs(canonical_slice.stop) == infinity:
            stop = 10**10 * math_tools.get_sign(canonical_slice.stop)
        if abs(canonical_slice.step) == infinity:
            step = 10**10 * math_tools.get_sign(canonical_slice.step)
        #######################################################################

        assert [
            canonical_slice.start, canonical_slice.stop, canonical_slice.step
        ].count(None) == 0

        for range_ in ranges:
            assert range_[slice_] == range_[canonical_slice.slice_]
Beispiel #5
0
 def length(self):
     '''
     The length of the `CuteRange`.
     
     We're using a property `.length` rather than the built-in `__len__`
     because `__len__` can't handle infinite values or floats.
     '''
     from python_toolbox import math_tools
     
     if math_tools.get_sign(self.distance_to_cover) != \
                                             math_tools.get_sign(self.step):
         return 0
     else:
         raw_length, remainder = math_tools.cute_divmod(
             self.distance_to_cover, self.step
         )
         raw_length += (remainder != 0)
         return raw_length
Beispiel #6
0
 def _value_to_ratio(self, value):
     '''Convert from value to ratio.'''
     return math_tools.get_sign(value) * \
            (2 / math.pi) * \
            math.acos(
                math.exp(
                    - (math.pi * math.sqrt(abs(value))) / \
                    (2 * math.sqrt(self.sensitivity))
                )
            )
Beispiel #7
0
def test():
    '''Test the basic workings of `parse_slice`.'''

    r1 = list(range(5))
    r2 = list(range(2, 10))
    r3 = list(range(100, 3, -7))
    ranges = [r1, r2, r3]

    slices = [
        slice(3),
        slice(5),
        slice(9),
        slice(1, 4),
        slice(4, 7),
        slice(6, 2),
        slice(1, 4, 1),
        slice(1, 5, 3),
        slice(6, 2, 3),
        slice(6, 2, -3),
        slice(8, 2, -1),
        slice(2, 5, -2),
        slice(None, 5, -2),
        slice(6, None, -2),
        slice(8, 4, None),
        slice(None, None, -2)
    ]

    for slice_ in slices:
        (start, stop, step) = parse_slice(slice_)

        # Replacing `infinity` with huge number cause Python's lists can't
        # handle `infinity`:
        if abs(start) == infinity: start = 10**10 * math_tools.get_sign(start)
        if abs(stop) == infinity: stop = 10**10 * math_tools.get_sign(stop)
        if abs(step) == infinity: step = 10**10 * math_tools.get_sign(step)
        #######################################################################

        assert [start, stop, step].count(None) == 0

        parsed_slice = slice(start, stop, step)
        for range_ in ranges:
            assert range_[slice_] == range_[parsed_slice]
Beispiel #8
0
    def index(self, i, start=-infinity, stop=infinity):
        '''Get the index number of `i` in this `CuteRange`.'''
        from python_toolbox import math_tools
        if not isinstance(i, numbers.Number):
            raise ValueError
        else:
            distance = i - self.start
            if distance == 0 and self:
                if start <= 0 < stop: return 0
                else: raise ValueError("Found but not within range.")
            if math_tools.get_sign(distance) != math_tools.get_sign(self.step):
                raise ValueError
            index, remainder = math_tools.cute_divmod(distance, self.step)
            if remainder == 0 and (0 <= index < self.length
                                   or index == self.length == infinity):
                if start <= index < stop: return index
                else: raise ValueError("Found but not within range.")

            else:
                raise ValueError
Beispiel #9
0
    def index(self, i, start=-infinity, stop=infinity):
        '''Get the index number of `i` in this `CuteRange`.'''
        from python_toolbox import math_tools
        if not isinstance(i, numbers.Number):
            raise ValueError
        else:
            distance = i - self.start
            if distance == 0 and self:
                if start <= 0 < stop: return 0
                else: raise ValueError("Found but not within range.")
            if math_tools.get_sign(distance) != math_tools.get_sign(self.step):
                raise ValueError
            index, remainder = math_tools.cute_divmod(distance, self.step)
            if remainder == 0 and (0 <= index < self.length or
                                             index == self.length == infinity):
                if start <= index < stop: return index
                else: raise ValueError("Found but not within range.")

            else:
                raise ValueError
Beispiel #10
0
 def _ratio_to_value(self, ratio):
     '''Convert from ratio to value.'''
     return self.sensitivity * \
            math_tools.get_sign(ratio) * \
            (4 / math.pi**2) * \
            math.log(math.cos(ratio * math.pi / 2))**2