用Python实现命令行闹钟脚本实例


Posted in Python onSeptember 05, 2016

前言:

这篇文章给大家介绍了怎样用python创建一个简单的报警,它可以运行在命令行终端,它需要分钟做为命令行参数,在这个分钟后会打印”wake-up”消息,并响铃报警,你可以用0分钟来测试,它会立即执行,用扬声器控制面板调整声音。

以下是脚本:

# alarm_clock.py
 
# Description: A simple Python program to make the computer act 
# like an alarm clock. Start it running from the command line 
# with a command line argument specifying the duration in minutes 
# after which to sound the alarm. It will sleep for that long, 
# and then beep a few times. Use a duration of 0 to test the 
# alarm immediiately, e.g. for checking that the volume is okay.
 
# Author: Vasudev Ram - http://www.dancingbison.com
 
import sys
import string
from time import sleep
 
sa = sys.argv
lsa = len(sys.argv)
if lsa != 2:
  print "Usage: [ python ] alarm_clock.py duration_in_minutes"
  print "Example: [ python ] alarm_clock.py 10"
  print "Use a value of 0 minutes for testing the alarm immediately."
  print "Beeps a few times after the duration is over."
  print "Press Ctrl-C to terminate the alarm clock early."
  sys.exit(1)
 
try:
  minutes = int(sa[1])
except ValueError:
  print "Invalid numeric value (%s) for minutes" % sa[1]
  print "Should be an integer >= 0"
  sys.exit(1)
 
if minutes < 0:
  print "Invalid value for minutes, should be >= 0"
  sys.exit(1)
 
seconds = minutes * 60
 
if minutes == 1:
  unit_word = " minute"
else:
  unit_word = " minutes"
 
try:
  if minutes > 0:
    print "Sleeping for " + str(minutes) + unit_word
    sleep(seconds)
  print "Wake up"
  for i in range(5):
    print chr(7),
    sleep(1)
except KeyboardInterrupt:
  print "Interrupted by user"
  sys.exit(1)
 
# EOF

总结:

这个脚本我工作中已经在使用了,非常实用,当然为避免影响其他同事工作,你最好带耳机,如果要求不高,其实从终端打印出的”wake-up”消息已经足够提醒的了。以上就是这篇文章的全部内容,希望对大家的学习和工作能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对三水点靠木的支持。

Python 相关文章推荐
使用python统计文件行数示例分享
Feb 21 Python
python字符串连接方式汇总
Aug 21 Python
Python写的服务监控程序实例
Jan 31 Python
python出现&quot;IndentationError: unexpected indent&quot;错误解决办法
Oct 15 Python
python3+dlib实现人脸识别和情绪分析
Apr 21 Python
python 检查是否为中文字符串的方法
Dec 28 Python
Python实现批量执行同目录下的py文件方法
Jan 11 Python
Pyqt QImage 与 np array 转换方法
Jun 27 Python
python脚本实现音频m4a格式转成MP3格式的实例代码
Oct 09 Python
Python日志logging模块功能与用法详解
Apr 09 Python
python对接ihuyi实现短信验证码发送
May 10 Python
Python实现列表索引批量删除的5种方法
Nov 16 Python
Python爬虫爬取美剧网站的实现代码
Sep 03 #Python
Python选课系统开发程序
Sep 02 #Python
简单谈谈Python中函数的可变参数
Sep 02 #Python
Python实现自动添加脚本头信息的示例代码
Sep 02 #Python
利用Python获取操作系统信息实例
Sep 02 #Python
好用的Python编辑器WingIDE的使用经验总结
Aug 31 #Python
Linux下为不同版本python安装第三方库
Aug 31 #Python
You might like
PHP 最大运行时间 max_execution_time修改方法
2010/03/08 PHP
php记录代码执行时间(实现代码)
2013/07/05 PHP
JSON 入门指南 想了解json的朋友可以看下
2009/08/26 Javascript
alert中断settimeout计时功能
2013/07/26 Javascript
js获取IFRAME当前的URL的方法
2013/11/13 Javascript
javascript和jquery修改a标签的href属性
2013/12/16 Javascript
深入理解JavaScript系列(48):对象创建模式(下篇)
2015/03/04 Javascript
Bootstrap基本组件学习笔记之按钮组(8)
2016/12/07 Javascript
详解vue父子模版嵌套案例
2017/03/04 Javascript
详解angular2实现ng2-router 路由和嵌套路由
2017/03/24 Javascript
详解基于webpack和vue.js搭建开发环境
2017/04/05 Javascript
Vue项目中跨域问题解决方案
2018/06/05 Javascript
video.js 实现视频只能后退不能快进的思路详解
2018/08/09 Javascript
在 Vue 中使用 JSX 及使用它的原因浅析
2020/02/10 Javascript
[01:06:32]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第一局
2016/02/28 DOTA
Python高效编程技巧
2013/01/07 Python
20招让你的Python飞起来!
2016/09/27 Python
python-opencv在有噪音的情况下提取图像的轮廓实例
2017/08/30 Python
python实现扫描ip地址的小程序
2019/04/16 Python
Python常用类型转换实现代码实例
2020/07/28 Python
基于Python的一个自动录入表格的小程序
2020/08/05 Python
基于Python正确读取资源文件
2020/09/14 Python
Python使用Pygame绘制时钟
2020/11/29 Python
html5实现图片转圈的动画效果——让页面动起来
2017/10/16 HTML / CSS
雪花秀美国官方网站:韩国著名草本护肤化妆品品牌
2016/10/19 全球购物
微软马来西亚官方网站:Microsoft马来西亚
2019/11/22 全球购物
标准毕业生自荐信范文
2013/11/04 职场文书
自我鉴定标准格式
2014/03/19 职场文书
2014年党员公开承诺践诺书
2014/03/25 职场文书
体育运动口号
2014/06/09 职场文书
私人委托书格式
2014/09/10 职场文书
2014年后勤工作总结
2014/11/18 职场文书
python中的装饰器该如何使用
2021/06/18 Python
python数字类型和占位符详情
2022/03/13 Python
Vue中使用import进行路由懒加载的原理分析
2022/04/01 Vue.js
关于Redis的主从复制及哨兵问题
2022/06/16 Redis