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

Резултати

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

Пребарај: #karaoketycoon

当前筛选 #karaoketycoon清除筛选
SingSing Announcement

@singsingchannel · Post #782 · 06.09.2024 г., 15:24

Dive into SingSing Tycoon NFTs🚀 🎤 SingSing Tycoon Game has 2 types of NFTs: ✅KTV NFTs: Unlock & upgrade floors to expand your karaoke mall 🏢 ✅Karaoke Box NFTs: Boost earnings from each booth, maximize your profits 💸 🔥Owning both to hold your key to building the ultimate karaoke empire with following features: 1️⃣Upgrade floors & booths to boost earnings 2️⃣Speed up customer flow, elevators, & cashier efficiency 3️⃣Spin for NFTs & use Ruby to fuse them into higher tiers 🎰 4️⃣Expand endlessly with new floors & booths—no limits! 🏗 5️⃣Ready to be the Karaoke King? 👑 Stay tuned! 🎶 #SingSing#KaraokeTycoon#NFT#GameFi

SingSing Announcement

@singsingchannel · Post #781 · 06.09.2024 г., 11:50

🤔 Are you ready to run a KTV Empire with SingSing Tycoon Game? 💸 Upgrade your NFTs to add more floors and boost your chain’s potential as well as increase revenue! 🚍 Buses drop off eager customers, ready to belt out tunes! 💸 Collect cash or unlock auto-collect to keep the money flowing! 🎡 Feeling lucky? Spin to win Rarer Karaoke NFTs! 🏗 Expand your karaoke chain and dominate the scene! Ready to be the Karaoke King? 👑 Stay tuned for more! 🎶 #SingSingGame#KaraokeTycoon#NFT#GameFi

SingSing Announcement

@singsingchannel · Post #813 · 20.09.2024 г., 09:03

🚨 New Tycoon Game Coming Soon! 🚨 Don't miss out on all the exciting updates! 🎉 Make sure to update your bookmarks with SingSing's latest social channels for valuable info on our upcoming releases: 🔗 Telegram Channel: https://t.me/singsingchannel 🔗 Telegram Chat: https://t.me/singsingglobal 🔗 X (Twitter): https://x.com/singsingglobal 🔗 Website: https://singsing.net/ 🔗 Game Mini App: https://t.me/SingSing_TG_bot Stay tuned, something BIG is coming! 🎮✨ #SingSing#KaraokeTycoon#NFT#GameFi#StayUpdated Announcement | Chat | X (Twitter) | Website | Game