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 слични објави

Пребарај: #tearline

当前筛选 #tearline清除筛选
Dataline Channel

@tearline_news · Post #74 · 19.11.2024 г., 13:00

🦉🚨Node Upgrade🚨🦉 Tearline AI app just rolled out a node upgrade! 🔥 This update will revamp power & compute calculations, ensuring a more equitable and transparent reward system. Embrace fairer perks and a stronger #Tearline. Plus, enjoy an easier-to-use sharing button in chat! Details👉https://medium.com/@tearlineAI/tearline-node-upgrade-and-system-changes-3053e78abee1

Hashtags

Dataline Channel

@tearline_news · Post #56 · 22.10.2024 г., 14:36

🌐✨#Tearline just dropped on the 🔝 AI tool directories — Toolify.ai & TopAI.tools! 🔗 Toolify: toolify.ai/tool/tearline 🔗 TopAI: topai.tools/t/tearline We’re blowing up in Web3 & tech. What are you waiting for? Start earning more today! 💸

Hashtags

Dataline Channel

@tearline_news · Post #26 · 26.07.2024 г., 13:03

Watch out, Tearline is leveling up - Your chat mate just got a turbo boost! 🦉💬 ✅Chat history - securely stored ✅Full-context convo - like talking to real person ✅Multi-window chat - as smooth as cutting melting butt ✅Multi-language support, sure thing! 🇰🇷🇯🇵🇷🇺Korean, Japanese, Russian, Traditional Chinese speakers🤖️#Tearline now speaks the same language with you! Let's rock those chats! Play free, Earn your Power & Compute to secure airdrops! 🔐💫

Hashtags

Dataline Channel

@tearline_news · Post #7 · 13.07.2024 г., 12:54

Exciting news! 🦉#Tearline Bot will soon be seamlessly available on Telegram. 🚀 1. Access a real-time Web3 research engine instantly. 2. Get daily consulting to enhance your trading assistant. 3. Grab community points by joining now! http://t.me/tearlinexyz Stay tuned for expanding service & ecosystem support📈✨

Hashtags

Dataline Channel

@tearline_news · Post #93 · 19.03.2025 г., 05:44

📢Tearline Points Program Update🚀 🔹 Phase 1 Points Snapshot Completed! 🎯 A snapshot of Phase 1 points will be taken on March 20 at 10:00 AM (UTC+8). These points have been securely recorded and will be a key reference for future TGE airdrops. 🔹 Phase 2 Starts NOW! 🌟 The next phase of the points program will officially begin on March 20 at 10:00 AM (UTC+8). You can now earn points across both the Mini App and the Web version. Log in with your Telegram account to seamlessly continue your journey. Start earning today 👉https://www.tearline.io 📖 Learn more on Medium: https://medium.com/tearline/the-future-of-web3-interaction-tearlines-ai-and-multi-chain-strategy-aac34e5c856a #Web3#Tearline#Airdrop#BNBChain