gpt4 book ai didi

python - 静音 music21 警告

转载 作者:行者123 更新时间:2023-12-05 01:06:18 26 4
gpt4 key购买 nike

我收到了大量关于零时长和弦的 music21 警告:

WARNING: midi chord with zero duration will be treated as grace

我尝试用以下内容覆盖 music21.environment.Environment() 上的 warn 方法:

def silence(*args, **kwargs): pass
e = music21.environment.Environment()
setattr(e, 'warn', silence)

我还尝试将所有警告静音:

import warnings
warnings.filterwarnings('ignore')

两者都没有成功。如何使来自 music21 的这些(或理想情况下的所有)警告静音? (我验证了我的数据后处理。)

最佳答案

如果您使用的是 ipython,您可以尝试将您尝试执行的部分静音:

from IPython.utils import io

with io.capture_output():
< ...your code... >

否则,在源代码 (~/Lib/site-packages/music21) 中,您可以导航到 midi/translate.py 并使用注释 block 或类似内容调整第 585-6 行以禁用警告。

enter image description here

第三个选项可能是创建一个上下文管理器来静音打印,类似于 io.capture_output() 函数:

import io
import sys

class MuteWarn:
def __enter__(self):
self._init_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._init_stdout

with MuteWarn():
< ...your code... >

关于python - 静音 music21 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69378318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com