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

Резултати

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

Пребарај: #bole

当前筛选 #bole清除筛选
Addis Standard

@addisstandardeng · Post #21757 · 18.03.2026 г., 09:03

#Ethiopia: Report warns of arbitrary detentions, growing restrictions on media in Addis Ababa ------- A new report by Human Rights First has raised concerns over alleged arbitrary detentions and increasing restrictions on media in #Addis_Ababa, particularly during periods of major international events. The report, titled “Situational Analysis of Human Rights in Addis Ababa,” states that security forces carried out “pre-emptive detentions” across several parts of the capital without due process. According to the findings, detentions were recorded in sub-cities including #Yeka, #Bole, and #Akaki_Kality, where individuals were held for more than a week without being charged or appearing before court. In addition, the report highlighted what it described as a growing pattern of restrictive actions against media between 2025 and early 2026. Read more: https://addisstandard.com/?p=55925

Addis Standard

@addisstandardeng · Post #21146 · 21.01.2026 г., 14:29

News: Report tracks #UAE-linked cargo flights to #Ethiopian military base amid regional power struggle A cargo aircraft previously linked to the supply of weapons to armed groups in #Sudan has conducted multiple flights to Ethiopia in recent weeks, according to an exclusive investigation by the UK-based outlet Middle East Eye (MEE), raising questions about Ethiopia’s role in intensifying regional rivalries tied to the war in Sudan. MEE reported that an Antonov An-124 cargo plane operated by UAE-based Maximus Air, and allegedly linked to Emirati military logistics, made repeated flights between #Abu_Dhabi and #Harar_Meda airbase, the main base of the Ethiopian Air Force. Flight-tracking data reviewed by the outlet shows at least four trips to Harar Meda between 3 and 17 January, including one flight that later continued to Addis Abeba’s #Bole International Airport before departing for #Europe and #Asia. While the purpose of the flights remains unclear... Read more: https://addisstandard.com/?p=54681

Addis Standard

@addisstandardeng · Post #21141 · 21.01.2026 г., 08:56

News: #Ethiopian Airlines confirms the purchase of nine #Boeing 787-9 Dreamliner, partners with Bridgestone on airport safety Ethiopian Airlines Group has confirmed the purchase of nine Boeing 787-9 #Dreamliner aircraft while launching a collaboration with #Bridgestone Corporation to enhance airport safety at Addis Ababa #Bole International Airport. The airline said the newly ordered Boeing 787-9 jets, scheduled for delivery between 2031 and 2033, will support network expansion and strengthen international connectivity. This order follows Ethiopian Airlines Group CEO #Mesfin_Tasew said the order demonstrates the carrier’s strategy to modernize its fleet with advanced, fuel-efficient aircraft while improving customer service. Boeing’s Vice President of Commercial Sales and Marketing for Africa, #Anbessie_Yitbarek, described the 787 Dreamliner family as a “game-changer” for connecting Africa with global markets. In parallel with its fleet expansion,... Read more: https://addisstandard.com/?p=54671