TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

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

Резултати

Пронајдени 2 слични објави

Пребарај: #trumptoken

当前筛选 #trumptoken清除筛选
Crypto M - Crypto News

@CryptoM · Post #64635 · 09.04.2026 г., 12:15

🚀 Trump to Attend Meme Coin Conference This Month U.S. President Donald Trump is set to participate in a Meme Coin conference later this month. According to BlockBeats, this event will include a grand luncheon for holders of the TRUMP token. Trump will be joined by 18 "superstars" at the luncheon, which is scheduled for April 25 at Mar-a-Lago. The top 297 token holders will be allowed to attend the luncheon, while the top 29 will receive invitations to a special VIP reception and champagne toast alongside President Trump. #Trump#MemeCoin#TRUMPtoken#MarALago#cryptocurrency#blockchain#VIPevent#superstars#champagnetoast

Crypto M - Crypto News

@CryptoM · Post #64720 · 09.04.2026 г., 15:54

🚀 TRUMP Token Extends Luncheon Event Deadline The TRUMP token has announced an extension of its luncheon leaderboard event by four days, with the new deadline set for April 14 at 12 p.m. Eastern Time. According to Foresight News, U.S. President Donald Trump is set to host a grand luncheon for TRUMP token holders, where he will be joined by 18 'superstars.' The top 297 participants will be granted access to the luncheon at Mar-a-Lago on April 25. Additionally, the top 29 holders will receive invitations to a special VIP reception and champagne toast, where they will have the opportunity to meet President Trump. #TRUMPTOKEN#luncheonevent#deadlineextension#DonaldTrump#superstars#Maralago#VIPreception#champagnetoast#ForesightNews#eventnews