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

Резултати

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

Пребарај: #barrios

当前筛选 #barrios清除筛选
Venezuelanalysis

@venanalysis · Post #2340 · 18.04.2026 г., 20:52

🖊🗞COLUMN | Self-Management in Cota 905: From the Most Dangerous Barrio to an Open Air Art Gallery VA columnist Jessica Dos Santos documents how a grassroots initiative is helping Cota 905 overcome its stigma of armed gangs and police violence. Between 2014 and 2021, Cota 905 in Caracas was under the control of armed gangs, impacting around 300,000 residents and limiting access to services. In 2021, the government launched Operation Gran Cacique Indio Guaicaipuro, deploying 34 checkpoints as part of a large-scale security intervention. Following these events, a community initiative called “The Cota 905 Tour: A Thousand Stories, Over a Hundred Murals, One Route” was developed. The project includes more than 100 murals and was created with participation from residents, artists, and local collectives. It has been officially recognized by Venezuela’s Ministry of Tourism. Read the full VA column here: https://venezuelanalysis.com/columns/self-management-cota-905-from-the-most-dangerous-barrio-to-an-open-air-art-gallery #Barrios#Cota905#TalesofResistance

Ultimora.net - POLITICS

@ultimoraPOLITICS · Post #39553 · 16.05.2022 г., 18:00

#Sondaggi#USA#Governative#Texas Sondaggio di UTT: Greg #Abbott (#R|ECR): 46% (+1) Beto #ORourke (#D|Centro-sinistra): 39% (+1) Delilah #Barrios (#GPUS|Verdi): 3% (+3) Mark Jay #Tippetts (#LP|Libertari): 3% (+3) Qualcun altro: 8% (-8) Non lo so: 2% (+1) Data rilevazione: 2-10 maggio +/-: 8-15 febbraio Intervistati: 1232 @UltimoraPolitics