Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a dictionary of ranges
#8
@Skaperen Thank to you. I learn how to work with floats as Fraction. Learn how to convert them.
Example my float range
from fractions import Fraction

class FloatRange:
    # width = how many float points
    def __init__(self, width, start, end, step=1):
        self.width_size = width
        self.width = Fraction(10 ** width, 1)
        if end is None:
            self.end = self.num_to_fraction(start)
            self.start = Fraction(1, 10 ** width)
        else:
            self.start = self.num_to_fraction(start)
            self.end = self.num_to_fraction(end)

        self.step = Fraction(step, 10 ** width)
        self.float_range = self.start, self.end, self.step

    def num_to_fraction(self, pynum):
        if type(pynum) is float:
            f = str(pynum)
            i = f.find('.') + 1
            return Fraction(int(f.replace('.', '')), 10 ** (len(f) - i))
        return Fraction(pynum, 1)

    def convert(self, value):
        str_value = str(value)
        index = str_value.find('.') + 1
        width = len(str_value) - index
        if width < self.width_size:
            return int(str_value.replace('.','')) * (10 ** (self.width_size - width))
        elif index == 0:
            return int(str_value.replace('.','')) * (10 ** self.width_size)
        return int(str_value.replace('.',''))

    def __iter__(self):
        while self.start < self.end:
            yield float(self.start)
            self.start += self.step

    def __lt__(self, value):
        return self.num_to_fraction(value) > self.float_range[1]

    def __gt__(self, value):
        return self.num_to_fraction(value) < self.float_range[0]

    def __contains__(self, value):
        v = self.convert(value)
        s = int(self.float_range[0] * self.width)
        e = int(self.float_range[1] * self.width)
        st = int(self.float_range[2] * self.width)
        return v in range(s, e, st) # or
        #return s <= v < e

if __name__ == "__main__":
    for i in range(1,4):
        frange = FloatRange(i, 4.5, 4.7)
        print("\n ** FloatRange contains float point", i, "**")
        print(452 in frange)
        print(45.2 in frange)
        print(4.52 in frange)
        print(4.521 in frange)

    print("\n ** Less than and Greater than test **")
    frange = FloatRange(2, 4.5, 4.7)
    print(4.4 < frange)
    print(4.6 < frange)
    print(4.6 > frange)
    print(4.8 > frange)

    print("\n ** Output test **")
    for i in frange:
        print(i)
Are you looking for something like this ?
from fractions import Fraction

class Ranges:
    def __init__(self):
        self.range_list =

    def add(self, start, end):
        for index, item in enumerate(self.range_list):
            if item is None:
                self.range_list[index] = (start, end)
                return

        self.range_list.append((start, end))

    def num_to_fraction(self, pynum):
        if type(pynum) is float:
            f = str(pynum)
            i = f.find('.') + 1
            return Fraction(int(f.replace('.', '')), 10 ** (len(f) - i))

        return Fraction(pynum, 1)

    def fetch(self, value):
        for index, group in enumerate(self.range_list):
            if group is None:
                continue
            elif group[0] <= value < group[1]:
                return index, group

        return None, None

    def run(self, index, width):
        if self.range_list[index] is None:
            return

        step = Fraction(1, 10 ** width)
        width = Fraction(10 ** width, 1)
        start, end = map(self.num_to_fraction, self.range_list[index])
        while start < end:
            yield float(start)
            start += step

    def run_combine(self, index, width, combine):
        for i in self.run(index, width):
            yield combine + str(i)

    def run_ip4(self, index, width, index2, width2):
        for i in self.run(index, width):
            for j in self.run(index2, width2):
                yield str(i) + '.' + str(j)

    def smash(self, index):
        self.range_list[index] = None

    def __str__(self):
        line =  "Ranges ["
        for r in self.range_list:
            if r is None:
                line += "None, "
            else:
                line += "({0}, {1}), ".format(*r)

        return line[:-2] + ']'


if __name__ == '__main__':
    my_range = Ranges()
    my_range.add(17.21, 17.27)
    my_range.add(2, 3.5)
    my_range.add(4.5, 6)

    for i in my_range.run_combine(1, 1, "192.168."):
        print(i)

    print()
    for i in my_range.run_ip4(0, 2, 2, 1):
        print(i)
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
a dictionary of ranges - by Skaperen - Nov-29-2017, 01:53 AM
RE: a dictionary of ranges - by Windspar - Nov-29-2017, 01:45 PM
RE: a dictionary of ranges - by buran - Nov-29-2017, 02:47 PM
RE: a dictionary of ranges - by Windspar - Nov-29-2017, 04:13 PM
RE: a dictionary of ranges - by Windspar - Nov-29-2017, 09:23 PM
RE: a dictionary of ranges - by Skaperen - Nov-30-2017, 02:17 AM
RE: a dictionary of ranges - by buran - Nov-30-2017, 04:52 AM
RE: a dictionary of ranges - by Skaperen - Dec-01-2017, 04:10 AM
RE: a dictionary of ranges - by Windspar - Nov-30-2017, 05:56 PM
RE: a dictionary of ranges - by Windspar - Dec-02-2017, 07:40 PM
RE: a dictionary of ranges - by Windspar - Dec-02-2017, 11:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,664 May-14-2023, 04:29 PM
Last Post: Winfried
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 2,022 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Dictionary with ranges that have a float step value Irv1n 2 2,197 Apr-21-2021, 09:04 PM
Last Post: Yoriz
  Two operations in two ranges salwa17 3 2,267 Jun-22-2020, 04:15 PM
Last Post: perfringo
  iterating a list of ranges Skaperen 1 2,091 May-22-2019, 07:44 AM
Last Post: Gribouillis
  Subnet Mask Ranges ab52 0 1,867 Mar-11-2019, 10:39 AM
Last Post: ab52
  joined ranges Skaperen 4 3,286 Apr-03-2018, 07:14 PM
Last Post: Gribouillis
  compacting multiple ranges Skaperen 2 3,168 Oct-11-2017, 08:33 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020