@Skaperen Thank to you. I learn how to work with floats as Fraction. Learn how to convert them.
Example my float range
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.