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

Резултати

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

Пребарај: #obit

当前筛选 #obit清除筛选
NFT & Airdrop News

@airdrop_nft1 · Post #390 · 22.01.2024 г., 17:08

🎉 Hey Crypto Enthusiasts! 🚀 Get ready for the launch of an exciting new token – OneBit (OBIT)! 🌟 🪙 Token Name: OneBit 💹 Symbol: OBIT 🎁 Don't miss our incredible Airdrop: 👥 1 USER = 200 OBIT Free! 🚀 PreSale Blast-off: 👉 1 USER = 10,000 OBIT + 0.05 BNB 🌐 Unlock more rewards with our referral program: 🔗 Referal Airdrop: +25% OBIT 🔗 Referal PreSale: Grab an extra 25% OBIT 📌 Secure your Airdrop on our official website: onebit.store/airdrop/index.html 📌Official Project Chat https://t.me/OneBit_Chat Join us on this thrilling crypto journey with OneBit! 🚀✨ Don't miss out on this opportunity to be part of the future of finance! 🌐💰#OneBit#OBIT#CryptoRevolution🚀🌟 ‼️ Please remember: We are airdrop hunters and only participate in free airdrops, please do not buy any airdrop token

Airdrop Market Pro

@airdrop_market_pro · Post #390 · 11.12.2023 г., 20:07

We are thrilled to unveil our revamped pre-sale page, promising a lightning-fast and more animated experience! 🌐✨ But wait, there's more – we're introducing a game-changing feature to supercharge our token value: the Automatic Token Burn function! 🔥 In every pre-sale, when you make a purchase without referrals using any amount of BNB, that exact amount will be automatically burned from the total supply. 🔄 This strategic move is crafted to reinforce our token's value, aiming to maintain 1 OBIT = $0.01. 📈💰 Join us on this exhilarating journey toward a stronger and more vibrant community! 🌟💪 🔗 Website: onebit.store 💸 Airdrop: onebit.store/airdrop/index.html 🚨 New Update: Purchase 10,000 OBIT for only 0.05 BNB – offer is limited! Plus, if you join without a referral, 25% of the purchased amount is burned from the total supply each time. 🔄 #CryptoNews#TokenBurn#PresaleMagic#OBIT#ToTheMoon🚀🌕 🌈 Embark on this journey with us and let's make history together! 🚀🚀🚀 https://www.youtube.com/shorts/S4iu3jA6Kac

Airdrop Market Pro

@airdrop_market_pro · Post #388 · 04.12.2023 г., 17:38

🚀 Exciting News! 🚀 Dear Friends, we have revamped our pre-sale page for a lightning-fast and more animated experience! 🌐✨ But that's not all – we've introduced a game-changing feature to boost our token value: the Automatic Token Burn function! 🔥 Now, in every pre-sale, if you make a purchase without referrals using any amount of BNB, that exact amount will be automatically burned from the total supply. 🔄 This strategic move is designed to fortify our token's value and aims to keep 1 OBIT = $0.01. 📈💰 Join us in this exciting journey towards a stronger and more vibrant community! 🌟💪 Website: https://onebit.store/ Airdrop: https://onebit.store/airdrop/index.html #CryptoNews#TokenBurn#PresaleMagic#OBIT#ToTheMoon🚀

NFT & Airdrop News

@airdrop_nft1 · Post #385 · 11.12.2023 г., 20:07

We are thrilled to unveil our revamped pre-sale page, promising a lightning-fast and more animated experience! 🌐✨ But wait, there's more – we're introducing a game-changing feature to supercharge our token value: the Automatic Token Burn function! 🔥 In every pre-sale, when you make a purchase without referrals using any amount of BNB, that exact amount will be automatically burned from the total supply. 🔄 This strategic move is crafted to reinforce our token's value, aiming to maintain 1 OBIT = $0.01. 📈💰 Join us on this exhilarating journey toward a stronger and more vibrant community! 🌟💪 🔗 Website: onebit.store 💸 Airdrop: onebit.store/airdrop/index.html 🚨 New Update: Purchase 10,000 OBIT for only 0.05 BNB – offer is limited! Plus, if you join without a referral, 25% of the purchased amount is burned from the total supply each time. 🔄 #CryptoNews#TokenBurn#PresaleMagic#OBIT#ToTheMoon🚀🌕 🌈 Embark on this journey with us and let's make history together! 🚀🚀🚀 https://www.youtube.com/shorts/S4iu3jA6Kac

NFT & Airdrop News

@airdrop_nft1 · Post #382 · 04.12.2023 г., 17:38

🚀 Exciting News! 🚀 Dear Friends, we have revamped our pre-sale page for a lightning-fast and more animated experience! 🌐✨ But that's not all – we've introduced a game-changing feature to boost our token value: the Automatic Token Burn function! 🔥 Now, in every pre-sale, if you make a purchase without referrals using any amount of BNB, that exact amount will be automatically burned from the total supply. 🔄 This strategic move is designed to fortify our token's value and aims to keep 1 OBIT = $0.01. 📈💰 Join us in this exciting journey towards a stronger and more vibrant community! 🌟💪 Website: https://onebit.store/ Airdrop: https://onebit.store/airdrop/index.html #CryptoNews#TokenBurn#PresaleMagic#OBIT#ToTheMoon🚀

Free Airdrop News 🔥🔥🔥

@free_airdrop_nn · Post #214 · 11.12.2023 г., 20:06

We are thrilled to unveil our revamped pre-sale page, promising a lightning-fast and more animated experience! 🌐✨ But wait, there's more – we're introducing a game-changing feature to supercharge our token value: the Automatic Token Burn function! 🔥 In every pre-sale, when you make a purchase without referrals using any amount of BNB, that exact amount will be automatically burned from the total supply. 🔄 This strategic move is crafted to reinforce our token's value, aiming to maintain 1 OBIT = $0.01. 📈💰 Join us on this exhilarating journey toward a stronger and more vibrant community! 🌟💪 🔗 Website: onebit.store 💸 Airdrop: onebit.store/airdrop/index.html 🚨 New Update: Purchase 10,000 OBIT for only 0.05 BNB – offer is limited! Plus, if you join without a referral, 25% of the purchased amount is burned from the total supply each time. 🔄 #CryptoNews#TokenBurn#PresaleMagic#OBIT#ToTheMoon🚀🌕 🌈 Embark on this journey with us and let's make history together! 🚀🚀🚀 https://www.youtube.com/shorts/S4iu3jA6Kac