Skip to content

Commit 884c58d

Browse files
committed
♻️ 使用asdict替代__dict__以序列化数据类
1 parent 29f3072 commit 884c58d

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/nonebot_plugin_nyaturingtest/image_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import base64
2-
from dataclasses import dataclass
2+
from dataclasses import asdict, dataclass
33
import hashlib
44
import io
55
import json
@@ -40,7 +40,7 @@ def to_json(self) -> str:
4040
"""
4141
将对象转换为JSON字符串
4242
"""
43-
return json.dumps(self.__dict__, ensure_ascii=False)
43+
return json.dumps(asdict(self), ensure_ascii=False)
4444

4545
@staticmethod
4646
def from_json(json_str: str) -> "ImageWithDescription":

src/nonebot_plugin_nyaturingtest/presets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dataclasses import dataclass, field
1+
from dataclasses import asdict, dataclass, field
22
import json
33
import os
44

@@ -65,7 +65,7 @@ def _load_presets_from_directory(directory: str = f"{store.get_plugin_config_dir
6565
if not os.path.exists(directory):
6666
os.makedirs(directory)
6767
with open(os.path.join(directory, "喵喵.json"), "w", encoding="utf-8") as f:
68-
json.dump(_猫娘预设.__dict__, f, ensure_ascii=False, indent=4)
68+
json.dump(asdict(_猫娘预设), f, ensure_ascii=False, indent=4)
6969
for filename in os.listdir(directory):
7070
if filename.endswith(".json"):
7171
path = os.path.join(directory, filename)

src/nonebot_plugin_nyaturingtest/session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import deque
22
from collections.abc import Awaitable, Callable
3-
from dataclasses import dataclass
3+
from dataclasses import asdict, dataclass
44
from datetime import datetime
55
from enum import Enum
66
import json
@@ -420,7 +420,7 @@ async def __feedback_stage(
420420
[
421421
{
422422
"user_name": profile.user_id,
423-
"emotion_tends_to_user": profile.emotion.__dict__,
423+
"emotion_tends_to_user": asdict(profile.emotion),
424424
}
425425
for profile in related_profiles
426426
],
@@ -751,7 +751,7 @@ async def __chat_stage(
751751
[
752752
{
753753
"user_name": profile.user_id,
754-
"emotion_tends_to_user": profile.emotion.__dict__,
754+
"emotion_tends_to_user": asdict(profile.emotion),
755755
}
756756
for profile in related_profiles
757757
],

0 commit comments

Comments
 (0)