2005-09-28 "土制闹钟"

{{{发件人: Kai <[email protected]> 回复: [email protected] 收件人: "[email protected]" <[email protected]> 日期: 2005-9-27 下午11:48 主题: [python-chinese] 我的土制闹钟 }}} 为了避免连续长时间看电脑,能让可怜的眼睛休息一下,我凑合出这个python程序(完全新手啊,汗,大伙给改改。而且还缺少unix上的播放命令)。 一开机,就让它一直运行,每过60分钟,它就随机选一首曲子播放,提醒歇会,看个三级写真照片什么的。

   1 #!/usr/bin/python
   2 # -*- coding: gb2312 -*-
   3 
   4 import sys
   5 import time
   6 import random
   7 import os
   8 
   9 directory = 'c:\\downloads\\music'  # 歌曲文件夹
  10 
  11 def play_soundfile(filename):  # 启动播放器
  12    if sys.platform == 'win32':
  13        import win32api
  14        win32api.ShellExecute(0, "open", filename, None, "", 0)
  15    else:
  16        print "some *nix commands here to play a sound file" # 需要加上在unix 上播放的命令
  17 
  18 fileList =  [os.path.join(directory, os.path.normcase(f)) for f in os.listdir(directory)]  # get a list of all music files
  19 
  20 while 1:
  21    print "--------------------------------------"
  22 
  23    fileName = random.choice(fileList)
  24    print fileName
  25 
  26    play_soundfile(fileName)
  27 
  28    time.sleep(1800)
  29    print '30 minutes have passed. ', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
  30    time.sleep(1800)

http://starship.python.net/crew/mhammond/win32/Downloads.html"

os.startfile(filename)

os.startfile()好像调用的就是win32api.ShellExecute()

startfile( path)

Start a file with its associated application. This acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated. startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application's exit status. The path parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash ("/"); the underlying Win32 ShellExecute() function doesn't work if it is. Use the os.path.normpath() function to ensure that the path is properly encoded for Win32. Availability: Windows. New in version 2.0.

http://www.workrave.org/welcome/

这个是用twisted的task来作, 随便把ipython放到里面, 这样还可以当ipython用.

   1 # -*- coding: utf-8 -*-
   2 
   3 from IPython.Shell import IPShellEmbed
   4 args = ['-pi1','In <\\#>:','-pi2','    .\\D.:','-po','Out<\\#>:','-nosep']
   5 ipshell = IPShellEmbed(args)
   6 ipshell.set_exit_msg('Ctrl-C to exit program')
   7 ipshell.set_banner('Ctrl-D to exit interpreter and continue program')
   8 from twisted.internet import reactor, threads, task
   9  
  10 shell = threads.deferToThread(ipshell)  #打开ipython
  11  
  12 player = task.LoopingCall(play_soundfile)  #这里是你要打开的程序
  13  
  14 player.start(60*30)              #每1800秒播放一次
  15  
  16 #可以用 player.stop() 来中止
  17 reactor.run()

MicroProj/2005-09-28 (last edited 2009-12-25 07:19:15 by localhost)