一个简单但有用的秒表库。

swp的Python项目详细描述


swpy:一个简单而实用的python秒表库

PyPI - Python VersionPyPICircleCIcodecovPyPI - License

要求

  • Python3.6+

安装

只需使用pip安装。

pip install swpy

用法

基本用法

swpy导入Timer类,并使用with语句将块封装到需要计时的位置。

fromswpyimportTimerfromtimeimportsleepwithTimer():sleep(1)

或者使用start()stop()函数来控制计时器。

t=Timer()t.start()sleep(1)t.stop()

输出如下:

[timer-1557406243.3309178] started.
[timer-1557406243.3309178] finish time: 1.00 sec.

功能

命名计时器

你可以给计时器起个名字让它容易辨认。

withTimer(name='test timer'):sleep(1)

现在计时器被重命名为test timer

[test timer] started.
[test timer] finish time: 1.00 sec.

圈速和分割时间

测量不停车时间有两种类型:单圈时间和分段时间。下图可能有助于您理解这些差异。

split_lap_time

我们为这种用法准备了splitlap函数。示例如下。

# measure split timewithTimer('timer')ast:sleep(1)t.split()sleep(1)t.split()sleep(1)t.split()
# outptus
[timer] started.
[timer] split time:  1.00 sec.
[timer] split time:  2.01 sec.
[timer] split time:  3.01 sec.
[timer] finish time: 3.01 sec.
# measure lap timewithTimer('timer')ast:sleep(1)t.lap()sleep(1)t.lap()sleep(1)t.lap()
# outputs
[timer] started.
[timer] lap time:    1.00 sec.
[timer] lap time:    1.01 sec.
[timer] lap time:    1.00 sec.
[timer] finish time: 3.01 sec.

在一次跑步中测量多个任务的情况下,你可以将你的圈速/分程时间命名如下。

withTimer('task timer')ast:task1()t.lap('task1')task2()t.lap('task2')
# outputs
[task timer] started.
[task timer] [task1] lap time:    3.69 sec.
[task timer] [task2] lap time:    4.21 sec.
[task timer] finish time: 7.91 sec.

使用自己的记录器

您可以使用自己的记录器,而不是默认的print

fromlogzeroimportloggerimportloggingwithTimer(name='test timer',logger=logger,level=logging.DEBUG):sleep(1)

它将使用记录器输出。

[D 190510 14:41:59 swpy:15] [test timer] started.
[D 190510 14:42:00 swpy:15] [test timer] finish time: 1.01 sec.

定义自己的回调

有时,我们想在工作完成后做一些事情,比如通知结果松弛,执行下一个进程等等。回调功能将帮助您做到这些。

# define a slack notification functionimportrequests,jsondefsend_slack(msg):requests.post(SLACK_URL,json.dumps({'text':msg}))# just specify the callback argumentwithTimer(name='experiment-1',callback=send_slack):sleep(1)

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在数据库中插入行时自动发送电子邮件?   从进程输出读取的Java问题   java质疑StyledDocument和JTextPane之间的关系,以及接口的正确使用   java错误getPlayer(args[0]);   java如何使Spring引导在重新打包的WAR中包含清单文件?   Java中的除法与模   java使用2d数组和JfreeChart制作散点图   java扩展SonarQube FindBugs插件和自定义FindBugs插件   javaspring:hibernate+ehcache   具有不正确的equals和HashCode实现的java HashMap   java Jaspersoft报告网。旧金山。jasperreports。发动机例外:net。旧金山。jasperreports。发动机填满JRepressionEvalException:计算表达式时出错   java如果输入与其变量不匹配,如何添加错误   在java中使用简单数组[]实现队列   无法启动上下文路径/hsx上的java FAIL应用程序