def __div__(self, amount): if isinstance(amount, Duration) and amount.month: m = self.month r = self.milli # DO NOT CONSIDER TIME OF DAY tod = r % MILLI_VALUES.day r = r - tod if m == 0 and r > (MILLI_VALUES.year / 3): m = Math.floor(12 * self.milli / MILLI_VALUES.year) r -= (m / 12) * MILLI_VALUES.year else: r = r - (self.month * MILLI_VALUES.month) if r >= MILLI_VALUES.day * 31: from pyLibrary.debugs.logs import Log Log.error("Do not know how to handle") r = MIN(29 / 30, (r + tod) / (MILLI_VALUES.day * 30)) output = Math.floor(m / amount.month) + r return output elif Math.is_number(amount): output = Duration(0) output.milli = self.milli / amount output.month = self.month / amount return output else: return self.milli / amount.milli
def test_floor_mod_identity_w_ints(self): for i in range(100): x = Random.float()*200 - 100.0 m = floor(abs(random.gauss(0, 5))) if m == 0: self.assertEqual(Math.floor(x, m), None) self.assertEqual(Math.mod(x, m), None) else: self.assertAlmostEqual(Math.floor(x, m)+Math.mod(x, m), x, places=7)
def pop(self, wait=SECOND, till=None): m = self.queue.read(wait_time_seconds=Math.floor(wait.seconds)) if not m: return None self.pending.append(m) return convert.json2value(m.get_body())
def icompressed2ibytes(source): """ :param source: GENERATOR OF COMPRESSED BYTES :return: GENERATOR OF BYTES """ decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS) last_bytes_count = 0 # Track the last byte count, so we do not show too many debug lines bytes_count = 0 for bytes_ in source: data = decompressor.decompress(bytes_) bytes_count += len(data) if Math.floor(last_bytes_count, 1000000) != Math.floor(bytes_count, 1000000): last_bytes_count = bytes_count if DEBUG: Log.note("bytes={{bytes}}", bytes=bytes_count) yield data
def icompressed2ibytes(source): """ :param source: GENERATOR OF COMPRESSED BYTES :return: GENERATOR OF BYTES """ decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS) last_bytes_count = 0 # Track the last byte count, so we do not show too many debug lines bytes_count = 0 for bytes_ in source: data = decompressor.decompress(bytes_) bytes_count += len(data) if Math.floor(last_bytes_count, 1000000) != Math.floor( bytes_count, 1000000): last_bytes_count = bytes_count if DEBUG: Log.note("bytes={{bytes}}", bytes=bytes_count) yield data
def floor(self, interval=None): if not isinstance(interval, Duration): from pyLibrary.debugs.logs import Log Log.error("Expecting an interval as a Duration object") output = Duration(0) if interval.month: if self.month: output.month = int(Math.floor(self.month / interval.month) * interval.month) output.milli = output.month * MILLI_VALUES.month return output # A MONTH OF DURATION IS BIGGER THAN A CANONICAL MONTH output.month = int(Math.floor(self.milli * 12 / MILLI_VALUES["year"] / interval.month) * interval.month) output.milli = output.month * MILLI_VALUES.month else: output.milli = Math.floor(self.milli / (interval.milli)) * (interval.milli) return output
def pop_message(self, wait=SECOND, till=None): """ RETURN THE MESSAGE, CALLER IS RESPONSIBLE FOR CALLING delete_message() WHEN DONE """ m = self.queue.read(wait_time_seconds=Math.floor(wait.seconds)) if not m: return None output = convert.json2value(m.get_body()) return output
def pop(self, wait=SECOND, till=None): if till is not None and not isinstance(till, Signal): Log.error("Expecting a signal") m = self.queue.read(wait_time_seconds=Math.floor(wait.seconds)) if not m: return None self.pending.append(m) output = convert.json2value(m.get_body()) return output
def setup(source, destination, settings): global done_min global done_max # FIND LARGEST ID IN DESTINATION max_id = destination.find_largest_key() # USE AS STARTING POINT if max_id: max_id = Math.floor(max_id, BLOCK_SIZE) done_min = done_max = max_id
def pop_message(self, wait=SECOND, till=None): """ RETURN TUPLE (message, payload) CALLER IS RESPONSIBLE FOR CALLING message.delete() WHEN DONE """ message = self.queue.read(wait_time_seconds=Math.floor(wait.seconds)) if not message: return None message.delete = lambda: self.queue.delete_message(message) payload = convert.json2value(message.get_body()) return message, payload
def advance(self): with self.locker: try: if self.today != today(): self.today = today() self.count = 0 batch = Math.floor(self.count/BATCH_SIZE) return unicode(self.today) + "." + unicode(batch), self.count % BATCH_SIZE finally: self.count += 1
def intervals(_min, _max=None, size=1): """ RETURN (min, max) PAIRS OF GIVEN SIZE, WHICH COVER THE _min, _max RANGE THE LAST PAIR MAY BE SMALLER Yes! It's just like range(), only cooler! """ if _max == None: _max = _min _min = 0 _max = int(Math.ceiling(_max)) _min = int(Math.floor(_min)) output = ((x, min(x + size, _max)) for x in __builtin__.range(_min, _max, size)) return output
def pop_message(self, wait=SECOND, till=None): """ RETURN TUPLE (message, payload) CALLER IS RESPONSIBLE FOR CALLING message.delete() WHEN DONE """ if till is not None and not isinstance(till, Signal): Log.error("Expecting a signal") message = self.queue.read(wait_time_seconds=Math.floor(wait.seconds)) if not message: return None message.delete = lambda: self.queue.delete_message(message) payload = convert.json2value(message.get_body()) return message, payload
def test_floor(self): self.assertEqual(Math.floor(0, 1), 0) self.assertEqual(Math.floor(1, 1), 1) self.assertEqual(Math.floor(-1, 1), -1) self.assertEqual(Math.floor(0.1, 1), 0) self.assertEqual(Math.floor(1.1, 1), 1) self.assertEqual(Math.floor(-1.1, 1), -2) self.assertEqual(Math.floor(0, 2), 0) self.assertEqual(Math.floor(1, 2), 0) self.assertEqual(Math.floor(-1, 2), -2) self.assertEqual(Math.floor(0.1, 2), 0) self.assertEqual(Math.floor(1.1, 2), 0) self.assertEqual(Math.floor(-1.1, 2), -2) self.assertEqual(Math.floor(-10, 2), -10)
def test_floor_mod_identity(self): for i in range(100): x = Random.float()*200 - 100.0 m = abs(random.gauss(0, 5)) self.assertAlmostEqual(Math.floor(x, m)+Math.mod(x, m), x, places=7)
def __unicode__(self): if not self.milli: return "zero" output = "" rest = (self.milli - (MILLI_VALUES.month * self.month) ) # DO NOT INCLUDE THE MONTH'S MILLIS isNegative = (rest < 0) rest = Math.abs(rest) # MILLI rem = rest % 1000 if rem != 0: output = "+" + unicode(rem) + "milli" + output rest = Math.floor(rest / 1000) # SECOND rem = rest % 60 if rem != 0: output = "+" + unicode(rem) + "second" + output rest = Math.floor(rest / 60) # MINUTE rem = rest % 60 if rem != 0: output = "+" + unicode(rem) + "minute" + output rest = Math.floor(rest / 60) # HOUR rem = rest % 24 if rem != 0: output = "+" + unicode(rem) + "hour" + output rest = Math.floor(rest / 24) # DAY if (rest < 11 and rest != 7) or rest % 10 == 0: rem = rest rest = 0 else: rem = rest % 7 rest = Math.floor(rest / 7) if rem != 0: output = "+" + unicode(rem) + "day" + output # WEEK if rest != 0: output = "+" + unicode(rest) + "week" + output if isNegative: output = output.replace("+", "-") # MONTH AND YEAR if self.month: sign = "-" if self.month < 0 else "+" month = Math.abs(self.month) if month <= 18 and month != 12: output = sign + unicode(month) + "month" + output else: m = month % 12 if m != 0: output = sign + unicode(m) + "month" + output y = Math.floor(month / 12) output = sign + unicode(y) + "year" + output if output[0] == "+": output = output[1::] if output[0] == '1' and not Math.is_number(output[1]): output = output[1::] return output
def __unicode__(self): if not self.milli: return "zero" output = "" rest = (self.milli - (MILLI_VALUES.month * self.month)) # DO NOT INCLUDE THE MONTH'S MILLIS isNegative = (rest < 0) rest = Math.abs(rest) # MILLI rem = rest % 1000 if rem != 0: output = "+" + unicode(rem) + "milli" + output rest = Math.floor(rest / 1000) # SECOND rem = rest % 60 if rem != 0: output = "+" + unicode(rem) + "second" + output rest = Math.floor(rest / 60) # MINUTE rem = rest % 60 if rem != 0: output = "+" + unicode(rem) + "minute" + output rest = Math.floor(rest / 60) # HOUR rem = rest % 24 if rem != 0: output = "+" + unicode(rem) + "hour" + output rest = Math.floor(rest / 24) # DAY if (rest < 11 and rest != 7) or rest % 10 == 0: rem = rest rest = 0 else: rem = rest % 7 rest = Math.floor(rest / 7) if rem != 0: output = "+" + unicode(rem) + "day" + output # WEEK if rest != 0: output = "+" + unicode(rest) + "week" + output if isNegative: output = output.replace("+", "-") # MONTH AND YEAR if self.month: sign = "-" if self.month < 0 else "+" month = Math.abs(self.month) if month <= 18 and month != 12: output = sign + unicode(month) + "month" + output else: m = month % 12 if m != 0: output = sign + unicode(m) + "month" + output y = Math.floor(month / 12) output = sign + unicode(y) + "year" + output if output[0] == "+": output = output[1::] if output[0] == '1' and not Math.is_number(output[1]): output = output[1::] return output