[英]Python error with literals and lists in loguru: unsupported operand type(s) for +: 'NoneType' and 'list'
Working with loguru in order to write a list back to the configuration file, and received the dreaded unsupported operand type(s) for +: 'NoneType' and 'list'
error.使用 loguru 将列表写回配置文件,并收到可怕
unsupported operand type(s) for +: 'NoneType' and 'list'
错误。 I know the error came from attempting to combine a literal and a list.我知道错误来自尝试结合文字和列表。 I attempted to break down the line by using a variable to represent the literal, and this only generated the same error.
我试图通过使用变量来表示文字来分解该行,但这只会产生相同的错误。
def update_config(config: dict):
"""
Update exisitng configuration file.
Parameters
----------
config: dict
Configuraiton to be written into config file.
"""
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS))
with open("config.yaml", "w") as yaml_file:
yaml.dump(config, yaml_file, default_flow_style=False)
logger.info("Updated last read message_id to config file")
Traceback (most recent call last):
File "/telegram-downloader-quackified/media_downloader.py", line 359, in <module>
main()
File "/telegram-downloader-quackified/media_downloader.py", line 343, in main
updated_config = asyncio.get_event_loop().run_until_complete(
File "/miniconda3/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/telegram-downloader-quackified/media_downloader.py", line 324, in begin_import
update_config(config)
File "/telegram-downloader-quackified/media_downloader.py", line 35, in update_config
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS))
I know something needs to change in the phrase list(set(config["ids_to_retry"] + FAILED_IDS))
, I just am not sure what.我知道短语
list(set(config["ids_to_retry"] + FAILED_IDS))
中需要更改一些内容,我只是不确定是什么。
FAILED_IDS is a list generated when the script encounters an exception and cannot successfully complete the download. FAILED_IDS 是脚本遇到异常无法成功完成下载时生成的列表。 When this occurs the message id is stored in memory like so
FAILED_IDS.append(message.message_id)
.发生这种情况时,消息 ID 将存储在 memory 中,就像
FAILED_IDS.append(message.message_id)
一样。 Then it is written to the configuration file with the following statement:然后使用以下语句将其写入配置文件:
if FAILED_IDS:
logger.info(
"Downloading of %d files failed. "
"Failed message ids are added to config file.\n",
len(set(FAILED_IDS)),
)
update_config(updated_config)
It's original value is:它的原始值是:
FAILED_IDS: list = []
Where "ids_to_retry" is more or less just a label used in the configuration file.其中“ids_to_retry”或多或少只是配置文件中使用的 label。 It is just a literal string, and is the nonetype.
它只是一个文字字符串,并且是非类型。 The ends result is for the two to be written in the configuration file to resemble something like the following:
最终结果是将两者写入配置文件中,类似于以下内容:
ids_to_retry:
- 26125
- 2063
- 2065
- 2080
- 2081
- 22052
- 21029
- 553
- 554
- 555
- 2102
- 22074
I hope this clarifies the nature of the variables we are working with.我希望这能阐明我们正在使用的变量的性质。
The reason for failure is config["ids_to_retry"] = None
and you are trying to extend a Nonetype to a list.失败的原因是
config["ids_to_retry"] = None
并且您正在尝试将 Nonetype 扩展到列表。 You can use something like你可以使用类似的东西
config["ids_to_retry"] = list(set(config["ids_to_retry"] + FAILED_IDS)) if config["ids_to_retry"] else FAILED_IDS
So if config["ids_to_retry"]
is None
it would take config["ids_to_retry"] = FAILED_IDS
, Without trying to extending it.因此,如果
config["ids_to_retry"]
为None
,则需要config["ids_to_retry"] = FAILED_IDS
,而无需尝试扩展它。
Assumption made that FAILED_IDS
is a list if not you can type cast it to a list.假设
FAILED_IDS
是一个列表,如果不是,您可以将其强制转换为列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.