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

Резултати

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

Пребарај: #january

当前筛选 #january清除筛选
APKPure_Official

@apkpurechannel · Post #2836 · 24.01.2026 г., 11:19

🆓REDEEM CODES🆓 🎁🚀AFK Arena Redeem #Codes 💫📌#January 2026 🌟🎁New Code:【 vdj82fht4r 】 💎💎💎Redeem now for 3,000 diamonds, ten time emblems, ten stargazer scrolls, and ten common scrolls!!! 👀All the Active AFK Arena Codes. Add @APKFreeDownloader_Bot to get any apk you want.

APKPure_Official

@apkpurechannel · Post #2830 · 18.01.2026 г., 11:47

🆓REDEEM CODES🆓 🎁🚀AFK Arena #Codes | #January 2026 💫🌟4 New Codes Released This Week! 👉Code 1:【 DON2026classic 】 👉Code 2:【 ujqrukd2at 】 👉Code 3:【 uj5fs5z58s 】 👉Code 4:【 u4fctemje2 】 🎁💎Redeem now for 2.2k diamond any many more!!! 🔗All working AFK Arena codes. Add @APKFreeDownloader_Bot to get any apk you want.

悦读「优质少量RSS聚合」

@Dailyrss · Post #13241 · 27.01.2026 г., 00:45

Wikipedia每日一图(#January 27, 2026)https://ift.tt/Msrmcyu “Selection” of Hungarian Jews on the ramp at Auschwitz-II-Birkenau in German-occupied Poland, May/June 1944, during the final phase of the Holocaust. Jews were sent either to forced labor or the gas chambers. 81 years ago today, on January 27, 1945, Auschwitz was liberated by the Red Army. Today is also International Holocaust Remembrance Day.. 了解更多 . #Wikipedia每日一图

悦读「优质少量RSS聚合」

@Dailyrss · Post #13240 · 26.01.2026 г., 00:45

Wikipedia每日一图(#January 26, 2026)https://ift.tt/EsDR49p Apple orchards after snowfall in Sangla in the northern Indian state of Himachal Pradesh. India has the second largest area under apple cultivation in the world and is the fifth largest producer of the fruit globally. More than 90% of India's apple production comes from just two of its northern Himalayan states: Jammu and Kashmir, and Himachal Pradesh. Today is Republic Day in India.. 了解更多 . #Wikipedia每日一图

悦读「优质少量RSS聚合」

@Dailyrss · Post #13237 · 23.01.2026 г., 00:45

Wikipedia每日一图(#January 23, 2026)https://ift.tt/oNaVYGk Northwest view of Yugar village and fields, viewed from Phuktal Gompa. Elevation 3,850 m (12,630 ft)), on the left bank of the Tsarap River (elev. 3,820 m (12,530 ft)). In this arid terrain above the tree line, villagers rely on water from the snow-fed Phu Nala (flowing from centre to bottom) for their fields. Zanskar, Ladakh, India. 了解更多 . #Wikipedia每日一图

ПретходнаСтраница 1 од 5Следна