B
B
bychok3002017-03-08 12:33:13
Python
bychok300, 2017-03-08 12:33:13

What is the best way to calculate time?

There is such a piece of code that displays the speed of the cursor and the time when the cursor moved. The task is to calculate the total time the cursor has moved since the code was launched

import sys 
import math
import time
from datetime import datetime


from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from toolz.itertoolz import second

# scriptStartAt = datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S")
# print('Script start at : ', scriptStartAt)

#time.sleep(3)

class Frame:
    def __init__(self, position, time):
        self.position = position
        self.time = time

    def speed(self, frame):
        d = distance(*self.position, *frame.position)
        time_delta = abs(frame.time - self.time)
        return d / time_delta

def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1)**2 + (y2-y1)**2)

def get_current_cursor_position():
    pos = QCursor.pos()
    return pos.x(), pos.y()

def get_current_frame():
    return Frame(get_current_cursor_position(), time.time())

#спрашиваем время каждую минуту
# def check_last_time():
#     nowTime = datetime.now()
#     print('Script still runing :', nowTime)
#     time.sleep(60)

if __name__ == '__main__':
    
    
    
    app = QApplication(sys.argv)

    last_frame = get_current_frame()

    while True:
        #объясвляем время
        nowTime = datetime.now()
        mouseWasMoveAt = nowTime.strftime('%H:%M:%S')
        new_frame = get_current_frame()
        
        if new_frame.speed(last_frame) != 0:
            print(mouseWasMoveAt)
            last_frame = new_frame
            
            time.sleep(0.07)

on the output we have the following values:
24.923661403031307 12:29:06
387.6390624573542 12:29:07
394.4926824122518 12:29:07
364.6319448618099 12:29:07
265.4365222073813 12:29:07
130.94569163049468 12:29:07
2.2565370492799803 12:29:09
2755.4110208927873 12:29:09
4424.753655893077 12:29:09
1333.456662360851 12:29:09
378.76671822675877 12:29:09

I thought about deleting all lines where the time is the same, then each line will be equal to 1 second and just count the number of lines. But I don’t really want to describe this deletion of lines, then summation, it will take a long time. Is there an easier way to do this? I think maybe a timer of some kind? so that when it moves, it starts counting and when the cursor stops, it stops. But if all this is output to the console, then there will be many increasing seconds and it will be even more difficult to count.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question