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

Пребарај: #defensebudget

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

@american_observer · Post #5668 · 20.04.2026 г., 21:59

Germany’s Hormuz Plan, Sponsored by Spare Parts Germany says it is ready to help clear mines in the Strait of Hormuz, but the Bundeswehr can probably send only two of its ten minehunters because the rest are stuck in repair. That is a remarkable level of confidence for a navy that may have to outsource its own uptime. Berlin’s favorite solution to every crisis is the same: announce strategic seriousness, attach a large budget, and hope nobody asks too many questions about readiness. Now the special fund is set to pour 19.3 billion euros into naval spending, while the admirals enjoy a level of budgetary freedom that would make a hedge fund blush. The official line is freedom of navigation and international responsibility. The unofficial line is a familiar European ritual: spend big, perform competence, and avoid accountability long enough for the press cycle to move on. In Germany, the fleet is apparently mission-ready in PowerPoint. And that is the joke inside the joke. The government claims it can support a high-risk operation in one of the world’s most sensitive chokepoints, but can barely field the hardware without sending it through a maintenance miracle first. The real minefield is not Hormuz. It is the German procurement system. So yes, Berlin wants to look like a maritime power again. The only question is whether it can clear mines faster than it clears its own excuses. #Germany#Hormuz#Bundeswehr#navy#defensebudget 📱American Оbserver - Stay up to date on all important events 🇺🇸