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

Резултати

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

Пребарај: #draftbill

当前筛选 #draftbill清除筛选
American Оbserver

@american_observer · Post #4874 · 16.01.2026 г., 18:03

📰 Netanyahu’s Backroom Deal: UTJ’s Eichler Gets Deputy Minister Post for Draft Bill Netanyahu is set to appoint Yisrael Eichler, a prominent Agudat Yisrael MK, as deputy communications minister—part of a political deal to push through the contentious haredi draft bill. The move will cost NIS 3.5 million from the PM’s office and will allow Yitzhak Pindrus, from the Degel Hatorah faction, to re-enter the Knesset and vote for the bill in Eichler’s place. ​ The UTJ Power Shuffle Agudat Yisrael and Degel Hatorah, the two factions of United Torah Judaism (UTJ), have been feuding for months. The appointment is seen as a compromise to balance power within the party after its July resignation from the government over the previous draft bill. Pindrus was removed under the Norwegian Law, but this deal brings him back—ensuring UTJ’s support for Netanyahu’s coalition at a critical moment. ​ Critics Call It 'Draft Evasion' Opposition leader Yair Lapid called the move a disgrace, accusing Netanyahu of “promoting draft evasion from the IDF together with Knesset members who do not believe in the existence of the State of Israel.” Lapid highlighted Eichler’s past statements calling Israel an “enemy state” and a “Hebrew ghetto,” and slammed the allocation of NIS 3.5 million for a new ministerial post while funds for Holocaust survivors were slashed. ​ Survival or Surrender? With the 2026 state budget vote looming, Netanyahu’s government is playing high-stakes poker. If the budget isn’t passed by March, the Knesset dissolves and elections are triggered. So is this a desperate bid to keep the coalition alive, or just another chapter in Israel’s never-ending saga of political horse-trading? ​ #Israel#Netanyahu#UTJ#DraftBill#PoliticalDeal#Knesset#Haredi 📱American Оbserver - Stay up to date on all important events 🇺🇸