Python& MySQL - 在3秒内找到所有行

时间:2017-01-27 20:49:52

标签: python mysql datetime pandas timedelta

我有物理设备是游戏的一部分。玩家获得激活其中一个设备的积分。我试图结合逻辑来标记'骗子'。如果设备在2.5秒范围内发生,则所有激活的设备都算作一个“事件”。我试图识别一个'作弊'玩家的跨度(有人只是快速地一遍又一遍地干扰按钮)。在数据库中,这看起来像多个行具有相同的deviceAddresseventValues为3或4,并且eventTimes在3秒内彼此相同。

我正在努力弄清楚如何在3秒内相互适当地找到事件。以下是我目前收到的错误消息:

if delta <= (timePreviousRow - timedelta(seconds=3)) and devPreviousRow == devThisRow:
TypeError: can't compare datetime.timedelta to datetime.datetime

非常感谢任何帮助。

这是我的MySQL查询,设置为变量cheatFinder

SELECT
eventTime,
deviceAddress,
node_id,
eventType,
eventValue,
sequence
FROM device_data
WHERE eventTime >= DATE_SUB(NOW(), INTERVAL 1 DAY)
AND eventType = 1
AND eventValue BETWEEN 3 AND 4
ORDER BY deviceAddress, eventTime;

以下是前10个结果:

+---------------------+------------------+------------+-----------+------------+----------+
| eventTime           | deviceAddress | node_id | eventType | eventValue | sequence |
+---------------------+------------------+------------+-----------+------------+----------+
| 2017-01-26 21:19:03 |           776128 |        221 |         1 | 03         |        3 |
| 2017-01-26 21:48:19 |           776128 |        221 |         1 | 03         |        4 |
| 2017-01-27 06:45:50 |           776128 |        221 |         1 | 04         |       18 |
| 2017-01-27 12:41:03 |           776128 |        221 |         1 | 03         |       24 |
| 2017-01-26 23:03:18 |          6096372 |        301 |         1 | 03         |      119 |
| 2017-01-27 00:21:47 |          6096372 |        301 |         1 | 03         |      120 |
| 2017-01-26 23:50:27 |         27038894 |        157 |         1 | 03         |      139 |
| 2017-01-27 01:19:42 |         29641083 |        275 |         1 | 03         |      185 |
| 2017-01-27 00:10:13 |         30371381 |        261 |         1 | 03         |       82 |
| 2017-01-27 00:53:45 |         30371381 |        261 |         1 | 03         |       87 |
+---------------------+------------------+------------+-----------+------------+----------+

这是有问题的Python方法:

import mysql.connector
import pandas
from datetime import datetime,timedelta

. . .     

def findBloodyCheaters(self):
    self.cursor.execute(cheatFinder)
    resultSet = self.cursor.fetchall()
    targetDF = pandas.DataFrame(columns=['eventTime', 'deviceAddress', 'gateway_id', 'eventType', 'eventValue',
                                         'sequence'])
    timePreviousRow = None
    devPreviousRow = None
    for row in resultSetRaw:
        if timePreviousRow is None and devPreviousRow is None:
            timePreviousRow = row[0]
            devPreviousRow = row[1]
        elif timePreviousRow is not None and devPreviousRow is not None:
            timeThisRow = row[0]
            devThisRow = row[1]
            delta = timePreviousRow - timeThisRow
            print(delta)
            if delta <= (timePreviousRow - timedelta(seconds=3)) and devPreviousRow == devThisRow:
                targetDF.append(row)
            timePreviousRow = timeThisRow
            devPreviousRow = devThisRow
            print(targetDF)

1 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为您尝试将delta timedelta对象(timePreviousRow - timedelta(seconds=3))datetime对象进行比较。我会尝试将deltatimedelta(seconds=3)进行比较,以便您的代码看起来像:

if delta <= timedelta(seconds=3) and devPreviousRow == devThisRow: