用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中split方法用法分析
Apr 17 Python
Python中的rfind()方法使用详解
May 19 Python
深入理解NumPy简明教程---数组1
Dec 17 Python
python+selenium实现自动抢票功能实例代码
Nov 23 Python
Python操作redis和mongoDB的方法
Dec 19 Python
Python模拟FTP文件服务器的操作方法
Feb 18 Python
Python3 shutil(高级文件操作模块)实例用法总结
Feb 19 Python
基于python连接oracle导并出数据文件
Apr 28 Python
python如何调用java类
Jul 05 Python
详解python metaclass(元类)
Aug 13 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
Flask中jinja2的继承实现方法及实例
Mar 03 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函数utf8转gb2312编码
2006/12/21 PHP
php实现的ping端口函数实例
2014/11/12 PHP
如何在Laravel5.8中正确地应用Repository设计模式
2019/11/26 PHP
PHP实现一个按钮点击上传多个图片操作示例
2020/01/23 PHP
PHP8.0新功能之Match表达式的使用
2020/07/19 PHP
PHP反射基础知识回顾
2020/09/10 PHP
根据对象的某一属性进行排序的js代码(如:name,age)
2010/08/10 Javascript
跟我学习javascript的call(),apply(),bind()与回调
2015/11/16 Javascript
详解js中class的多种函数封装方法
2016/01/03 Javascript
Vue开发中整合axios的文件整理
2017/04/29 Javascript
详解react如何在组件中获取路由参数
2017/06/15 Javascript
Vue.js常用指令的使用小结
2017/06/23 Javascript
微信小程序获取手机号授权用户登录功能
2017/11/09 Javascript
JavaScript字符和ASCII实现互相转换
2020/06/03 Javascript
如何使用JavaScript实现无缝滚动自动播放轮播图效果
2020/08/20 Javascript
Python中3种内建数据结构:列表、元组和字典
2014/11/30 Python
24式加速你的Python(小结)
2019/06/13 Python
Python 创建TCP服务器的方法
2020/07/28 Python
在pycharm创建scrapy项目的实现步骤
2020/12/01 Python
英国Boots旗下太阳镜网站:Boots Designer Sunglasses
2018/07/07 全球购物
Radley英国官网:英国莱德利小狗包
2019/03/21 全球购物
美国购物网站:Clickhere2shop
2021/01/28 全球购物
梅西百货官网:Macy’s
2020/08/04 全球购物
带薪年假请假条
2014/02/04 职场文书
职工运动会感言
2014/02/07 职场文书
工程承包协议书
2014/04/22 职场文书
根叔历年演讲稿
2014/05/20 职场文书
平安建设工作方案
2014/06/02 职场文书
美术专业自荐信
2014/07/07 职场文书
司法工作人员群众路线对照检查材料思想汇报
2014/09/30 职场文书
先进党组织事迹材料
2014/12/26 职场文书
地陪导游欢迎词
2015/01/26 职场文书
大学生毕业个人总结
2015/02/15 职场文书
就业推荐表自我评价范文
2015/03/02 职场文书
上甘岭观后感
2015/06/10 职场文书
信息技术远程培训心得体会
2016/01/09 职场文书