用Python实现命令行闹钟脚本实例
前言:
这篇文章给大家介绍了怎样用python创建一个简单的报警,它可以运行在命令行终端,它需要分钟做为命令行参数,在这个分钟后会打印”wake-up”消息,并响铃报警,你可以用0分钟来测试,它会立即执行,用扬声器控制面板调整声音。
以下是脚本:
#alarm_clock.py #Description:AsimplePythonprogramtomakethecomputeract #likeanalarmclock.Startitrunningfromthecommandline #withacommandlineargumentspecifyingthedurationinminutes #afterwhichtosoundthealarm.Itwillsleepforthatlong, #andthenbeepafewtimes.Useadurationof0totestthe #alarmimmediiately,e.g.forcheckingthatthevolumeisokay. #Author:VasudevRam-http://www.dancingbison.com importsys importstring fromtimeimportsleep sa=sys.argv lsa=len(sys.argv) iflsa!=2: print"Usage:[python]alarm_clock.pyduration_in_minutes" print"Example:[python]alarm_clock.py10" print"Useavalueof0minutesfortestingthealarmimmediately." print"Beepsafewtimesafterthedurationisover." print"PressCtrl-Ctoterminatethealarmclockearly." sys.exit(1) try: minutes=int(sa[1]) exceptValueError: print"Invalidnumericvalue(%s)forminutes"%sa[1] print"Shouldbeaninteger>=0" sys.exit(1) ifminutes<0: print"Invalidvalueforminutes,shouldbe>=0" sys.exit(1) seconds=minutes*60 ifminutes==1: unit_word="minute" else: unit_word="minutes" try: ifminutes>0: print"Sleepingfor"+str(minutes)+unit_word sleep(seconds) print"Wakeup" foriinrange(5): printchr(7), sleep(1) exceptKeyboardInterrupt: print"Interruptedbyuser" sys.exit(1) #EOF
总结:
这个脚本我工作中已经在使用了,非常实用,当然为避免影响其他同事工作,你最好带耳机,如果要求不高,其实从终端打印出的”wake-up”消息已经足够提醒的了。以上就是这篇文章的全部内容,希望对大家的学习和工作能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对毛票票的支持。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短