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

Резултати

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

Пребарај: #movement

当前筛选 #movement清除筛选
Amazing Geography 🌍

@amazingeo · Post #672 · 25.03.2026 г., 20:31

🌍 The world’s biggest human migration still happens every year in China, as hundreds of millions travel during Lunar New Year— the largest annual movement of people on Earth. ✨ #migration⚡#population⚡#movement⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography 👉more Channels ​

Amazing Geography 🌍

@amazingeo · Post #592 · 25.01.2026 г., 12:31

🌍 In 2022, the number of international migrants reached an estimated 281 million—more than triple the figure from 1970—making up 3.6% of the world’s total population today. ✨ #migration⚡#demographics⚡#movement⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography 👉more Channels ​

Amazing Geography 🌍

@amazingeo · Post #175 · 04.09.2025 г., 16:12

🌍 Since the 1990s, the number of people living outside their birthplace has more than doubled, with most international migration now happening between countries in Asia and Europe. ✨ #migration⚡#demography⚡#movement⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography🌍 ​

Amazing Geography 🌍

@amazingeo · Post #88 · 21.08.2025 г., 09:12

🌍 Tokyo’s Shibuya Crossing is one of the busiest pedestrian intersections in the world, where as many as 3,000 people can cross with every green light, illustrating dense urban flow. ✨ #city⚡#infrastructure⚡#movement⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography🌍

Crypto Media

@crypto_medias · Post #1548 · 10.03.2025 г., 09:01

🚀 Top Crypto Events This Week (March 10-17) 🔹 March 10 – AMA: #Movement x Bitget (12 PM UTC) 🔹 March 11 – Zen 5.0.6 Upgrade (#Horizen) 🆕 🔹 March 12 – 11.31M Token Unlock (#Aptos) 💰 🔹 March 14 – RavenQuest Global Release (#Immutable) 🎮 🔹 March 16 – 92.65M Token Unlock (#Arbitrum) ⚡ Big moves ahead – Stay sharp! 🚀