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

Резултати

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

Пребарај: #ageofmars

当前筛选 #ageofmars清除筛选

💵 How to make money in the P2E game Age of Mars? 💵 AGE OF MARS is a 3D game in which you conquer Mars. You will need to produce water, oxygen, and rocket fuel to trade these resources for real money How it works 👇 1️⃣ Buy Aqua NFT and (or) Planta NFT of any rarity - from common to legendary. The higher the rarity of an NFT, the greater its profitability. 2️⃣ Place your NFTs on the playing field. They will become in-game assets and will generate daily resources that can be exchanged for MRT tokens. This token is traded on the Pancake Swap exchange as MRT/USDT pair, which means you can instantly top-up your USDT wallet. 3️⃣ Increase your income - upgrade your NFTs to level 10. Yield at Level 10 can be 101% higher than at Level 1. 4️⃣ Try minting - create a new NFT from two existing ones. Minting is available for Level 10 tokens. You can play together with your team members and earn passive income - up to 15% from NFT sales and up to 80% from all resources generated in your team. 💰How to start earning 💰 ➡️Register using the link ➡️ Top up your balance ➡️ Buy NFTs on the marketplace ➡️ Place them on the in-game map ➡️ Collect resources and exchange them for USDT REGISTRATION | TELEGRAM CHANNEL #P2E#Play2Earn#AgeOfMars

​​🎙TOP TIER PROJECT ANNOUNCEMENT 🔥🔥🔥 👉Age of Mars - a P2E game. Make money on the colonization of Mars 💵👽💵 📌A whole new generation of P2E games is coming: they offer a free 2 play model, high-quality graphics, addictive gameplay, and a sustainable economic model. It is a fully-fledged 3D game in which you are required to turn Mars into a habitable planet. 📌The spaceship will take you to Mars, where your mission will be to create a habitable planet. You will get to develop your base and extract liquid resources that can be instantly converted into MRT utility tokens (MRT 👉 USDT). 👩‍🚀 Level up your NFTs and earn more. Depending on the rarity of the NFT, the yield on level 10 can be 101% higher than on level 1. In addition, level 10 unlocks Minting, the process of creating a new NFT at the expense of two NFTs you already own. 🧨 Age of Mars has a dedicated game mode for teams. You can earn up to 15% from NFT sales and up to 80% of the resources generated in your team. These are the best rewards across all blockchain games! 🤌 👉You can join the conquest of Mars by following the link.🔥🔥🔥 REGISTRATION| Telegram #P2E#Play2Earn#AgeOfMars