我正在尝试使用新的 python 功能(数据类)。我正在尝试初始化变量,但出现错误:
raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'dict'> for field headers is not allowed: use default_factory
Run Code Online (Sandbox Code Playgroud)
我的代码:
@dataclass
class Application():
__config = ConfigParser()
__config.read('mydb.ini')
__host: str = __config.get('db','dbhost')
__user: str = __config.get('db','dbuser')
__password: str = __config.get('db','dbpw')
__database: str = __config.get('db','database')
url: str = "https://xxxx.domain.com/"
headers: str = {'X-ApiKeys':'accessKey=xxxxxxx;secretKey=xxxxx','Content-Type': 'application/json'}
def main(self):
print(self.__host,self.__user,self.__password, self.__database)
app = Application()
if __name__=="__main__":
app.main()
Run Code Online (Sandbox Code Playgroud)
初始化字典的正确方法是什么?
我创建了一个从 Youtube 下载视频和声音的脚本,然后使用 ffmpeg 合并声音和视频,我想知道是否有另一种方法可以产生相同的结果,但速度更快?因为这个脚本大约需要 7 分钟 ~ 取决于视频质量和持续时间。我的代码如下:
from pytube import YouTube
import sys
import ffmpeg
import os
class Downloader(YouTube):
def __init__(self, link):
self.link = YouTube(link)
self.hq = []
self.best_video = []
self.best_sound = []
def stream_objects(self):
q = [self.hq.append(x) for x in self.link.streams.all()]
self.best_video.append(str(self.hq[1]).split()[1].split('\"')[1])
self.best_sound.append(str(self.hq[-1]).split()[1].split('\"')[1])
return self.best_video, self.best_sound
def downloady(self):
vid = self.link.streams.get_by_itag(str(self.best_video).strip("['']"))
audio = self.link.streams.get_by_itag(str(self.best_sound).strip("['']"))
self.vid_title = (f"{vid.title}"+".mp4")
vid.download(filename='video')
audio.download(filename='audio')
print('Downloaded, Now Starting Merge \n\n\n\n\n')
print(f'{self.vid_title}'+'\n')
def merge(self):
ffmpeg.output(ffmpeg.input('video.mp4'), ffmpeg.input('audio.webm'), self.vid_title).run()
os.remove('video.mp4')
os.remove('audio.webm')
if __name__=='__main__':
a = …Run Code Online (Sandbox Code Playgroud)