内存管理和拷贝
review
1.内置类属性: name、doc、class、dict、module、bases
2.私有化
3.属性的getter和setter
4.继承
class 类名(父类1,父类2):
pass
class Person:
def __init__(self, name, age, gender='男'):
# name = '小明', age = 18, gender='男'
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, study_id, gender='男'):
# name='小明', age=18, study_id='001', gender='男'
super(Student, self).__init__(name, age, gender)
self.study_id = study_id
stu = Student('小明', 18, '001')
多继承
1.多继承
python中的类支持多继承(让一个类同时继承多个类);
多继承的时候,子类只能继承第一个父类所有的属性和方法,后面的父类中只有字段和方法可以被继承
class Animal(object):
num = 100
def __init__(self):
self.age = 0
self.gender = '雌'
@classmethod
def func1(cls):
print('动物类的类方法')
def func2(self):
print('动物类中的对象方法')
class Fly(object):
name = '飞行器'
def __init__(self):
self.height = 100
self.time = 5
self.speed = 100
def func2(self):
print('飞行的对象方法')
class Bird(Animal, Fly):
pass
bird1 = Bird()
# 字段都能继承
print(Bird.num, Bird.name)
Bird.func1()
bird1.func2()
# print(bird1.age, bird1.gender)
# print(bird1.speed, bird1.height, bird1.time)
运算符重载
import copy
1.运算符
python中所有的类型都是类,所以所有的数据都是对象;
python中使用任意的运算符都是在调用相应类中的相应方法,每一个运算符对应什么方法是固定的,
某种数据是否支持某个运算符操作就看这个数据类型中是否实现了对应的方法
2.运算符重载指的是在不同的类中实现同样的运算符对应的函数
类的对象默认情况下只支持: ==, !=
10 * 20
'abc'+'123'
[1, 2] + [2, 3, 4]
# {'a': 10} + {'b': 20}
class Student:
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return '<%s, id:%s>' % (str(self.__dict__)[1:-1], hex(id(self)))
# a+b -> a.__add__(b)
# self -> 当前类的对象,也是+前面的那个数据
# other -> +后面的那个数据, 类型根据运算规则的设计可以是任何类型的数据
def __add__(self, other):
# return self.age + other.age
return self.score + other.score
# return Student(self.name+other.name, self.age+other.age, self.score + other.score)
# return self.score + other
# a*b -> a.__mul__(b)
def __mul__(self, other):
list = []
for _ in range(other):
list.append(copy.copy(self))
return list
# a<b -> a.__lt__(b)
# 注意: <和>符号只需要重载其中一个就可以
def __lt__(self, other):
return self.score < other.score
stu1 = Student('小明', 19, 90)
stu2 = Student('小花', 20, 78)
# stu1.__add__(stu2)
print(stu1 + stu2)
# stu1.__add__(100)
# print(stu1 + 100)
print(stu1 * 3)
students = [stu1, stu2, Student('小红', 12, 100)]
students.sort()
print(students)
print(stu1 < stu2)
print(stu1 > stu2)
浅拷贝和深拷贝
from copy import copy, deepcopy
class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
p1 = Person('小明', 18, Dog('大黄', '黄色'))
1.直接赋值
将变量中的地址直接赋给新的变量;赋值后两个变量的地址相同
p2 = p1
print(id(p1), id(p2)) # 4537270848 4537270848
p1.name = '小花'
print(p1.name, p2.name) # 小花 小花
p2.dog.color = '绿色'
print(p1.dog.color, p2.dog.color) # 绿色 绿色
2.拷贝
不管是浅拷贝还是深拷贝都会对原数据进行赋值产生新的地址
list1 = [1, 2, 3]
list2 = copy(list1)
list3 = deepcopy(list1)
print(id(list1), id(list2), id(list3))
list1.append(100)
print(list2, list3)
p3 = copy(p1)
p4 = deepcopy(p1)
print(id(p1), id(p3), id(p4))
p1.name = '小红'
print(p3.name, p4.name)
3.浅拷贝
字符串、列表和元祖的切片;对象.copy(); copy模块中的copy方法都是浅拷贝
浅拷贝指拷贝当前对象,不会对子对象进行拷贝
print('==========浅拷贝=========')
p3 = copy(p1)
print(id(p1), id(p3)) # 4401914600 4401916336
print(id(p1.dog), id(p3.dog)) # 4567065152 4567065152
p1.name = 'Tom'
print(p1.name, p3.name) # Tom 小红
p1.dog.color = '红色'
print(p1.dog.color, p3.dog.color) # 红色 红色
4.深拷贝
copy模块中的deepcopy方法是深拷贝
print('==========深拷贝=========')
p4 = deepcopy(p1)
print(id(p1), id(p4)) # 4486472592 4486474384
print(id(p1.dog), id(p4.dog)) # 4485063960 4486474552
p1.name = 'Bob'
print(p1.name, p4.name) # Bob Tom
p1.dog.color = '橙色'
print(p1.dog.color, p4.dog.color) # 橙色 红色
练习:
a = ['color', 'height', 'background']
b = [a, 'aaa', 'bbb']
c1 = b
c2 = copy(b)
c3 = deepcopy(b)
a[-1] = ['BG']
b.append('ccc')
# b = [['color', 'height', 'background'], 'aaa', 'bbb']
# 问题: print(c1), print(c2), print(c3)的结果分别是
# c1 = [['color', 'height', ['BG']], 'aaa', 'bbb', 'ccc']
# c2 = [['color', 'height', ['BG']], 'aaa', 'bbb']
# c3 = [['color', 'height', 'background'], 'aaa', 'bbb']
print(c1)
print(c2)
print(c3)
枚举
from enum import Enum, unique
枚举值的特点:
1.可以通过有意义的属性名直接显示数据
2.每个数据的值不能修改
3.可以做到不同的数据的值唯一
@unique
class PokerNum(Enum):
J = 11
Q = 12
K = 13
A = 1
class Color:
RED = (255, 0, 0)
print(PokerNum.J)
print(PokerNum.K.value, PokerNum.J.value > PokerNum.Q.value)
内存管理
from copy import copy, deepcopy
from sys import getrefcount
1.内存的开辟
内存区间分为栈区间和堆区间;栈区间的内存自动开辟自动释放,堆区间的内存需要程序员手动开辟,手动释放;
但是python已经将堆区间内存的开辟和释放自动化
a.当每次给变量赋值的时候,系统会先在堆区间中开辟空间将数据存起来,然后再将数据在堆中的地址存到变量中,变量存在栈区间;
b.数字和字符串数据在开辟空间的时候,会先检测内存中之前是否已经有这个数据,
如果有就直接使用之前的数据,没有才开辟新的空间保存数据
a = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
b = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
print(id(a), id(b)) # 4484287176 4484285768
print(id(a[3]), id(b[3]))
print(id(a[0]), id(b[0]), id(a[3][0]), id(b[3][0])) # 4449482688 4449482688 4449482688 4449482688
a1 = 100
b1 = 100
print(id(a1), id(b1)) # 4480746528 4480746528
a2 = 'hello'
b2 = 'hello'
print(id(a2), id(b2)) # 4492420184 4492420184
a3 = {'a': 10}
b3 = {'a': 10}
print(a3 == b3) # True
print(a3 is b3) # False
a4 = 200
b4 = deepcopy(a4)
print(id(a4), id(b4)) # 4367007904 4367007904
a5 = '安徽省打发卡机阿哈萨克阿按时发阿萨德回复杰卡斯发是打飞机啊山东矿机返回撒娇快递费哈萨克京东方撒地方'
b5 = '安徽省打发卡机阿哈萨克阿按时发阿萨德回复杰卡斯发是打飞机啊山东矿机返回撒娇快递费哈萨克京东方撒地方'
print(id(a5), id(b5)) # 4338364064 4338364064
2.内存的释放
栈区间:全局栈区间在程序结束后销毁,函数栈区间在函数调用结束后销毁(自动)
堆区间: 看一个对象是否销毁,就看这个对象的引用计数是否为0;如果一个对象的引用为0,这个对象就会销毁;
(垃圾回收机制)
注意: python中针对对象的循环引用已经做了处理,程序员不需要写额外的代码来解决循环引用问题
a6 = {'name': '小明', 'age': 18}
b6 = a6
print(getrefcount(a6))
b6 = 100
print(getrefcount(a6))
del a6
print(getrefcount(b6))
class Person:
def __init__(self, name):
self.name = name
self.dog = None
class Dog:
def __init__(self, name):
self.name = name
self.owner = None
dog1 = Dog('大黄')
p1 = Person('小明')
p1.dog = dog1
dog1.owner = p1
del dog1
del p1
扑克洗牌发牌
from enum import Enum,unique
from random import shuffle
# 1.写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
@unique
class PokerNum(Enum):
J = 11, 'J', 11
Q = 12, 'Q', 12
K = 13, 'K', 13
A = 1, 'A', 14
TWO = 2, '2', 15
BigJoker = 15, '大王', 17
SmallJoker = 14, '小王', 16
@unique
class PokerColor(Enum):
Club = '♣'
Diamond = '♦'
Heart = '♥'
Spade = '♠'
Space = ''
class Poker:
def __init__(self, num: PokerNum, color: PokerColor):
self.num = num
self.color = color
def __repr__(self):
if isinstance(self.num, PokerNum):
return str(self.color.value) + str(self.num.value[1])
return str(self.color.value)+str(self.num)
def __gt__(self, other):
if isinstance(self.num, PokerNum):
a = self.num.value[2]
else:
a = self.num
if isinstance(other.num, PokerNum):
b = other.num.value[2]
else:
b = other.num
return a > b
class Game:
# 斗地主
def __init__(self):
pokers = []
colors = [PokerColor.Club, PokerColor.Diamond, PokerColor.Heart, PokerColor.Spade]
nums = [PokerNum.A, PokerNum.TWO, 3, 4, 5, 6, 7, 8, 9, 10, PokerNum.J, PokerNum.Q, PokerNum.K]
for num in nums:
for color in colors:
p = Poker(num, color)
pokers.append(p)
pokers.extend([Poker(PokerNum.SmallJoker, PokerColor.Space), Poker(PokerNum.BigJoker, PokerColor.Space)])
self.pokers = pokers
self.pokers_iter = iter(self.pokers)
# 洗牌
def shuffling(self):
shuffle(self.pokers)
self.pokers_iter = iter(self.pokers)
# 斗地主发牌方式
def deal1(self):
ps1 = []
ps2 = []
ps3 = []
for _ in range(17):
ps1.append(next(self.pokers_iter))
ps2.append(next(self.pokers_iter))
ps3.append(next(self.pokers_iter))
ps1.sort(reverse=True)
ps2.sort(reverse=True)
ps3.sort(reverse=True)
return ps1, ps2, ps3, list(self.pokers_iter)
game1 = Game()
game1.shuffling()
p1, p2, p3, di = game1.deal1()
print(p1)
print(p2)
print(p3)
print('底牌:', di)
歌词解释器
# (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词
# 提示:歌词文件的内容一般是按下面的格式进行存储的。
# 歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
class Lyric:
def __init__(self, time, word):
value = float(time[1:3])*60 + float(time[4:])
self.time = value
self.word = word
def __repr__(self):
return '<'+str(self.time) + ':' + self.word+'>'
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, name):
# 歌名
self.name = name
self.__all_lyrics = []
def get_word(self, time):
# =======解析歌词文件======
if not self.__all_lyrics:
print('解析歌词')
with open('files/'+self.name) as f:
while True:
line = f.readline()
if not line:
break
lines = line.split(']')
word = lines[-1]
for t in lines[:-1]:
lyric = Lyric(t, word)
self.__all_lyrics.append(lyric)
# 排序
self.__all_lyrics.sort(reverse=True)
# ==========获取歌词==========
for lyric in self.__all_lyrics:
if lyric.time <= time:
return lyric.word
ly = LyricAnalysis('蓝莲花')
print('===:',ly.get_word(123))
print('===:',ly.get_word(10))
print('===:',ly.get_word(89))
ly2 = LyricAnalysis('一首简单的歌')
print('!!!:', ly2.get_word(30))
print('!!!:', ly2.get_word(90))
内存管理和拷贝
review
1.内置类属性: name、doc、class、dict、module、bases
2.私有化
3.属性的getter和setter
4.继承
class 类名(父类1,父类2):
pass
class Person:
def __init__(self, name, age, gender='男'):
# name = '小明', age = 18, gender='男'
self.name = name
self.age = age
self.gender = gender
class Student(Person):
def __init__(self, name, age, study_id, gender='男'):
# name='小明', age=18, study_id='001', gender='男'
super(Student, self).__init__(name, age, gender)
self.study_id = study_id
stu = Student('小明', 18, '001')
多继承
1.多继承
python中的类支持多继承(让一个类同时继承多个类);
多继承的时候,子类只能继承第一个父类所有的属性和方法,后面的父类中只有字段和方法可以被继承
class Animal(object):
num = 100
def __init__(self):
self.age = 0
self.gender = '雌'
@classmethod
def func1(cls):
print('动物类的类方法')
def func2(self):
print('动物类中的对象方法')
class Fly(object):
name = '飞行器'
def __init__(self):
self.height = 100
self.time = 5
self.speed = 100
def func2(self):
print('飞行的对象方法')
class Bird(Animal, Fly):
pass
bird1 = Bird()
# 字段都能继承
print(Bird.num, Bird.name)
Bird.func1()
bird1.func2()
# print(bird1.age, bird1.gender)
# print(bird1.speed, bird1.height, bird1.time)
运算符重载
import copy
1.运算符
python中所有的类型都是类,所以所有的数据都是对象;
python中使用任意的运算符都是在调用相应类中的相应方法,每一个运算符对应什么方法是固定的,
某种数据是否支持某个运算符操作就看这个数据类型中是否实现了对应的方法
2.运算符重载指的是在不同的类中实现同样的运算符对应的函数
类的对象默认情况下只支持: ==, !=
10 * 20
'abc'+'123'
[1, 2] + [2, 3, 4]
# {'a': 10} + {'b': 20}
class Student:
def __init__(self, name, age, score=0):
self.name = name
self.age = age
self.score = score
def __repr__(self):
return '<%s, id:%s>' % (str(self.__dict__)[1:-1], hex(id(self)))
# a+b -> a.__add__(b)
# self -> 当前类的对象,也是+前面的那个数据
# other -> +后面的那个数据, 类型根据运算规则的设计可以是任何类型的数据
def __add__(self, other):
# return self.age + other.age
return self.score + other.score
# return Student(self.name+other.name, self.age+other.age, self.score + other.score)
# return self.score + other
# a*b -> a.__mul__(b)
def __mul__(self, other):
list = []
for _ in range(other):
list.append(copy.copy(self))
return list
# a<b -> a.__lt__(b)
# 注意: <和>符号只需要重载其中一个就可以
def __lt__(self, other):
return self.score < other.score
stu1 = Student('小明', 19, 90)
stu2 = Student('小花', 20, 78)
# stu1.__add__(stu2)
print(stu1 + stu2)
# stu1.__add__(100)
# print(stu1 + 100)
print(stu1 * 3)
students = [stu1, stu2, Student('小红', 12, 100)]
students.sort()
print(students)
print(stu1 < stu2)
print(stu1 > stu2)
浅拷贝和深拷贝
from copy import copy, deepcopy
class Dog:
def __init__(self, name, color):
self.name = name
self.color = color
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def __repr__(self):
return '<%s,id: %s>' % (str(self.__dict__)[1:-1], hex(id(self)))
p1 = Person('小明', 18, Dog('大黄', '黄色'))
1.直接赋值
将变量中的地址直接赋给新的变量;赋值后两个变量的地址相同
p2 = p1
print(id(p1), id(p2)) # 4537270848 4537270848
p1.name = '小花'
print(p1.name, p2.name) # 小花 小花
p2.dog.color = '绿色'
print(p1.dog.color, p2.dog.color) # 绿色 绿色
2.拷贝
不管是浅拷贝还是深拷贝都会对原数据进行赋值产生新的地址
list1 = [1, 2, 3]
list2 = copy(list1)
list3 = deepcopy(list1)
print(id(list1), id(list2), id(list3))
list1.append(100)
print(list2, list3)
p3 = copy(p1)
p4 = deepcopy(p1)
print(id(p1), id(p3), id(p4))
p1.name = '小红'
print(p3.name, p4.name)
3.浅拷贝
字符串、列表和元祖的切片;对象.copy(); copy模块中的copy方法都是浅拷贝
浅拷贝指拷贝当前对象,不会对子对象进行拷贝
print('==========浅拷贝=========')
p3 = copy(p1)
print(id(p1), id(p3)) # 4401914600 4401916336
print(id(p1.dog), id(p3.dog)) # 4567065152 4567065152
p1.name = 'Tom'
print(p1.name, p3.name) # Tom 小红
p1.dog.color = '红色'
print(p1.dog.color, p3.dog.color) # 红色 红色
4.深拷贝
copy模块中的deepcopy方法是深拷贝
print('==========深拷贝=========')
p4 = deepcopy(p1)
print(id(p1), id(p4)) # 4486472592 4486474384
print(id(p1.dog), id(p4.dog)) # 4485063960 4486474552
p1.name = 'Bob'
print(p1.name, p4.name) # Bob Tom
p1.dog.color = '橙色'
print(p1.dog.color, p4.dog.color) # 橙色 红色
练习:
a = ['color', 'height', 'background']
b = [a, 'aaa', 'bbb']
c1 = b
c2 = copy(b)
c3 = deepcopy(b)
a[-1] = ['BG']
b.append('ccc')
# b = [['color', 'height', 'background'], 'aaa', 'bbb']
# 问题: print(c1), print(c2), print(c3)的结果分别是
# c1 = [['color', 'height', ['BG']], 'aaa', 'bbb', 'ccc']
# c2 = [['color', 'height', ['BG']], 'aaa', 'bbb']
# c3 = [['color', 'height', 'background'], 'aaa', 'bbb']
print(c1)
print(c2)
print(c3)
枚举
from enum import Enum, unique
枚举值的特点:
1.可以通过有意义的属性名直接显示数据
2.每个数据的值不能修改
3.可以做到不同的数据的值唯一
@unique
class PokerNum(Enum):
J = 11
Q = 12
K = 13
A = 1
class Color:
RED = (255, 0, 0)
print(PokerNum.J)
print(PokerNum.K.value, PokerNum.J.value > PokerNum.Q.value)
内存管理
from copy import copy, deepcopy
from sys import getrefcount
1.内存的开辟
内存区间分为栈区间和堆区间;栈区间的内存自动开辟自动释放,堆区间的内存需要程序员手动开辟,手动释放;
但是python已经将堆区间内存的开辟和释放自动化
a.当每次给变量赋值的时候,系统会先在堆区间中开辟空间将数据存起来,然后再将数据在堆中的地址存到变量中,变量存在栈区间;
b.数字和字符串数据在开辟空间的时候,会先检测内存中之前是否已经有这个数据,
如果有就直接使用之前的数据,没有才开辟新的空间保存数据
a = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
b = [1, 2, 3, [1, 2]] # [1, 2, 0xccc]
print(id(a), id(b)) # 4484287176 4484285768
print(id(a[3]), id(b[3]))
print(id(a[0]), id(b[0]), id(a[3][0]), id(b[3][0])) # 4449482688 4449482688 4449482688 4449482688
a1 = 100
b1 = 100
print(id(a1), id(b1)) # 4480746528 4480746528
a2 = 'hello'
b2 = 'hello'
print(id(a2), id(b2)) # 4492420184 4492420184
a3 = {'a': 10}
b3 = {'a': 10}
print(a3 == b3) # True
print(a3 is b3) # False
a4 = 200
b4 = deepcopy(a4)
print(id(a4), id(b4)) # 4367007904 4367007904
a5 = '安徽省打发卡机阿哈萨克阿按时发阿萨德回复杰卡斯发是打飞机啊山东矿机返回撒娇快递费哈萨克京东方撒地方'
b5 = '安徽省打发卡机阿哈萨克阿按时发阿萨德回复杰卡斯发是打飞机啊山东矿机返回撒娇快递费哈萨克京东方撒地方'
print(id(a5), id(b5)) # 4338364064 4338364064
2.内存的释放
栈区间:全局栈区间在程序结束后销毁,函数栈区间在函数调用结束后销毁(自动)
堆区间: 看一个对象是否销毁,就看这个对象的引用计数是否为0;如果一个对象的引用为0,这个对象就会销毁;
(垃圾回收机制)
注意: python中针对对象的循环引用已经做了处理,程序员不需要写额外的代码来解决循环引用问题
a6 = {'name': '小明', 'age': 18}
b6 = a6
print(getrefcount(a6))
b6 = 100
print(getrefcount(a6))
del a6
print(getrefcount(b6))
class Person:
def __init__(self, name):
self.name = name
self.dog = None
class Dog:
def __init__(self, name):
self.name = name
self.owner = None
dog1 = Dog('大黄')
p1 = Person('小明')
p1.dog = dog1
dog1.owner = p1
del dog1
del p1
扑克洗牌发牌
from enum import Enum,unique
from random import shuffle
# 1.写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
@unique
class PokerNum(Enum):
J = 11, 'J', 11
Q = 12, 'Q', 12
K = 13, 'K', 13
A = 1, 'A', 14
TWO = 2, '2', 15
BigJoker = 15, '大王', 17
SmallJoker = 14, '小王', 16
@unique
class PokerColor(Enum):
Club = '♣'
Diamond = '♦'
Heart = '♥'
Spade = '♠'
Space = ''
class Poker:
def __init__(self, num: PokerNum, color: PokerColor):
self.num = num
self.color = color
def __repr__(self):
if isinstance(self.num, PokerNum):
return str(self.color.value) + str(self.num.value[1])
return str(self.color.value)+str(self.num)
def __gt__(self, other):
if isinstance(self.num, PokerNum):
a = self.num.value[2]
else:
a = self.num
if isinstance(other.num, PokerNum):
b = other.num.value[2]
else:
b = other.num
return a > b
class Game:
# 斗地主
def __init__(self):
pokers = []
colors = [PokerColor.Club, PokerColor.Diamond, PokerColor.Heart, PokerColor.Spade]
nums = [PokerNum.A, PokerNum.TWO, 3, 4, 5, 6, 7, 8, 9, 10, PokerNum.J, PokerNum.Q, PokerNum.K]
for num in nums:
for color in colors:
p = Poker(num, color)
pokers.append(p)
pokers.extend([Poker(PokerNum.SmallJoker, PokerColor.Space), Poker(PokerNum.BigJoker, PokerColor.Space)])
self.pokers = pokers
self.pokers_iter = iter(self.pokers)
# 洗牌
def shuffling(self):
shuffle(self.pokers)
self.pokers_iter = iter(self.pokers)
# 斗地主发牌方式
def deal1(self):
ps1 = []
ps2 = []
ps3 = []
for _ in range(17):
ps1.append(next(self.pokers_iter))
ps2.append(next(self.pokers_iter))
ps3.append(next(self.pokers_iter))
ps1.sort(reverse=True)
ps2.sort(reverse=True)
ps3.sort(reverse=True)
return ps1, ps2, ps3, list(self.pokers_iter)
game1 = Game()
game1.shuffling()
p1, p2, p3, di = game1.deal1()
print(p1)
print(p2)
print(p3)
print('底牌:', di)
歌词解释器
# (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词
# 提示:歌词文件的内容一般是按下面的格式进行存储的。
# 歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
class Lyric:
def __init__(self, time, word):
value = float(time[1:3])*60 + float(time[4:])
self.time = value
self.word = word
def __repr__(self):
return '<'+str(self.time) + ':' + self.word+'>'
def __gt__(self, other):
return self.time > other.time
class LyricAnalysis:
def __init__(self, name):
# 歌名
self.name = name
self.__all_lyrics = []
def get_word(self, time):
# =======解析歌词文件======
if not self.__all_lyrics:
print('解析歌词')
with open('files/'+self.name) as f:
while True:
line = f.readline()
if not line:
break
lines = line.split(']')
word = lines[-1]
for t in lines[:-1]:
lyric = Lyric(t, word)
self.__all_lyrics.append(lyric)
# 排序
self.__all_lyrics.sort(reverse=True)
# ==========获取歌词==========
for lyric in self.__all_lyrics:
if lyric.time <= time:
return lyric.word
ly = LyricAnalysis('蓝莲花')
print('===:',ly.get_word(123))
print('===:',ly.get_word(10))
print('===:',ly.get_word(89))
ly2 = LyricAnalysis('一首简单的歌')
print('!!!:', ly2.get_word(30))
print('!!!:', ly2.get_word(90))