代码片段非常好,但是有人说,我引用“这是我见过的最奇怪的代码之一”,所以我想要任何关于它的建议。它应该计算我点击鼠标左键的次数。
有什么办法使它更简洁吗?
from pynput.mouse import Listener
import logging
logging.basicConfig(filename='mouseLog.txt', level=logging.DEBUG, format = '%(asctime)s: %(message)s')
mouseCount = 0
#def on_move(x, y):
#print('Mouse moved to ({0}, {1})' .format(x, y))
def on_click(x, y, button, pressed):
global mouseCount
if pressed:
logging.info('Mouse clicked at ({0}, {1}) with {2}' .format(x, y, button))
while pressed == True:
mouseCount += 1
logging.info(f'mouseClick is at {mouseCount}')
break
#def on_scroll(x, y, dx, dy):
#print('Mouse scrolled at ({0}, {1}} ({2}, {3})' .format(x, y, dx, dy))
with Listener(on_click = on_click) as listener:
listener.join()
发布于 2022-11-05 16:53:39
这段代码中最糟糕的部分可能是:
while pressed == True:
# statements
break
循环末尾的break
将停止循环第二次执行,这意味着# statements
将精确执行一次当且仅当pressed == True
。换言之,这可以改写为:
if pressed == True:
mouseCount += 1
logging.info(f'mouseClick is at {mouseCount}')
假设pressed
只接受True
或False
的值( pynput
文档不显式,但不提出相反的建议),那么== True
是多余的,我们可以简单地这样做:
if pressed:
mouseCount += 1
logging.info(f'mouseClick is at {mouseCount}')
现在,就在这之前是另一个if pressed:
语句,而pressed
不能在这两个语句之间更改值,因此可以将它们组合成一个if
语句:
if pressed:
logging.info('Mouse clicked at ({0}, {1}) with {2}' .format(x, y, button))
mouseCount += 1
logging.info(f'mouseClick is at {mouseCount}')
在f'string'
日志语句中使用mouseClick,那么为什么显式.format(…)
调用:
logging.info('Mouse clicked at ({0}, {1}) with {2}' .format(x, y, button))
为什么不…?
logging.info(f'Mouse clicked at ({x}, {y}) with {button}')
…保存17个角色?
尽管如此,logging
模块的编写考虑到了效率。可以将日志记录级别设置为(例如) logging.WARNING
,然后.info(…)
语句将不会生成任何输出。不幸的是,将值内插到格式字符串中的时间被浪费了。
更好的做法是将这些值传递给记录器,如果该记录器启用了给定的日志级别,则让记录器执行格式化操作。
logging.info('Mouse clicked at (%d, %d) with %s', x, y, button)
效率可能并不重要,在这个鼠标按钮点击计数器,因此,可读性改进的f-字符串方法在这里可能更重要。请注意,日志记录可以使效率更高。
Python编码样式指南提供了风格建议,以提高最广泛的读者的可读性。
在Python中任何地方都不推荐bumpyWords
。snake_case
应该用于函数、成员和变量名。(CapWords
代表类名,CAP_WORDS
代表常量)
mouseCount
违反了这个命名。它是一个变量,应该命名为mouse_count
。当我们重命名它时,计算机可能只有一个鼠标,也许应该命名为mouse_press_count
、press_count
或click_count
。
=
有时是一个运算符,应该用空格包围:
mouseCount = 0
在函数调用中,它被用作关键字参数标识符,不应该使用空白,例如filename='mouseLog.txt'
和level=logging.DEBUG
。
您违反了后者与format = '%(asctime)s: %(message)s'
和on_click = on_click
。=
的两边都不应该有空间。
源代码应该分组到相关的块中。import
语句首先是全局常量和变量,然后是函数,然后是主线代码。logging.basicConfig(…)
是在定义函数之前执行的语句。这是不正常的。
通常,主线代码放在主保护程序中,甚至是主保护程序调用的main()
函数中。
from pynput.mouse import Listener
import logging
LOG = logging.getLogger(__name__)
click_count = 0
def on_click(x, y, button, pressed):
global click_count
if pressed:
LOG.info(f'Mouse clicked at ({x}, {y}) with {button}')
click_count += 1
LOG.info(f'mouseClick is at {click_count}')
def main():
logging.basicConfig(filename='mouseLog.txt', level=logging.DEBUG,
format='%(asctime)s: %(message)s')
with Listener(on_click=on_click) as listener:
listener.join()
if __name__ == '__main__':
main()
应该添加类型提示和文档字符串,以提高可读性.你应该学会这些。
不允许使用global
语句修改全局变量。这可以用单击计数器class
对象代替,其中click_count
是对象的成员。所以,你也应该学会使用类。
https://codereview.stackexchange.com/questions/280988
复制相似问题