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

Резултати

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

Пребарај: #cityholder

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

@giiveaways · Post #3395 · 27.12.2024 г., 20:22

CITY Holder: Welcome to the country! Join us at DROGON!🐉 Let's build the best country in the simulation and compete for a prize of 1000 TON🤑 👉 @cityholderdrogon city 👈 #cityholder

Hashtags

🏆 Premium, NFT, Tokens

@giiveaways · Post #3316 · 29.11.2024 г., 15:38

🗓Combo of the Day: 29.11.2024 💰 In #CityHolder, you get +7,500,000 coins every day for the Combo of the Day! The combo is updated at 2:00 PM. Tg Today's Combo: ✅Country Cottage -Residential buildings ✅Old Town - Residential buildings ✅HistoricMansion -Residential buildings ❓How to collect the combo in City Holder? Go to @CityHolder. Open the "Construction" section. Select the required combo cards. 👉 Join @CityHolder! #CityHolder

Hashtags

🏆 Premium, NFT, Tokens

@giiveaways · Post #3307 · 23.11.2024 г., 18:52

🗓Combo of the Day: 23.11.2024 💰 In #CityHolder, you get +7,500,000 coins every day for the Combo of the Day! The combo is updated at 2:00 PM. Today's Combo: ✅ Shopping Center ✅ Business Center ✅ Shopping Arcade ❓How to collect the combo in City Holder? Go to @CityHolder. Open the "Construction" section. Select the required combo cards. Daily Quiz 23.11.2024 👇 1. What is the type of the cryptocurrency designed to ensure price stability? Answer: Stablecoin 2. Which city is considered the cultural capital of Russia? Answer: Saint Petersburg 3. Which musical instrument was invented by Italians? Answer: Mandolin 4. In what year did Bitcoin appear? Answer: 2009 5. In which country is Kruger National Park located? Answer: South Africa 6. Which of these dishes is a national dish of Malaysia? Answer: Satay 7. Which dance style originated in Brazil? Answer: Samba 8. What is the official language of Brazil? Answer: Portuguese 9. Which city hotel is known as the most luxurious in the world? Answer: Burj Al Arab 10. What is the name of the Kyrgyz national epic? Answer: Manas 👉 Join @CityHolder! #CityHolder

Hashtags

🏆 Premium, NFT, Tokens

@giiveaways · Post #3304 · 22.11.2024 г., 16:04

🗓Combo of the Day: 22.11.2024 💰 In #CityHolder, you get +7,500,000 coins every day for the Combo of the Day! The combo is updated at 2:00 PM. Today's Combo: ✅ Agrocomplex - Industrial facilities ✅ Fish Farm - Industrial facilities ✅ Mill - Industrial facilities ❓How to collect the combo in City Holder? Go to @CityHolder. Open the "Construction" section. Select the required combo cards. Quiz 👇 1. / Guatemala 2. / Goulash 3. / SHA- 256 4. / Milan 5. / Indians 6. / Complete anonymity 7./ Jordan 8. / Juhannus 9. / Evan Duffield 10. / Genesis Block 👉 Join @CityHolder! #CityHolder

Hashtags

🏢 Daily Quiz :16 Jul 🔑Q.1 Ans. :GPS 🔑Q.2 Ans. :Bluetooth 🔑Q.3 Ans. :Miso soup 🔑Q.4 Ans. :Magnetometry 🔑Q.5 Ans. :Malay Archipelago 🔑Q.6 Ans. :Isaac Asimov 🔑Q.7 Ans. :Spacewar! 🔑Q.8 Ans. :Rigoletto 🔑Q.9 Ans. :Johannes Gutenberg 🔑Q.10 Ans. :Friedrich Schiller 🔗 Link :City Holder #cityholder ❤️ If you appreciate our work, tap a like or heart to support — we’ll keep bringing top passive income projects!

Hashtags

🏢 Daily Quiz :15 Jul 🔑Q.1 Ans. :Astana 🔑Q.2 Ans. :Sodium 🔑Q.3 Ans. :Hogwarts 🔑Q.4 Ans. :Haruki Murakami 🔑Q.5 Ans. :Apex Legends 🔑Q.6 Ans. :Riga 🔑Q.7 Ans. :Lead 🔑Q.8 Ans. :MR 🔑Q.9 Ans. :The Hurt Locker 🔑Q.10 Ans. :James Watt 🔗 Link :City Holder #cityholder ❤️ If you appreciate our work, tap a like or heart to support — we’ll keep bringing top passive income projects!

Hashtags

🏢 Daily Quiz :14 Jul 🔑Q.1 Ans. :Jeff Bezos 🔑Q.2 Ans. :Decentralization 🔑Q.3 Ans. :Wellington 🔑Q.4 Ans. :Forrest Gump 🔑Q.5 Ans. :Gobi 🔑Q.6 Ans. :Dow Jones 🔑Q.7 Ans. :Pokémon GO 🔑Q.8 Ans. :Gabriel García Márquez 🔑Q.9 Ans. :Jack Dorsey 🔑Q.10 Ans. :Guido van Rossum 🔗 Link :City Holder #cityholder ❤️ If you appreciate our work, tap a like or heart to support — we’ll keep bringing top passive income projects!

Hashtags

🏢 Daily Quiz :13 Jul 🔑Q.1 Ans. :Antoine de Saint-Exupéry 🔑Q.2 Ans. :The Scream 🔑Q.3 Ans. :Paradise Lost 🔑Q.4 Ans. :In Search of Lost Time 🔑Q.5 Ans. :Magical Realism 🔑Q.6 Ans. :Alexander Pushkin 🔑Q.7 Ans. :Data Mining 🔑Q.8 Ans. :Decentralization 🔑Q.9 Ans. :Psychologism 🔑Q.10 Ans. :Anton Chekhov 🔗 Link :City Holder #cityholder ❤️ If you appreciate our work, tap a like or heart to support — we’ll keep bringing top passive income projects!

Hashtags

🏢 Daily Quiz :12 Jul 🔑Q.1 Ans. :Evaporation 🔑Q.2 Ans. :Electron 🔑Q.3 Ans. :Atomic physics 🔑Q.4 Ans. :Electrolysis 🔑Q.5 Ans. :Blockchain 🔑Q.6 Ans. :Jupiter 🔑Q.7 Ans. :Ethology 🔑Q.8 Ans. :Troposphere 🔑Q.9 Ans. :Mitochondrion 🔑Q.10 Ans. :Dispersion 🔗 Link :City Holder #cityholder ❤️ If you appreciate our work, tap a like or heart to support — we’ll keep bringing top passive income projects!

Hashtags

123•••101112
ПретходнаСтраница 1 од 12Следна