@Telegraher · Post #633 · 29.01.2023 г., 12:47
Hahaha, eat that! 1989 #JinPooh#freetibet#西藏独立运动#西藏独立
Hashtags
TGINSIGHT SIMILAR POSTS
Изворен канал @pythonotes · Post #397 · 12 ное.
Использование Pydantic сегодня стало нормой, и это правильно. Но иногда на ревью вижу, что используют его не всегда корректно. Например, метод BaseModel.model_dump() по умолчанию не преобразует стандартные типы, такие как datetime, UUID или Decimal, в простой сериализуемый для JSON вид. Тогда пишут кастмоный сериализатор для этих типов чтобы функция json.dump() не падала с ошибкой. import uuid from datetime import datetime from decimal import Decimal from uuid import UUID from pydantic import BaseModel class MyModel(BaseModel): id: UUID date: datetime value: Decimal obj = MyModel( id=uuid.uuid4(), date=datetime.now(), value='1.23' ) print(obj.model_dump()) # не подходит для json.dump # { # 'id': UUID('4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1'), # 'date': datetime.datetime(2025, 12, 12, 12, 12, 12, 111111), # 'value': Decimal('1.23') # } # добавляем свой кастомный сериализатор json.dumps(obj.model_dump(), cls=MySerializer) # { # 'id': '4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1', # 'date': '2025-12-12T12:12:12.111111', # 'value': '1.23' # } В данном случае класс MySerializer обрабатывает datetime, UUID и Decimal. Например так: class MySerializer(json.JSONEncoder): def default(self, o): if isinstance(o, Decimal): return str(o) elif isinstance(o, datetime): return o.isoformat() elif isinstance(o, UUID): return str(o) return super().default(o) Специально для тех, кто всё еще так делает - в этом нет необходимости! Pydantic может это сделать сам, просто нужно добавить параметр mode="json". json.dumps(obj.model_dump(mode="json")) # { # 'id': '4f8c1bc4-25fd-40cd-9dbe-2c73639b0dc1', # 'date': '2012-12-12T12:12:12.111111', # 'value': '1.23' # } #pydantic#libs
Пребарај: #jinpooh
@Telegraher · Post #633 · 29.01.2023 г., 12:47
Hahaha, eat that! 1989 #JinPooh#freetibet#西藏独立运动#西藏独立
Hashtags
@Telegraher · Post #519 · 26.07.2022 г., 20:55
Folks, some handy tips: • read comments before commenting • vanilla tg OR our fork also have "translate" button If you commenting w/o reading the discussion.. Well, go on reddit 😁 More details: post about Nekogram picked up from NekoX chat. Moar details: • Nekogram is a bitch who ban you for political reasons and opinions like 1989 #JinPooh#freetibet#西藏独立运动#西藏独立 . • NekoX can be a bitchie and ban you when you critic Durov or TG TOS but they always unban you, while i'm still banned in Nekogram chat (i was banned twice in both and forever in Nekogram). My political messages are still in NekoX chat 😎
Hashtags
@Telegraher · Post #516 · 23.07.2022 г., 08:55
Hallo folks, fond this stuff in NekoX chat: https://github.com/NekogramX/Nekogram - link is fuckedup by github probably (upd). Fake it or not?.. Well, nekogram assholes are banned me also for politic content: 1989 #JinPooh#freetibet#西藏独立运动#西藏独立 Here https://t.me/telegraher/74 And here https://t.me/telegraher/95 https://t.me/telegraher/97 Ban + wipe any forbidden in china content. Text translated by chinese guy from nekox chat, original messages are in our chat. P.S. NekoX are bitchies also, but a much less, they're just for TG's TOS & Durov, not the chinese Gov, thats important différence. FREETIBET !
Hashtags