finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
songs[songs_undone_one[i].replace(“\n”, “”)] = songs_undone_two[i].replace(“\n”, “”)
在对“songs_one.txt”和“songs_two.txt”的处理中,我们就需要用到表格all_songs了。为保证音乐都存在,它会尝试打开路径所指向的文件。如果找不到文件,它会将音乐修改为all_songs中所记录的随机一个音乐(在这里可以进行改善:加一个if判断,如果路径指向的文件后缀不是“.mp3”或“.ogg”,则也要修改音乐。这个工作感兴趣的读者可以自行加上,在此我不再给出代码——这主要是由于我在解释我的程序代码时即使发现了不合理之处,只要不是大问题,就也不愿再行修改的习惯)。
finding = open(“alarm_list.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“alarm_list.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_one.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“dates_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_two.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“dates_two.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_one.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“songs_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_two.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“songs_two.txt”, “w”) as finding:
finding.write(finder)
我的删除闹钟的程序片段不知为何,一直有一些BUG,会在删除处于结尾的闹钟时留下空行(这会导致程序运行出错)。于是,我们很有必要对其的后果作出一些挽回:将每一个文件结尾的换行符删除。而这个程序就是进行了这样一个工作。
def delete(i):
global all_songs, all_songs_dict, alarm_list, dates, songs,alarm_list_undone,dates_undone_one,dates_undone_two,songs_undone_one,songs_undone_two
try:
try:
alarm_list_undone.remove(i)
alarm_list[-1] = alarm_list[-1].replace(“\n”, “”)
except:
alarm_list_undone.remove(i + “\n”)
except:
if False:
print()
alarm_list.remove(i)
with open(“alarm_list.txt”, “w”) as finding:
finding.writelines(alarm_list_undone)
for j in range(len(songs_undone_one)):
if songs_undone_one[j] == i or songs_undone_one[j] == i + “\n”:
songs_undone_one.pop(j)
songs_undone_two.pop(j)
try:
songs_undone_one[-1]=songs_undone_one[-1].replace(“\n”, “”)
songs_undone_two[-1]=songs_undone_two[-1].replace(“\n”, “”)
except:
if False:
print()
songs = {}
for j in range(len(songs_undone_one)):
songs[songs_undone_one[j].replace(“\n”, “”)] = songs_undone_two[j].replace(“\n”, “”)
with open(“songs_one.txt”, “w”) as finding:
finding.writelines(songs_undone_one)
with open(“songs_two.txt”, “w”) as finding:
finding.writelines(songs_undone_two)
for j in range(len(dates_undone_one)):
if dates_undone_one[j] == i or dates_undone_one[j] == i + “\n”:
dates_undone_one.pop(j)
dates_undone_two.pop(j)
try:
dates_undone_one[-1]=dates_undone_one[-1].replace(“\n”, “”)
dates_undone_two[-1]=dates_undone_two[-1].replace(“\n”, “”)
except:
if False:
print()
dates = {}
for j in range(len(dates_undone_one)):
dates[dates_undone_one[j].replace(“\n”, “”)] = dates_undone_two[j].replace(“\n”, “”)
with open(“dates_one.txt”, “w”) as finding:
finding.writelines(dates_undone_one)
with open(“dates_two.txt”, “w”) as finding:
finding.writelines(dates_undone_two)
这就是刚刚提到的有BUG的删除程序。这里输入的i是闹钟的时间(在这个程序中,闹钟时间具有唯一性,一个时间只对应一个闹钟)。这个程序试图删除i所对应的所有信息,但是,对于位于文件和表格结尾的闹钟,它不能有效处理。我试图使它删除上一条末尾的换行符,但显然出现了某种错误。我因此不得不另加一行代码来弥补它的后果。
def play_it(url):
global Listener
pygame.mixer.init()
t = pygame.mixer.music.load(url)
pygame.mixer.music.play()
def on_release(key):
if key == Key.space:
pygame.mixer.music.stop()
with Listener(on_release=on_release) as Listener:
Listener.join()
播放程序,用于在时间到时播放音乐。这里不仅使用了Pygame的音乐播放,还使用了Pynput的键盘监听,这样可以在按下空格时关闭音乐(当然,有时候在打字时也许会误关闹钟,但这无关紧要——你完全可以设置另一个不常用的键来关闭闹钟)。
def alarm():
global all_songs,all_songs_dict,alarm_list,dates,songs
while True:
try:
毫无疑问,在这些代码后面,跟的是程序的核心代码。但接下来这一段无疑令人失望:
e = time.time()
finding = open(“alarm_list.txt”, “a”)
finding.close()
finding = open(“alarm_list.txt”, “r”)
alarm_list_undone = finding.readlines()
finding.close()
alarm_list = []
for i in alarm_list_undone:
alarm_list.append(i.replace(“\n”, “”))
finding = open(“dates_one.txt”, “a”)
finding.close()
finding = open(“dates_one.txt”, “r”)
dates_undone_one = finding.readlines()
finding.close()
finding = open(“dates_two.txt”, “a”)
finding.close()
finding = open(“dates_two.txt”, “r”)
dates_undone_two = finding.readlines()
finding.close()
dates = {}
for i in range(len(dates_undone_one)):
dates[dates_undone_one[i].replace(“\n”, “”)] = eval(dates_undone_two[i].replace(“\n”, “”))
finding = open(“songs_one.txt”, “a”)
finding.close()
finding = open(“songs_one.txt”, “r”)
songs_undone_one = finding.readlines()
finding.close()
finding = open(“songs_two.txt”, “a”)
finding.close()
finding = open(“songs_two.txt”, “r”)
songs_undone_two = finding.readlines()
finding.close()
songs = {}
for i in range(len(songs_undone_two)):
try:
open(songs_undone_two[i].replace(“\n”, “”), “r”)
except:
songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + “\n”
with open(“songs_two.txt”, “w”) as finding:
finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
songs[songs_undone_one[i].replace(“\n”, “”)] = songs_undone_two[i].replace(“\n”, “”)
finding = open(“alarm_list.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“alarm_list.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_one.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“dates_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_two.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“dates_two.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_one.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“songs_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_two.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“songs_two.txt”, “w”) as finding:
finding.write(finder)
没错,这就是前面的存储载入的代码。这是为了刷新对文件的读取,毕竟,你很有可能会在程序运行时修改闹钟。
for i in alarm_list:
if str(datetime.datetime.now())[11:16] == i:
if time.strftime(“%w”, time.localtime()) in dates.get(i):
url = songs.get(i)
True_False=int(time.strftime(“%Y”, time.localtime())) < int(dates.get(i)[-1][4:8]) or (int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime(“%j”, time.localtime())) <= int(dates.get(i)[-1][9:]))
if int(time.strftime(“%Y”, time.localtime())) > int(dates.get(i)[-1][4:8]) or (
int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
time.strftime(“%j”, time.localtime())) >= int(dates.get(i)[-1][9:])):
delete(i)
if True_False:
play_it(url)
else:
if int(time.strftime(“%Y”, time.localtime())) > int(dates.get(i)[-1][4:8]) or (
int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
time.strftime(“%j”, time.localtime())) > int(dates.get(i)[-1][9:])):
delete(i)
if int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime(“%j”, time.localtime())) == int(dates.get(i)[-1][9:]) and int(str(datetime.datetime.now())[11:13])*60+int(str(datetime.datetime.now())[14:16]) > int(i[:2])*60+int(i[3:]):
delete(i)
这才是你们期待的核心代码(虽然功能很核心,但却还是很简单的)我们将闹钟列表遍历,将时间与闹钟所设置的时间所比对,如果现在是某个闹钟所设定的时间,同时现在日期在一星期中的位置(暂且就这么表述,我突然发现像“星期一”之类的词似乎……没有一个统称)也存在于对应的表格中,同时现在的日期也没有超过闹钟的截止日期,我们就会响铃(铃声会遵从该闹钟的设置)。同时,对于已截至的闹钟,它会予以删除。
这样,响铃的程序就已初步完成。但是,为了使程序不挤占进程(很多闹钟程序都会如此),我们还是有必要加上这一段代码:
time.sleep(1 - (time.time() - e))
except:
time.sleep(1 - (time.time() - e))
当然最后得运行函数:
alarm()
这样,我们就把闹钟的程序完成了。
附上完整代码:
#coding:utf-8
import datetime
import pygame
import os
import time
import random
from pynput.keyboard import Key,Listener
all_songs=[]
all_songs_dict={}
for root, dirs, names in os.walk(“d:/Users/{}/Music”.format(os.getlogin())):
for filename in names:
if os.path.join(root, filename)[-4:] == “.mp3” or os.path.join(root, filename)[-4:] == “.ogg”:
all_songs.append(os.path.join(root, filename)[os.path.join(root, filename).find(“\”) + 1:-4])
all_songs_dict[
os.path.join(root, filename)[os.path.join(root, filename).find(“\”) + 1:-4]] = os.path.join(root,
filename).replace(
“\”, “/”)
finding = open(“alarm_list.txt”, “a”)
finding.close()
finding = open(“alarm_list.txt”, “r”)
alarm_list_undone = finding.readlines()
finding.close()
alarm_list = []
for i in alarm_list_undone:
alarm_list.append(i.replace(“\n”, “”))
finding = open(“dates_one.txt”, “a”)
finding.close()
finding = open(“dates_one.txt”, “r”)
dates_undone_one = finding.readlines()
finding.close()
finding = open(“dates_two.txt”, “a”)
finding.close()
finding = open(“dates_two.txt”, “r”)
dates_undone_two = finding.readlines()
finding.close()
dates = {}
for i in range(len(dates_undone_one)):
dates[dates_undone_one[i].replace(“\n”, “”)] =eval( dates_undone_two[i].replace(“\n”, “”))
finding = open(“songs_one.txt”, “a”)
finding.close()
finding = open(“songs_one.txt”, “r”)
songs_undone_one = finding.readlines()
finding.close()
finding = open(“songs_two.txt”, “a”)
finding.close()
finding = open(“songs_two.txt”, “r”)
songs_undone_two = finding.readlines()
finding.close()
songs = {}
for i in range(len(songs_undone_two)):
try:
open(songs_undone_two[i].replace(“\n”, “”),“r”)
except:
songs_undone_two[i]=all_songs_dict[all_songs[random.randint(0,len(all_songs)-1)]]+“\n”
with open(“songs_two.txt”, “w”)as finding:
finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
songs[songs_undone_one[i].replace(“\n”, “”)] = songs_undone_two[i].replace(“\n”, “”)
finding = open(“alarm_list.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“alarm_list.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_one.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“dates_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_two.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“dates_two.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_one.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“songs_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_two.txt”, “r”)
finder=finding.read()
try:
while finder[-1] == “\n”:
finder = finder[:-1]
except:
while False:
print()
with open(“songs_two.txt”, “w”) as finding:
finding.write(finder)
def delete(i):
global all_songs, all_songs_dict, alarm_list, dates, songs,alarm_list_undone,dates_undone_one,dates_undone_two,songs_undone_one,songs_undone_two
try:
try:
alarm_list_undone.remove(i)
alarm_list[-1] = alarm_list[-1].replace(“\n”, “”)
except:
alarm_list_undone.remove(i + “\n”)
except:
if False:
print()
alarm_list.remove(i)
with open(“alarm_list.txt”, “w”) as finding:
finding.writelines(alarm_list_undone)
for j in range(len(songs_undone_one)):
if songs_undone_one[j] == i or songs_undone_one[j] == i + “\n”:
songs_undone_one.pop(j)
songs_undone_two.pop(j)
try:
songs_undone_one[-1]=songs_undone_one[-1].replace(“\n”, “”)
songs_undone_two[-1]=songs_undone_two[-1].replace(“\n”, “”)
except:
if False:
print()
songs = {}
for j in range(len(songs_undone_one)):
songs[songs_undone_one[j].replace(“\n”, “”)] = songs_undone_two[j].replace(“\n”, “”)
with open(“songs_one.txt”, “w”) as finding:
finding.writelines(songs_undone_one)
with open(“songs_two.txt”, “w”) as finding:
finding.writelines(songs_undone_two)
for j in range(len(dates_undone_one)):
if dates_undone_one[j] == i or dates_undone_one[j] == i + “\n”:
dates_undone_one.pop(j)
dates_undone_two.pop(j)
try:
dates_undone_one[-1]=dates_undone_one[-1].replace(“\n”, “”)
dates_undone_two[-1]=dates_undone_two[-1].replace(“\n”, “”)
except:
if False:
print()
dates = {}
for j in range(len(dates_undone_one)):
dates[dates_undone_one[j].replace(“\n”, “”)] = dates_undone_two[j].replace(“\n”, “”)
with open(“dates_one.txt”, “w”) as finding:
finding.writelines(dates_undone_one)
with open(“dates_two.txt”, “w”) as finding:
finding.writelines(dates_undone_two)
def play_it(url):
global Listener
pygame.mixer.init()
t = pygame.mixer.music.load(url)
pygame.mixer.music.play()
def on_release(key):
if key == Key.space:
pygame.mixer.music.stop()
with Listener(on_release=on_release) as Listener:
Listener.join()
def alarm():
global all_songs,all_songs_dict,alarm_list,dates,songs
while True:
try:
e = time.time()
finding = open(“alarm_list.txt”, “a”)
finding.close()
finding = open(“alarm_list.txt”, “r”)
alarm_list_undone = finding.readlines()
finding.close()
alarm_list = []
for i in alarm_list_undone:
alarm_list.append(i.replace(“\n”, “”))
finding = open(“dates_one.txt”, “a”)
finding.close()
finding = open(“dates_one.txt”, “r”)
dates_undone_one = finding.readlines()
finding.close()
finding = open(“dates_two.txt”, “a”)
finding.close()
finding = open(“dates_two.txt”, “r”)
dates_undone_two = finding.readlines()
finding.close()
dates = {}
for i in range(len(dates_undone_one)):
dates[dates_undone_one[i].replace(“\n”, “”)] = eval(dates_undone_two[i].replace(“\n”, “”))
finding = open(“songs_one.txt”, “a”)
finding.close()
finding = open(“songs_one.txt”, “r”)
songs_undone_one = finding.readlines()
finding.close()
finding = open(“songs_two.txt”, “a”)
finding.close()
finding = open(“songs_two.txt”, “r”)
songs_undone_two = finding.readlines()
finding.close()
songs = {}
for i in range(len(songs_undone_two)):
try:
open(songs_undone_two[i].replace(“\n”, “”), “r”)
except:
songs_undone_two[i] = all_songs_dict[all_songs[random.randint(0, len(all_songs) - 1)]] + “\n”
with open(“songs_two.txt”, “w”) as finding:
finding.writelines(songs_undone_two)
for i in range(len(songs_undone_one)):
songs[songs_undone_one[i].replace(“\n”, “”)] = songs_undone_two[i].replace(“\n”, “”)
finding = open(“alarm_list.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“alarm_list.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_one.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“dates_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“dates_two.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“dates_two.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_one.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“songs_one.txt”, “w”) as finding:
finding.write(finder)
finding = open(“songs_two.txt”, “r”)
finder = finding.read()
while finder[-1] == “\n”:
finder = finder[:-1]
with open(“songs_two.txt”, “w”) as finding:
finding.write(finder)
for i in alarm_list:
if str(datetime.datetime.now())[11:16] == i:
if time.strftime(“%w”, time.localtime()) in dates.get(i):
url = songs.get(i)
True_False=int(time.strftime(“%Y”, time.localtime())) < int(dates.get(i)[-1][4:8]) or (int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime(“%j”, time.localtime())) <= int(dates.get(i)[-1][9:]))
if int(time.strftime(“%Y”, time.localtime())) > int(dates.get(i)[-1][4:8]) or (
int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
time.strftime(“%j”, time.localtime())) >= int(dates.get(i)[-1][9:])):
delete(i)
if True_False:
play_it(url)
else:
if int(time.strftime(“%Y”, time.localtime())) > int(dates.get(i)[-1][4:8]) or (
int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(
time.strftime(“%j”, time.localtime())) > int(dates.get(i)[-1][9:])):
delete(i)
if int(time.strftime(“%Y”, time.localtime())) == int(dates.get(i)[-1][4:8]) and int(time.strftime(“%j”, time.localtime())) == int(dates.get(i)[-1][9:]) and int(str(datetime.datetime.now())[11:13])*60+int(str(datetime.datetime.now())[14:16]) > int(i[:2])*60+int(i[3:]):
delete(i)
time.sleep(1 - (time.time() - e))
except:
time.sleep(1 - (time.time() - e))
print(all_songs)
print(all_songs_dict)
print(alarm_list)
print(dates)
print(songs)
alarm()
你是否疑惑:我们如何设置闹钟?这后面的内容,我们就来解决这一件事。
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数软件测试工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-cFRzScpL-1712696997598)]
[外链图片转存中…(img-Fp6wga3P-1712696997598)]
[外链图片转存中…(img-2Y24OiTX-1712696997598)]
[外链图片转存中…(img-iGKeeUif-1712696997599)]
[外链图片转存中…(img-a4catGiB-1712696997599)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上软件测试开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-vYBP1FSU-1712696997599)]
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!