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

Резултати

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

Пребарај: #mine

当前筛选 #mine清除筛选
🏆 Premium, NFT, Tokens

@giiveaways · Post #2717 · 10.08.2024 г., 19:44

🔥 What's up with the listing of HOT and the project overall ? ⚡️ Futures on HOT can already be traded on the 🐂#WhiteBIT exchange, and the price is promising 🤘 Chart on the screenshot, 1 HOT = 19.803 USDT). When spot trading for HOT will be launched is just a matter of time... . 🙂#MineHOT here: https:://t.me/herewalletbot/app👈 🔥

🏆 Premium, NFT, Tokens

@giiveaways · Post #2556 · 26.07.2024 г., 18:31

🔥 What's up with the listing of HOT and the project overall ? ⚡️ Futures on HOT can already be traded on the 🐂#WhiteBIT exchange, and the price is promising 🤘 Chart on the screenshot, 1 HOT = 9.142 USDT). When spot trading for HOT will be launched is just a matter of time... . 🙂#MineHOT here: https:://t.me/herewalletbot/app👈 🔥

🏆 Premium, NFT, Tokens

@Giiveaways · Post #2375 · 08.07.2024 г., 08:02

🔥 What's up with the listing of HOT and the project overall ? ⚡️ Futures on #HOT can already be traded on the 🐂#WhiteBIT exchange, and the price is promising 🤘 Chart on the screenshot, 1 HOT = 6.600#USDT). When spot trading for HOT will be launched is just a matter of time... . 🙂#MineHOT here: https:://t.me/herewalletbot/app👈 ➖

🏆 Premium, NFT, Tokens

@giiveaways · Post #2462 · 15.07.2024 г., 19:42

🔥 What's up with the listing of HOT and the project overall ? ⚡️ Futures on #HOT can already be traded on the 🐂#WhiteBIT exchange, and the price is promising 🤘 Chart on the screenshot, 1 HOT = 8.438#USDT). When spot trading for HOT will be launched is just a matter of time... . 🙂#MineHOT here: https:://t.me/herewalletbot/app👈 ➖

🏆 Premium, NFT, Tokens

@Giiveaways · Post #1974 · 05.06.2024 г., 16:57

🔥 What's up with the listing of HOT and the project overall ? After #Notcoin🤑, one of the major applications that launched was: HOT Near #Wallet. "How to mine - HERE ⚡️ The developers are actively engaging in marketing and have already formed numerous collaborations. In the past two days, they partnered with #1inch and added MPC wallets. ⚡️ Direct support from #Near: it's already evident that all transactions are being recorded on the blockchain. We are #mining a real token, not just pixels on a screen! Moreover, HOT plans to launch its own blockchain. ⚡️ Futures on #HOT can already be traded on the 🐂#WhiteBIT exchange, and the price is promising 🤘 (chart on the screenshot, 1 HOT = 3.987 #USDT). When spot trading for HOT will be launched is just a matter of time... 💯 In conclusion, we can say that the game has a strong foundation and prospects for future development. 🙂#MineHOT here:👇👇 https://t.me/herewalletbot/app?startapp=200850-village-15134