用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抓取京东图书评论数据
Aug 31 Python
Python环境变量设置方法
Aug 28 Python
解决python读取几千万行的大表内存问题
Jun 26 Python
Python I/O与进程的详细讲解
Mar 08 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 Python
Django REST framework 单元测试实例解析
Nov 07 Python
Python模块future用法原理详解
Jan 20 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
Apr 14 Python
python多线程semaphore实现线程数控制的示例
Aug 10 Python
Pycharm快捷键配置详细整理
Oct 13 Python
基于Python爬取京东双十一商品价格曲线
Oct 23 Python
Python实现视频中添加音频工具详解
Dec 06 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
通过html表格发电子邮件
2006/10/09 PHP
php str_replace的替换漏洞
2008/03/15 PHP
php 静态页面中显示动态内容
2009/08/14 PHP
遍历指定目录下的所有目录和文件的php代码
2011/11/27 PHP
用php随机生成福彩双色球号码的2种方法
2013/02/04 PHP
php代码书写习惯优化小结
2013/06/20 PHP
ThinkPHP中的关联模型注意点
2014/06/16 PHP
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间
2016/05/06 PHP
php htmlentities()函数的定义和用法
2016/05/13 PHP
php for 循环使用的简单实例
2016/06/02 PHP
Laravel中如何增加自定义全局函数详解
2017/05/09 PHP
PHP中将一个字符串部分字符用星号*替代隐藏的实现代码
2019/09/08 PHP
仅IE9/10同时支持script元素的onload和onreadystatechange事件分析
2011/04/27 Javascript
JS实现局部选择打印和局部不选择打印
2014/04/03 Javascript
不想让浏览器运行javascript脚本的方法
2015/11/20 Javascript
jQuery防止重复绑定事件的解决方法
2016/05/14 Javascript
javascript中的面向对象
2017/03/30 Javascript
vue.js根据代码运行环境选择baseurl的方法
2018/02/28 Javascript
JavaScript函数Call、Apply原理实例解析
2020/02/17 Javascript
JavaScript编码小技巧分享
2020/09/17 Javascript
[01:02:04]EG vs Liquid 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.23
2019/09/05 DOTA
python中利用Future对象回调别的函数示例代码
2017/09/07 Python
python ---lambda匿名函数介绍
2019/03/13 Python
django 通过URL访问上传的文件方法
2019/07/28 Python
关于numpy.where()函数 返回值的解释
2019/12/06 Python
详解Python IO编程
2020/07/24 Python
css3背景_动力节点Java学院整理
2017/07/11 HTML / CSS
CSS3 绘制BMW logo实的现代码
2013/04/25 HTML / CSS
Linux如何为某个操作添加别名
2013/03/01 面试题
酒店管理专业毕业生推荐信
2013/11/10 职场文书
法人代表证明书
2014/09/18 职场文书
中学生检讨书范文
2014/11/03 职场文书
民间借贷纠纷起诉书
2015/08/03 职场文书
利用Nginx代理如何解决前端跨域问题详析
2021/04/02 Servers
动画「半妖的夜叉姬」新BD特典图公开
2022/03/22 日漫
【DOTA2】半决赛强强对话~ PSG LGD vs EHOME - DPC 2022 CN REGIONAL FINALS WINTER
2022/04/02 DOTA