def _subdivide_interval_by_hour(start, end): """Convert a time interval to a map from hours (represented as :py:class:`datetime.datetime` for the start of the hour) to the number of seconds during that hour that are within the interval *start* and *end* are :py:class:`datetime.datetime` objects. """ start_hour = start.replace(minute=0, second=0, microsecond=0) end_hour = end.replace(minute=0, second=0, microsecond=0) if start_hour == end_hour: hour_to_secs = {start_hour: timedelta.total_seconds(end - start)} else: hour_to_secs = {} hour_to_secs[start_hour] = timedelta.total_seconds( start_hour + timedelta(hours=1) - start) hour_to_secs[end_hour] = timedelta.total_seconds(end - end_hour) # fill in dates in the middle cur_hour = start_hour + timedelta(hours=1) while cur_hour < end_hour: hour_to_secs[cur_hour] = timedelta.total_seconds( timedelta(hours=1)) cur_hour += timedelta(hours=1) # remove zeros hour_to_secs = dict( (h, secs) for h, secs in hour_to_secs.items() if secs) return hour_to_secs
def _subdivide_interval_by_date(start, end): """Convert a time interval to a map from :py:class:`datetime.date` to the number of seconds within the interval on that date. *start* and *end* are :py:class:`datetime.datetime` objects. """ if start.date() == end.date(): date_to_secs = {start.date(): timedelta.total_seconds(end - start)} else: date_to_secs = {} date_to_secs[start.date()] = timedelta.total_seconds( datetime(start.year, start.month, start.day, tzinfo=start.tzinfo) + timedelta(days=1) - start) date_to_secs[end.date()] = timedelta.total_seconds( end - datetime(end.year, end.month, end.day, tzinfo=end.tzinfo)) # fill in dates in the middle cur_date = start.date() + timedelta(days=1) while cur_date < end.date(): date_to_secs[cur_date] = timedelta.total_seconds(timedelta(days=1)) cur_date += timedelta(days=1) # remove zeros date_to_secs = dict( (d, secs) for d, secs in date_to_secs.items() if secs) return date_to_secs
def time_to_go(game_date): tzinfos = {'CEST': 2} game_tz = tzinfos[game_date.split(' ')[-1]] current_tz = time.timezone / -3600 start_time = parser.parse(game_date[:game_date.rfind(' ')]) + timedelta(hours=current_tz - game_tz) if start_time < datetime.now(): return 'LIVE' to_go = start_time - datetime.now() hours = int(timedelta.total_seconds(to_go) / 3600) minutes = int(timedelta.total_seconds(to_go) % 3600) / 60 return 'in ' + str(hours).zfill(2) + ':' + str(minutes).zfill(2)
def operation_uptime(self): if self.end_date and self.start_date: timedelta = fields.Datetime.from_string(self.end_date) - \ fields.Datetime.from_string(self.start_date) self.uptime = timedelta.total_seconds() / 3600. else: self.uptime = 0
def getwkrev(conn): #Gets the revenue/week of FBO's c=getpaydbcon(conn) print("Getting FBO logs...") categories=("FBO ground crew fee", "Refuelling with JetA", "Refuelling with 100LL", "Aircraft maintenance", "Sale of wholesale JetA", "Sale of wholesale 100LL", "Sale of supplies", "Sale of building materials", "Installation of equipment in aircraft") revs=[] #List of revenue per week for each week fbos=getfbos(conn) #FBO's we care about wk=0 #Track which week is being added for pay in c.execute('SELECT date, amount, reason, fbo, comment FROM payments WHERE payto = ? ORDER BY date DESC',(fseutils.getname(),)): for cat in categories: if log[2]==cat: for fbo in fbos: if log[3]==fbo: #Ok we actually care about this payment and FBO thedate=fseutils.getdtime(pay[0]) if wk==0: #Add first payment info wk=1 revs.append([thedate,pay[1]]) else: #Figure it out I guess delta=thedate-revs[0][0] thisweek=timedelta.total_seconds(delta)/604800 if thisweek<wk: #Add to current week revs[wk-1][1]+=log[1] else: #Add new week revs.append([thedate,pay[1]]) break break return revs
def request(self, method, url, name = None, **kwargs): # store meta data that is used when reporting the request to locust's statistics request_meta = {"jobKey" : self.jobKey} # # set up pre_request hook for attaching meta data to the request object # request_meta["start_time"] = time.time() # gevent.sleep(1) print method, url response = self._send_request_safe_mode(method, url, **kwargs) # record the consumed time in milliseconds request_meta["response_time"] = round(timedelta.total_seconds(response.elapsed)*1000,3) or 0 request_meta["request_type"] = response.request.method # request_meta["name"] = name or (response.history and response.history[0] or response).request.path_url request_meta["name"] = '/' try: response.raise_for_status() except RequestException as e: request_meta['exception'] = e requests_stats.on_request_failure(**request_meta) else: request_meta["response_length"] = len(response.content or "") requests_stats.on_request_success(**request_meta)
def Sign(): Signin = "http://www.sun-knife.com/u.php" Singin2 = "http://home.51cto.com/index.php?s=/Home/toSign" date_object = LoadJson(r'Mytime.json') if date_object.date() >= datetime.now().date(): win32api.MessageBox(win32con.NULL, 'Finished, thanks!', 'SUN 7+1', win32con.MB_OK ) exit() else: nextdate = date_object + timedelta(days=1, minutes=2 ) print 'nextdate:', nextdate if nextdate < datetime.now(): webbrowser.open(Signin) sleep(5) webbrowser.open(Singin2) sleep(5) win32api.MessageBox(win32con.NULL, 'Recode the timestamp to file!', 'SUN 7+1', win32con.MB_OK ) WriteJson(r'Mytime.json', datetime.now()) else: webbrowser.open(Signin) sleep(5) webbrowser.open(Singin2) delttime = nextdate - datetime.now() deltat = int(timedelta.total_seconds(delttime)) win32api.MessageBox(win32con.NULL, str(delttime), 'SUN 7+1', win32con.MB_OK ) sleep(deltat) webbrowser.open(Signin) print sleep(5) win32api.MessageBox(win32con.NULL, 'Recode the timestamp to file!', 'SUN 7+1', win32con.MB_OK ) WriteJson(r'Mytime.json', datetime.now())
def get_speed(host,port,recvlen): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((host, port)) length=0 timeout=5 running = True starttime=datetime.now() while running : data = sock.recv(recvlen) if len(data) != 0 : length += len(data) else : break timedelta = datetime.now() - starttime print "Client received %d M data" % (length/1024/1024) print "Speed is %fMbs"% ((length/1024/1024)/timedelta.total_seconds()) finally: sock.close()
def parseFile(self, connection, newestChangeset, changesetFile): parsedCount = 0 skippedCount = 0 insertedCount = 0 startTime = datetime.now() context = etree.iterparse(changesetFile); action, root = context.next() for action, elem in context: if(elem.tag != 'changeset'): continue parsedCount += 1 if newestChangeset != -1 and long(elem.attrib['id']) <= newestChangeset: skippedCount += 1 else: tags = {} for tag in elem.iterchildren(tag='tag'): tags[tag.attrib['k']] = tag.attrib['v'] self.insertNew(connection, elem.attrib['id'], elem.attrib.get('uid', None), elem.attrib['created_at'], elem.attrib.get('min_lat', None), elem.attrib.get('max_lat', None), elem.attrib.get('min_lon', None), elem.attrib.get('max_lon', None), elem.attrib.get('closed_at', None), elem.attrib.get('open', None), elem.attrib.get('num_changes', None), elem.attrib.get('user', None), tags) insertedCount += 1 if((parsedCount % 10000) == 0): print "parsed %s skipped %s inserted %s" % ('{:,}'.format(parsedCount), '{:,}'.format(skippedCount), '{:,}'.format(insertedCount)) print "cumulative rate: %s/sec" % '{:,.0f}'.format(parsedCount/timedelta.total_seconds(datetime.now() - startTime)) elem.clear() root.clear() connection.commit() print "parsing complete" print "parsed {:,}".format(parsedCount) print "skipped {:,}".format(skippedCount) print "inserted {:,}".format(insertedCount)
def _run(self): self._timer = threading.Timer(self._tempo, self._run) self._timer.start() duration = int(self._duration - ((timedelta.total_seconds(datetime.now() - self._started)) / 60)) if duration <= 0 : self.stop() self._cbEnd(*self._args) else : self._cbTempo(duration, *self._args)
def is_locked(self): if self.locktime is None: return False timedelta = datetime.now() - self.locktime if timedelta.total_seconds() > TIMELIMIT: return False else: return True
def calc_max_donor_number(self): """ :return: """ event_duration_in_minutes = datetime.strptime(str(self.end_time), "%H:%M") - datetime.strptime(str(self.start_time), "%H:%M") event_duration_in_minutes = timedelta.total_seconds(event_duration_in_minutes) // 60 max_donor_number = ((event_duration_in_minutes - PREPARATION_TIME) // DONATION_TIME) * int(self.available_beds) self.max_donor_number = int(max_donor_number)
def elapsed_time(time1, time2): """ Returns the absolute value of the time elapsed between two datetimes in units of seconds time1 - first datetime time2 - second datetime """ elapsed = np.abs(timedelta.total_seconds(time1 - time2)) return elapsed
def calculate_times(self, entity, attribute, old, new, kwargs): # Only execute if turned on if new == "on": #Calculate past lengths of time conn = sqlite3.connect("file:" + kwargs["db_location"] + "?mode=ro", uri=True) c = conn.cursor() c.execute( 'SELECT state,last_changed FROM states where entity_id = "' + self.args["entity_id"] + '"') results = c.fetchall() conn.close() if self.args["debug"]: self.log("Entity changed.. reprocessing times") records = 0 times = [] if self.args["debug"]: self.log(results) # Iterate through entity changed times and create a list of times spent "on" for result in results: process = False if result[0] == "on": on = result[1] else: off = result[1] process = True if process == True: # Takes "off" timestamp and subtracts "on" timestamp to find timedelta length = datetime.strptime( off, '%Y-%m-%d %H:%M:%S.%f') - datetime.strptime( on, '%Y-%m-%d %H:%M:%S.%f') if self.args["debug"]: self.log("Found length: " + str(length)) times.append(length) records += 1 # Calculate average time spent "on" average = sum(times, timedelta(0)) / len(times) # Get total seconds from datetime hours, minutes, seconds average_seconds = int(timedelta.total_seconds(average)) if self.args["debug"]: self.log( "Calculation complete, determined average on time of " + str(average)) # Schedule our turn_off action X seconds in the future, the average time entity has spent is "on" self.log("Scheduling turn_off of %s in %s seconds." % (self.args["entity_id"], str(average_seconds))) self.run_in(self.average_exceeded, average_seconds)
def on_keep_alive(self, _e): if self.state.status == "INACTIVE": self.update_state({"status": "OFF"}) elif ( self.state.status == "IN_CALL" and timedelta.total_seconds(datetime.now() - self.state.last_action_time) > KEEP_ALIVE_INTERVAL ): self.update_state({"status": "INACTIVE"})
def _run(self): self._timer = threading.Timer(self._tempo, self._run) self._timer.start() duration = int(self._duration - ( (timedelta.total_seconds(datetime.now() - self._started)) / 60)) if duration <= 0: self.stop() self._cbEnd(*self._args) else: self._cbTempo(duration, *self._args)
def custom_encode(obj): if isinstance(obj, datetime): return date_util.timestamp(obj) elif isinstance(obj, date): return date_util.timestamp(obj) elif isinstance(obj, timedelta): return timedelta.total_seconds() elif isinstance(obj, unicode): return obj.encode('utf-8') raise NotImplementedError('Custom encoder not implemented')
def date_to_index(self, date): """datetime to time slot index in self.calendar_bit_field Example: now is 2020-02-21T00:00:00Z date: 2020-02-22T00:00:00Z returns 96 since (24h * (60min / 15min)) = 96 """ timedelta = date.astimezone(self.timezone) - self.today_start return round(timedelta.total_seconds() // 60) // self.MIN_RESOLUTION
def gen(self, count, earliest, latest, samplename=None): # print type(latest - earliest).total_seconds() time_interval = timedelta.total_seconds((latest - earliest)) / count for i in xrange(count): current_time_object = earliest + datetime.timedelta( 0, time_interval * (i + 1)) msg = '{0} -0700 WINDBAG Event {1} of {2}'.format( current_time_object, (i + 1), count) self._out.send(msg) return 0
def tempoRestante(self): self.horaAtual = datetime.now() diff = self.convTime(self.finalExpediente) - self.horaAtual if self.horaAtual < self.convTime(self.horaAlmoco): diff -= self.convTime(self.retornoAlmoco) - self.convTime( self.horaAlmoco) return timedelta.total_seconds(diff)
def mem_get(key): t = memcache.get(key) if t : val, save_time = t age = timedelta.total_seconds(datetime.utcnow()-save_time) else: val, age = None, 0 logging.info(t) return val,age
def gen(self, count, earliest, latest, samplename=None): if count < 0: self.logger.warn('Sample size not found for count=-1 and generator=windbag, defaulting to count=60') count = 60 time_interval = timedelta.total_seconds((latest - earliest)) / count for i in xrange(count): current_time_object = earliest + datetime.timedelta(0, time_interval * (i + 1)) msg = '{0} -0700 WINDBAG Event {1} of {2}'.format(current_time_object, (i + 1), count) self._out.send(msg) return 0
def parseFile(self, connection, changesetFile, doReplication): parsedCount = 0 startTime = datetime.now() cursor = connection.cursor() context = etree.iterparse(changesetFile) action, root = context.next() for action, elem in context: if (elem.tag != 'changeset'): continue parsedCount += 1 tags = {} for tag in elem.iterchildren(tag='tag'): tags[tag.attrib['k']] = tag.attrib['v'] comments = [] for discussion in elem.iterchildren(tag='discussion'): for commentElement in discussion.iterchildren(tag='comment'): comment = dict() comment['uid'] = commentElement.attrib.get('uid') comment['user'] = commentElement.attrib.get('user') comment['date'] = commentElement.attrib.get('date') for text in commentElement.iterchildren(tag='text'): comment['text'] = text.text comments.append(comment) if (doReplication): self.deleteExisting(connection, elem.attrib['id']) self.insertNew(connection, elem.attrib['id'], elem.attrib.get('uid', None), elem.attrib['created_at'], elem.attrib.get('min_lat', None), elem.attrib.get('max_lat', None), elem.attrib.get('min_lon', None), elem.attrib.get('max_lon', None), elem.attrib.get('closed_at', None), elem.attrib.get('open', None), elem.attrib.get('num_changes', None), elem.attrib.get('user', None), tags, comments) if ((parsedCount % 10000) == 0): print "parsed %s" % ('{:,}'.format(parsedCount)) print "cumulative rate: %s/sec" % '{:,.0f}'.format( parsedCount / timedelta.total_seconds(datetime.now() - startTime)) #clear everything we don't need from memory to avoid leaking elem.clear() while elem.getprevious() is not None: del elem.getparent()[0] connection.commit() print "parsing complete" print "parsed {:,}".format(parsedCount)
async def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = timedelta.total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) got_one_item = False try: async for item in target(*args, **kwargs): got_one_item = True yield item except exception as e: # If we've already fed a result out of this method, # we can't pull it back. So don't try to pull back/retry # the exception either. if got_one_item: raise giveup_result = await giveup(e) max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if giveup_result or max_tries_exceeded or max_time_exceeded: await _call_handlers(on_giveup, *details) raise try: seconds = _next_wait(wait, jitter, elapsed, max_time_) except StopIteration: # pragma: no cover await _call_handlers(on_giveup, *details) raise e await _call_handlers(on_backoff, *details, wait=seconds) # Note: there is no convenient way to pass explicit event # loop to decorator, so here we assume that either default # thread event loop is set and correct (it mostly is # by default), or Python >= 3.5.3 or Python >= 3.6 is used # where loop.get_event_loop() in coroutine guaranteed to # return correct value. # See for details: # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk> # <https://bugs.python.org/issue28613> await asyncio.sleep(seconds) else: await _call_handlers(on_success, *details) return
def solution(play_time, adv_time, logs): all_time = 0 adv_time = list(map(int, adv_time.split(":"))) play_time_list = list(map(int, play_time.split(":"))) play_time_second = timedelta(hours=play_time_list[0], minutes=play_time_list[1], seconds=play_time_list[2]) for log in logs: log = log.split('-') start_time = list(map(int, log[0].split(":"))) end_time = list(map(int, log[0].split(":"))) adv_start_time = timedelta(hours=start_time[0], minutes=start_time[1], seconds=start_time[2]) adv_end_time = timedelta( hours=end_time[0], minutes=end_time[1], seconds=end_time[2]) + timedelta( hours=adv_time[0], minutes=adv_time[1], seconds=adv_time[2]) case_time = 0 if adv_end_time < play_time_second: for chk in logs: chk = chk.split('-') start_time_list = list(map(int, chk[0].split(":"))) end_time_list = list(map(int, chk[1].split(":"))) play_start_time = timedelta(hours=start_time_list[0], minutes=start_time_list[1], seconds=start_time_list[2]) play_end_time = timedelta(hours=end_time_list[0], minutes=end_time_list[1], seconds=end_time_list[2]) viewing_start_time = adv_start_time viewing_end_time = adv_end_time if play_end_time < adv_start_time: continue if play_start_time > adv_end_time: continue if adv_start_time <= play_start_time: viewing_start_time = play_start_time if adv_end_time >= play_end_time: viewing_end_time = play_end_time viewing_time = viewing_end_time - viewing_start_time viewing_time = int(timedelta.total_seconds(viewing_time)) case_time += viewing_time if all_time < case_time: all_time = case_time if all_time == 0: answer = "00:00:00" else: answer = str(timedelta(seconds=all_time)) return answer
def readFromPairBuilder(scorer_id, batchtype, batchsize): print(scorer_id) temp = 0 time_x = dt.now() consumer_c3 = KafkaConsumer("pairs" + str(scorer_id), bootstrap_servers=['localhost:9092'], group_id='apoorva-thesis') print("Consumer for pairs" + str(scorer_id) + " is ready to listen!") last_read = dt.now() timeout_period = timedelta(seconds=5) timeout = last_read + timeout_period for msg in consumer_c3: # superstring: (mgfID, fastaID, load, time) _separator_ if "_init_" in msg.key.decode('utf-8'): pass elif "_final_" in msg.key.decode('utf-8'): pass elif msg not in messages: messages.add(msg) timings = msg.key.decode('utf-8').split("_the_real_separator_") casstime2 = timings[3] secondKafkaTime = timedelta.total_seconds( dt.now() - dt.strptime(timings[0], '%Y-%m-%d %H:%M:%S.%f')) firstKafkaTime = timings[1] alternativeCassTime = timings[2] if batchtype == "predefined": batch.add(msg) if len(batch) == batchsize or dt.now() > timeout: print("Batch of " + str(len(batch)) + " sent for processing!") processbatch(batch, temp) batch.clear() last_read = dt.now() timeout_period = timedelta(seconds=5) timeout = last_read + timeout_period elif batchtype == "perspectra": messageinquestion = msg.value.decode('utf-8') time_y = dt.now() kafkatime = timedelta.total_seconds(time_y - time_x) processspectra(messageinquestion, kafkatime, secondKafkaTime, firstKafkaTime, casstime2, alternativeCassTime)
async def test_get_duration_swapped_arguments(): """ Invoke get_duration function with swapped start and end arguments. :return: None. """ start = datetime.utcnow() end = start + timedelta(seconds=1) actual = await get_duration(start=end, end=start) assert isinstance(actual, float) assert actual == round(number=timedelta.total_seconds(start - end), ndigits=3)
def average_turnover_2pt(self, ratios, sampledatetime): """Calculate the average isotope turnover rate in 1/hr using the 2pt method :param ratios: measured urine isotope ratios :param sampledatetime: time and date of urine collections :return: average isotope turnover rate in 1/hr """ turnovers = np.zeros(4) elapsedhours = (timedelta.total_seconds(sampledatetime[4] - sampledatetime[2])) / 3600 turnovers[0] = self.isotope_turnover_2pt(ratios[0], ratios[1], ratios[3], elapsedhours) elapsedhours = (timedelta.total_seconds(sampledatetime[5] - sampledatetime[2])) / 3600 turnovers[1] = self.isotope_turnover_2pt(ratios[0], ratios[1], ratios[4], elapsedhours) elapsedhours = (timedelta.total_seconds(sampledatetime[4] - sampledatetime[3])) / 3600 turnovers[2] = self.isotope_turnover_2pt(ratios[0], ratios[2], ratios[3], elapsedhours) elapsedhours = (timedelta.total_seconds(sampledatetime[5] - sampledatetime[3])) / 3600 turnovers[3] = self.isotope_turnover_2pt(ratios[0], ratios[2], ratios[4], elapsedhours) return np.nanmean(turnovers)
def send(self, port, event): from datetime import datetime, timedelta starttime = datetime.now() raw_tuple = mk_raw(event, port) alsaseq.output(raw_tuple) elapsed = datetime.now() - starttime if elapsed > timedelta(microseconds=1000): print "MIDI send took %s milliseconds" % timedelta.total_seconds() * 1000
def main(): poison_ips(gateway_ip) while True: try: now = datetime.now() if td.total_seconds(now - then) > 5: poison_ips(gateway_ip) then = now except KeyboardInterrupt: print("> Ending ARP poisoning attack.") return
def Add(self, hashtags, timestamp): self.__hashtags.append(hashtags) self.__timestamp.append(timestamp) self.__counter += 1 # Only consider hashtags if it's more than 1 if len(hashtags) > 1: for k in range(len(hashtags)): # Adds hashtags in cyclical manner to the graph dictionary tempkey = hashtags.popleft() templist = list(copy.copy(hashtags)) if tempkey not in self.__graphs: #Initial count is 1 for each self.__graphs[tempkey] = Counter(templist) self.__sum += len(templist) # Keep count for the total graph nodes self.__denom += 1 else: for l in range(len(templist)): if templist[l] not in self.__graphs[tempkey]: self.__sum += 1 self.__graphs[tempkey] += Counter(templist) # Add the hashtags back to retain the cyclical manner hashtags.append(tempkey) while timedelta.total_seconds(timestamp - self.__timestamp[0]) > self.__window: self.__timestamp.popleft() hashtags = self.__hashtags.popleft() #Only consider if hashtags length is more than 1 if len(hashtags) > 1: for k in range(len(hashtags)): # Remove hashtags in cyclical manner to the graph dictionary tempkey = hashtags.popleft() templist = list(copy.copy(hashtags)) self.__graphs[tempkey] -= Counter(templist) for l in range(len(templist)): if self.__graphs[tempkey][templist[l]] == 0: self.__sum -= 1 if len(self.__graphs[tempkey]) == 0: del self.__graphs[tempkey] self.__denom -= 1 hashtags.append(tempkey) if self.__denom > 0: slidingavg = float(self.__sum) / self.__denom else: slidingavg = 0.0 return slidingavg
def time_passed(dt): time = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S.%f") now = datetime.now() td = timedelta.total_seconds(now - time) td = int(td) m, s = divmod(td, 60) h, m = divmod(m, 60) uptime = "%d:%02d:%02d" % (h, m, s) return uptime
def calculate_trip_durations_citibike(df): #convert the Start and End Time columns to datetime. df['Start Time']=pd.to_datetime(df['Start Time']) df['Stop Time']=pd.to_datetime(df['Stop Time']) #Trip Duration is End - Start time. This will result in datetime.timedelta objects, stored in the 'Trip Duration' column. df['Trip Duration']=df['Stop Time'] - df['Start Time'] #This is still timedelta. #Convert datetime.timedelta object Trip Duration to floating point number of minutes for ease of plotting. df['Trip Duration Minutes']=df['Trip Duration'].apply(lambda x: timedelta.total_seconds(x)/60.0) return df
def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() time_of_working = timedelta.total_seconds(timedelta(end - start)) if class_name: print(class_name, end=" ") print(func.__name__, "started") if class_name: print(class_name, end=" ") print(func.__name__, "finished in", time_of_working, "\n") return result
def Add(self,hashtags,timestamp): self.__hashtags.append(hashtags) self.__timestamp.append(timestamp) self.__counter += 1 # Only consider hashtags if it's more than 1 if len(hashtags) > 1: for k in range(len(hashtags)): # Adds hashtags in cyclical manner to the graph dictionary tempkey = hashtags.popleft() templist = list(copy.copy(hashtags)) if tempkey not in self.__graphs: #Initial count is 1 for each self.__graphs[tempkey] = Counter(templist) self.__sum += len(templist) # Keep count for the total graph nodes self.__denom += 1 else: for l in range(len(templist)) : if templist[l] not in self.__graphs[tempkey]: self.__sum += 1 self.__graphs[tempkey] += Counter(templist) # Add the hashtags back to retain the cyclical manner hashtags.append(tempkey) while timedelta.total_seconds(timestamp - self.__timestamp[0]) > self.__window: self.__timestamp.popleft() hashtags = self.__hashtags.popleft() #Only consider if hashtags length is more than 1 if len(hashtags) > 1: for k in range(len(hashtags)): # Remove hashtags in cyclical manner to the graph dictionary tempkey = hashtags.popleft() templist = list(copy.copy(hashtags)) self.__graphs[tempkey] -= Counter(templist) for l in range(len(templist)) : if self.__graphs[tempkey][templist[l]] == 0: self.__sum -= 1 if len(self.__graphs[tempkey]) == 0: del self.__graphs[tempkey] self.__denom -= 1 hashtags.append(tempkey) if self.__denom > 0 : slidingavg = float(self.__sum)/self.__denom else: slidingavg = 0.0 return slidingavg
def getClientInfo(self, ip, mac): info = dict() if models.Whitelist.objects.filter(MAC_Address=mac).exists(): whitelisted_flg = True status = 'Connected' time_left = timedelta(0) total_coins = 0 notif_id = '' vouchers = None else: whitelisted_flg = False client, created = models.Clients.objects.get_or_create(MAC_Address=mac, defaults={'IP_Address': ip}) if not created: if client.IP_Address != ip: client.IP_Address = ip client.save() try: coin_queue = models.CoinQueue.objects.get(Client=mac) total_coins = coin_queue.Total_Coins except ObjectDoesNotExist: total_coins = 0 try: vouchers = models.Vouchers.objects.filter(Voucher_client=mac, Voucher_status='Not Used') except ObjectDoesNotExist: vouchers = None status = client.Connection_Status if status == 'Connected': time_left = client.running_time elif status == 'Disconnected': time_left = timedelta(0) elif status == 'Paused': time_left = client.Time_Left notif_id = client.Notification_ID info['ip'] = ip info['mac'] = mac info['whitelisted'] = whitelisted_flg info['status'] = status info['time_left'] = int(timedelta.total_seconds(time_left)) info['total_coins'] = total_coins info['vouchers'] = vouchers info['appNotification_ID'] = notif_id return info
def avg_intercept_dilution_space(self, doseweight, molmass, doseenrichment, turnoverrate, ratios, sampledatetime): """Calculate the average of the 4 and 5 hour dilution spaces calculated by the intercept method :param doseweight: weight of the does in grams :param molmass: moleular mass of the dose in grams/mol :param doseenrichment: enrichment of the dose in ppm :param turnoverrate: isotope turnover rate in 1/hr :param ratios: measured urine isotope ratios :param sampledatetime: time and date of urine collections :return: average of the 4 and 5 hour dilution spaces calculated by the intercept method in mol """ dilspace = np.zeros(2) dosetime = np.zeros(2) dosetime[0] = timedelta.total_seconds(sampledatetime[1] - sampledatetime[0]) / 3600 dilspace[0] = self.dilution_space_intercept(doseweight, molmass, doseenrichment, ratios[1], ratios[0], turnoverrate, dosetime[0]) dosetime[1] = timedelta.total_seconds(sampledatetime[2] - sampledatetime[0]) / 3600 dilspace[1] = self.dilution_space_intercept(doseweight, molmass, doseenrichment, ratios[2], ratios[0], turnoverrate, dosetime[1]) return np.mean(dilspace)
def get_age_seconds(old_time, new_time=None): if old_time is None: raise ValueError('old time cannot be None for getting age - found [%s]' % old_time) datetime1 = datetime.strptime(old_time, OA_TIME_FORMAT) if new_time is None: datetime2 = datetime.now() else: datetime2 = datetime.strptime(new_time, OA_TIME_FORMAT) timedelta = datetime1 - datetime2 return timedelta.total_seconds()
def test_session_expiry_date(self): """ Test the expiry date is set correct """ self.session.load() ndb_session_key = ndb.Key(Session,self.session.session_key) s = ndb_session_key.get() now = datetime.utcnow() timedelta = s.expire_date - now self.assertTrue((settings.SESSION_COOKIE_AGE - timedelta.total_seconds()) < 1)
def tdelta_f_string(self, start=str, end=str): # timedelta from string ''' Oblicza roznice czasu z typu string i zwraca jako datetime object i roznice w sekundach''' from datetime import timedelta try: s = datetime.datetime.strptime(str(start), '%b %d %Y %H:%M:%S') e = datetime.datetime.strptime(str(end), '%b %d %Y %H:%M:%S') return e - s, timedelta.total_seconds(e - s) # difference, in seconds except: print 'Input format example: Jun 1 2005 1:33:02,Jun 2 2005 13:33:03'
def scan_for_upcoming_recordings(self): threshold = '10 minutes' logger.info('Scanning for upcoming recordings, threshold: {0}'.format(threshold)) self.execute('SELECT recording.id AS recording_id, lineup_station.atsc, station_program.air_date_time, station_program.duration, recording.media_path FROM recording JOIN station_program ON station_program.id = recording.station_program_id JOIN lineup_station ON lineup_station.station_id = station_program.station_id WHERE recording.status = \'pending\' AND station_program.air_date_time <= (CURRENT_TIMESTAMP + INTERVAL \'{0}\') AND station_program.air_date_time >= CURRENT_TIMESTAMP ORDER BY station_program.air_date_time'.format(threshold)) for row in self.fetchall(): start_time = row.air_date_time - timedelta(seconds = self.start_early_sec) duration_sec = self.start_early_sec + row.duration + self.end_late_sec script_path = self.generate_recording_script(row.recording_id, row.atsc, duration_sec, row.media_path) self.set_recording_status(row.recording_id, 'scheduled') sec_until = timedelta.total_seconds(start_time - datetime.now()) logger.info('Scheduling execution of {0} in {1} seconds'.format(script_path, sec_until)) Timer(sec_until, self.invoke_script, [script_path]).start()
def tdelta_f_dt(self, start=datetime.datetime, end=datetime.datetime): # timedelta from datetime ''' Oblicza roznice czasu z typu datetime i zwraca jako datetime object i roznice w sekundach''' from datetime import timedelta try: return end - start, timedelta.total_seconds( end - start) # difference, in seconds except: print 'Input format example: Jun 1 2005 1:33:02,Jun 2 2005 13:33:03'
def bounce(request, code): notification = get_object_or_404(Notification, code=code) # If webhook is more than 10 minutes late, don't accept it: td = timezone.now() - notification.created if td.total_seconds() > 600: return HttpResponseForbidden() notification.error = request.body[:200] notification.save() return HttpResponse()
def readFromPairBuilder(scorer_id): print(scorer_id) consumer_c3 = KafkaConsumer("pairs" + str(scorer_id), bootstrap_servers=['localhost:9092'], group_id='apoorva-thesis') print("Consumer is ready to listen!") temp = 0 currScore = -1 prof_ctr = 10 for msg in consumer_c3: #(mgfID, fastaID), load, time in ms if "__final__" in msg.key: print "All received... Shutting down cassandra connection" cass_session.shutdown() sys.exit(0) if "__init__" not in msg.key: prof_ctr -= 1 if prof_ctr <= 0: # Profile block 2 if profile: pr.disable() s = cStringIO.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print s.getvalue() # End of profile block 2 prof_ctr = 10 pairdata = eval(str(msg.value)) print "Received Pair: ", pairdata preTime = dt.now() loadRealData(pairdata[0], pairdata[1]) try: currScore = calculateScore() st = "Final score of pair " + str(temp) + " is: " + str( currScore) + " at time " + str(dt.now()) print st #f = open('output/output_results.txt', 'a') #f.write('\n' + st) #f.close() except Exception as e: print "Error occured in pair ", temp, "....Hence skipped\n", str( e) #currScore = setVariables(pairdata,temp) temp += 1 postTime = dt.now() sendScores( pairdata[0], pairdata[1], currScore, pairdata[3] + timedelta.total_seconds(postTime - preTime))
def getDiffTempDict(res): diffTempDict = DiffTempHoraDict() i = 0 for i in range(0, len(res), 1): if (i > 0): diffTempDict.diferencaTemperatura = round( abs(float(res[i][0]) - float(res[i - 1][0])), 1) diffTempDict.diferencaMin = int( round( abs( int(timedelta.total_seconds(res[i][1] - res[i - 1][1])) / 60), 0)) return vars(diffTempDict)
def pretty_timedelta(timedelta): seconds = int(timedelta.total_seconds()) days, seconds = divmod(seconds, 86400) hours, seconds = divmod(seconds, 3600) minutes, seconds = divmod(seconds, 60) if days > 0: return '%dd%dh%dm%ds' % (days, hours, minutes, seconds) elif hours > 0: return '%dh%dm%ds' % (hours, minutes, seconds) elif minutes > 0: return '%dm%ds' % (minutes, seconds) else: return '%ds' % (seconds, )
def get_age_seconds(old_time, new_time=None): if old_time is None: raise ValueError( 'old time cannot be None for getting age - found [%s]' % old_time) datetime1 = datetime.strptime(old_time, OA_TIME_FORMAT) if new_time is None: datetime2 = datetime.now() else: datetime2 = datetime.strptime(new_time, OA_TIME_FORMAT) timedelta = datetime1 - datetime2 return timedelta.total_seconds()
def epoch_to_dt(t): """ Convert epoch date to date time """ # Setup Offsets offset = datetime(1970, 1, 1, 0, 0, 0)-datetime(1900, 1, 1, 0, 0, 0) off_sec = timedelta.total_seconds(offset) # Convert using offsets t_datetime = [] for tt in t: t_sec = tt - off_sec t_datetime.append(datetime.utcfromtimestamp(t_sec)) return t_datetime
async def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = timedelta.total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) ret = await target(*args, **kwargs) if predicate(ret): max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if max_tries_exceeded or max_time_exceeded: await _call_handlers(on_giveup, *details, value=ret) break try: seconds = _next_wait(wait, jitter, elapsed, max_time_) except StopIteration: await _call_handlers(on_giveup, *details, value=ret) break await _call_handlers(on_backoff, *details, value=ret, wait=seconds) # Note: there is no convenient way to pass explicit event # loop to decorator, so here we assume that either default # thread event loop is set and correct (it mostly is # by default), or Python >= 3.5.3 or Python >= 3.6 is used # where loop.get_event_loop() in coroutine guaranteed to # return correct value. # See for details: # <https://groups.google.com/forum/#!topic/python-tulip/yF9C-rFpiKk> # <https://bugs.python.org/issue28613> await asyncio.sleep(seconds) continue else: await _call_handlers(on_success, *details, value=ret) break return ret
def bounce(request, code): notification = get_object_or_404(Notification, code=code) # If webhook is more than 10 minutes late, don't accept it: td = timezone.now() - notification.created if td.total_seconds() > 600: return HttpResponseForbidden() notification.error = request.body[:200] notification.save() notification.channel.email_verified = False notification.channel.save() return HttpResponse()
def stop_task(request, entry): entry = WorkEntry.objects.get(id=entry) if(entry.workduration_set.filter(isCurrent=True).count() != 1): # There should be exactly one current work duration raise Http404 else: duration = entry.workduration_set.get(isCurrent=True) # get current duration duration.end = timezone('US/Eastern').localize(datetime.now()) duration.isCurrent = False duration.save() entry.isRunning = False timedelta = duration.end - duration.start entry.totalTime += round((timedelta.total_seconds()/3600),2) entry.save() return HttpResponseRedirect('/today')
def parseFile(self, connection, changesetFile, doReplication): parsedCount = 0 startTime = datetime.now() cursor = connection.cursor() context = etree.iterparse(changesetFile) action, root = context.next() for action, elem in context: if(elem.tag != 'changeset'): continue parsedCount += 1 tags = {} for tag in elem.iterchildren(tag='tag'): tags[tag.attrib['k']] = tag.attrib['v'] comments = [] for discussion in elem.iterchildren(tag='discussion'): for commentElement in discussion.iterchildren(tag='comment'): comment = dict() comment['uid'] = commentElement.attrib.get('uid') comment['user'] = commentElement.attrib.get('user') comment['date'] = commentElement.attrib.get('date') for text in commentElement.iterchildren(tag='text'): comment['text'] = text.text comments.append(comment) if(doReplication): self.deleteExisting(connection, elem.attrib['id']) self.insertNew(connection, elem.attrib['id'], elem.attrib.get('uid', None), elem.attrib['created_at'], elem.attrib.get('min_lat', None), elem.attrib.get('max_lat', None), elem.attrib.get('min_lon', None), elem.attrib.get('max_lon', None),elem.attrib.get('closed_at', None), elem.attrib.get('open', None), elem.attrib.get('num_changes', None), elem.attrib.get('user', None), tags, comments) if((parsedCount % 10000) == 0): print "parsed %s" % ('{:,}'.format(parsedCount)) print "cumulative rate: %s/sec" % '{:,.0f}'.format(parsedCount/timedelta.total_seconds(datetime.now() - startTime)) #clear everything we don't need from memory to avoid leaking elem.clear() while elem.getprevious() is not None: del elem.getparent()[0] connection.commit() print "parsing complete" print "parsed {:,}".format(parsedCount)
def send(self, port, event): from datetime import datetime, timedelta starttime = datetime.now() raw_tuple = mk_raw(event, port) try: alsaseq.output(raw_tuple) except Exception as e: log.debug("alsaseq: error on output of", raw_tuple, e) log.debug_traceback() elapsed = datetime.now() - starttime if elapsed > timedelta(microseconds=1000): print "MIDI send took %s milliseconds" % timedelta.total_seconds() * 1000
def test_graph(G): start_time = datetime.now() n = len(G.nodes()) skeletons = [KS(1, n), KS(2, n), KS(3, n), KS(4, n), KS(5, n)] for edge in G.edges(): for skeleton in skeletons: skeleton.add_edge(*edge) end_time = datetime.now() total_time = td.total_seconds(end_time-start_time) print('Graph {} took {} seconds to stream graph.'.format(G.graph['name'], total_time)) # print('Graph was of size V = {}, E = {}.'.format(n, len(G.edges()))) sizes = [] for i in range(0, len(skeletons)): size = asizeof.asizeof(skeletons[i]) sizes.append(size) # print('\t {} bytes used for k = {}.'.format(size, i+1)) # print() return total_time, sizes
def default(self, obj): # For Date Time string spec, see ECMA 262 # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 if isinstance(obj, Promise): return force_text(obj) elif isinstance(obj, datetime.datetime): representation = obj.isoformat() if obj.microsecond: representation = representation[:23] + representation[26:] if representation.endswith('+00:00'): representation = representation[:-6] + 'Z' return representation elif isinstance(obj, datetime.date): return obj.isoformat() elif isinstance(obj, datetime.time): if timezone and timezone.is_aware(obj): raise ValueError("JSON can't represent timezone-aware times.") representation = obj.isoformat() if obj.microsecond: representation = representation[:12] return representation elif isinstance(obj, timedelta): return six.text_type(timedelta.total_seconds(obj)) elif isinstance(obj, decimal.Decimal): # Serializers will coerce decimals to strings by default. return FakeFloat(obj) elif isinstance(obj, uuid.UUID): return six.text_type(obj) elif isinstance(obj, QuerySet): return tuple(obj) elif hasattr(obj, 'tolist'): # Numpy arrays and array scalars. return obj.tolist() elif hasattr(obj, '__getitem__'): try: return dict(obj) except: pass elif hasattr(obj, '__iter__'): return tuple(item for item in obj) return super(JSONEncoder, self).default(obj)
def playbook_on_stats(self, stats): self.play = self.playbook.filename res = dict([(h, stats.summarize(h)) for h in stats.processed]) end_time = datetime.now() timedelta = end_time - self.start_time duration = timedelta.total_seconds() res.update({"start":str(self.start_time)}) res.update({"end":str(end_time)}) res.update({"play_duration":duration}) if self.current is not None and self.current != "NULL": # Record the timing of the very last task self.stats[self.current] = time.time() - self.stats[self.current] res.update({"role_duration":self.stats}) json_log(res, self.uuid, self.play, self.role, None,'Play Completed')
def retry(*args, **kwargs): # change names because python 2.x doesn't have nonlocal max_tries_ = _maybe_call(max_tries) max_time_ = _maybe_call(max_time) tries = 0 start = datetime.datetime.now() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 elapsed = timedelta.total_seconds(datetime.datetime.now() - start) details = (target, args, kwargs, tries, elapsed) ret = target(*args, **kwargs) if predicate(ret): max_tries_exceeded = (tries == max_tries_) max_time_exceeded = (max_time_ is not None and elapsed >= max_time_) if max_tries_exceeded or max_time_exceeded: _call_handlers(on_giveup, *details, value=ret) break try: seconds = _next_wait(wait, jitter, elapsed, max_time_) except StopIteration: _call_handlers(on_giveup, *details) break _call_handlers(on_backoff, *details, value=ret, wait=seconds) time.sleep(seconds) continue else: _call_handlers(on_success, *details, value=ret) break return ret