用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通过BF算法实现关键词匹配的方法
Mar 13 Python
python中getaddrinfo()基本用法实例分析
Jun 28 Python
Python 搭建Web站点之Web服务器网关接口
Nov 06 Python
python版本的仿windows计划任务工具
Apr 30 Python
Python 读取某个目录下所有的文件实例
Jun 23 Python
python2.7和NLTK安装详细教程
Sep 19 Python
如何通过python实现人脸识别验证
Jan 17 Python
使用sklearn对多分类的每个类别进行指标评价操作
Jun 11 Python
python else语句在循环中的运用详解
Jul 06 Python
python中Mako库实例用法
Dec 31 Python
python基础之类属性和实例属性
Oct 24 Python
利用Python将list列表写入文件并读取的方法汇总
Mar 25 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文本数据库的搜索方法
2006/10/09 PHP
谈谈PHP语法(5)
2006/10/09 PHP
40个迹象表明你还是PHP菜鸟
2008/09/29 PHP
列举PHP的Yii 2框架的开发优势
2015/07/03 PHP
PHP+Mysql分布式事务与解决方案深入理解
2021/02/27 PHP
jquery简单体验
2007/01/10 Javascript
How to Auto Include a Javascript File
2007/02/02 Javascript
node.js集成百度UE编辑器
2015/02/05 Javascript
JS弹出可拖拽可关闭的div层完整实例
2015/02/13 Javascript
JavaScript字符串常用类使用方法汇总
2015/04/14 Javascript
jquery 中ajax执行的优先级
2015/06/22 Javascript
JSONObject使用方法详解
2015/12/17 Javascript
vue中页面跳转拦截器的实现方法
2017/08/23 Javascript
(模仿京东用户注册)用JQuery实现简单表单验证,初学者必看
2018/01/08 jQuery
Vue 自定义标签的src属性不能使用相对路径的解决
2019/09/17 Javascript
JS严格模式原理与用法实例分析
2020/04/27 Javascript
iview实现动态表单和自定义验证时间段重叠
2021/01/10 Javascript
[54:25]Ti4 循环赛第三日LGD vs MOUZ
2014/07/12 DOTA
浅谈Tensorflow模型的保存与恢复加载
2018/04/26 Python
解决python3 Pycharm上连接数据库时报错的问题
2018/12/03 Python
python之验证码生成(gvcode与captcha)
2019/01/02 Python
详解python 爬取12306验证码
2019/05/10 Python
Python动态参数/命名空间/函数嵌套/global和nonlocal
2019/05/29 Python
Python使用OpenPyXL处理Excel表格
2020/07/02 Python
罗德与泰勒百货官网:Lord & Taylor
2016/08/12 全球购物
秘鲁购物网站:Linio秘鲁
2017/04/07 全球购物
企业文化建设实施方案
2014/03/22 职场文书
员工试用期自我评价
2014/09/18 职场文书
单位委托书怎么写
2014/09/21 职场文书
老员工辞职信范文
2015/05/12 职场文书
心灵捕手观后感
2015/06/02 职场文书
英语读书笔记
2015/07/02 职场文书
Nginx 过滤静态资源文件的访问日志的实现
2021/03/31 Servers
pytest配置文件pytest.ini的详细使用
2021/04/17 Python
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python