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

Пребарај: #sats

当前筛选 #sats清除筛选
TokenPocket

@tokenpocket_channel · Post #1000 · 12.12.2023 г., 10:04

🟧 .sats domain name is live on TokenPocket_TP! 👉http://tokenpocket.pro 🌟You can view the .sats domain name directly on the TokenPocket Transfer page after you enter the #Bitcoin wallet address! Make #Bitcoin Great Again! #Ordinals#Bitcoin#SATs#TokenPocket $SATS 【Details】https://twitter.com/TokenPocket_TP/status/1734513835862798519 【Powered By】Crypto Box

TokenPocket

@tokenpocket_channel · Post #1050 · 26.12.2023 г., 09:07

🟧 Easily track the real-time #BRC20 Market on ​TokenPocket_TP! ✅Empowering you with real-time price updates and dynamic percentage changes! 📈 ✅Providing you with a more stable and convenient wallet service! 👉https://www.tokenpocket.pro/ #Bitcoin#BRC20#SATS#ORDI 【Details】https://twitter.com/TokenPocket_TP/status/1739573022779170991 【Powered By】Crypto Box

Most people lose in crypto not because of the market, but because of emotions. The solution is simple: accumulate BTC in small satoshi amounts regularly. You can start with $100 today. This is not financial advice. @saylor@APompliano@aantonop #Bitcoin#BTC#Sats#HODL#CryptoEducation#MyMining

TokenPocket

@tokenpocket_channel · Post #1033 · 21.12.2023 г., 05:33

✨ Unlock simplicity in #BRC20 transfer! 🚀Now, easily transfer your #BRC20 assets in just ONE step! 🔄 Inscribe and transfer seamlessly for a more efficient and convenient experience. 👉https://tokenpocket.pro Ready to experience seamless #BRC20 transactions? Follow the tutorial: 👉https://help.tokenpocket.pro/en/wallet-faq-en/btc-wallet/brc20 #Sats#ordi#RATS#Inscriptions 【Details】https://twitter.com/TokenPocket_TP/status/1737707370523005390 【Powered By】Crypto Box

TokenPocket

@tokenpocket_channel · Post #1025 · 19.12.2023 г., 03:22

🟧 TokenPocket Now Fully supports BRC-20! #BRC20 ✅#BRC20 tokens deposit, withdrawal, and token price display. ✅ Transfer your #BRC20 in one single transaction (Inscribe and Transfer in one step) 🆕We just released the iOS new version 🔺2.1.9🔺(TP Wallet) and Android new version 🔺1.9.5🔺. Upgrade your TP Wallet/TokenPocket and discover the #BRC20#Inscriptions world with us! 👉https://www.tokenpocket.pro/ #Sats#ordi#RATS#Inscriptions 【Details】https://twitter.com/TokenPocket_TP/status/1736949473048420555 【Powered By】Crypto Box

TokenPocket

@tokenpocket_channel · Post #1029 · 20.12.2023 г., 09:36

🟧Easily Track the hottest #BRC20 tokens on ​TokenPocket_TP! Still not too late to know this secret! 😍 ✅Click the "+" button on the #Bitcoin Asset page, and you can view the trending token list directly! Figure out what are most TokenPocket users interested in and get a chance to find a #GEM💎 with your #TokenPocket Wallet! 👉https://www.tokenpocket.pro/ #ORDI#SATs#BIIS#RATS 【Details】https://twitter.com/TokenPocket_TP/status/1737405949722366261 【Powered By】Crypto Box