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

Резултати

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

Пребарај: #uel

当前筛选 #uel清除筛选
Football Daily News

@FootyNews · Post #112806 · 18.05.2023 г., 22:09

Sevilla have won 100% of the #UEL semi-finals they've contested: ◉ 2005/06 v Schalke ◉ 2006/07 v Osasuna ◉ 2013/14 v Valencia ◉ 2014/15 v Fiorentina ◉ 2015/16 v Shaktar ◉ 2019/20 v Man Utd ◉ 2022/23 v Juventus And they've gone on to lift the trophy every single time. 🤯https://t.co/QoHm1XFxnh

Hashtags

Football Daily News

@FootyNews · Post #112798 · 18.05.2023 г., 19:09

RT @Squawka_Live: 19-year-old England U20 international Samuel Iling-Junior starts for Juventus for only the third time. Thrown in at the deep end. 🤿 #UELhttps://t.co/m5XW1Rx6Cihttps://t.co/bBFN7rfQGO

Hashtags

Football Daily News

@FootyNews · Post #112594 · 11.05.2023 г., 20:09

RT @Squawka_Live: Lucas Ocampos first-half by numbers vs Juventus: 100% passing accuracy 100% dribbles completed 6 touches in opp. box (most) 5 shots (most) 5 duels won (Sevilla most) 2 chances created (joint-most) 1 assist 0.73 xG Posting FT stats at HT. 🥶 #UELhttps://t.co/0SXs7yyirm

Hashtags

Football Daily News

@FootyNews · Post #112593 · 11.05.2023 г., 19:10

RT @Squawka_Live: Juventus' record at home in the Europa League since 2009: 🤝 0-0 ✅ 3-1 ✅ 1-0 ✅ 1-0 🤝 3-3 🤝 1-1 🤝 0-0 ✅ 2-0 🤝 1-1 ✅ 2-1 🤝 0-0 🤝 1-1 ✅ 1-0 ✅ 1-0 Mission impossible for Sevilla? #UELhttps://t.co/ulnbRPSOGg

Hashtags

Football Daily News

@FootyNews · Post #111952 · 20.04.2023 г., 22:09

RT @Squawka_Live: Paulo Dybala's game by numbers vs Feyenoord: 47 minutes played 38 touches 6 shots 3 duels won 3 chances created 3 shots on target 1 goal 0.31xG Impact. 💥#UELhttps://t.co/awnOgtSlY4

Hashtags

Football Daily News

@FootyNews · Post #111949 · 20.04.2023 г., 22:09

RT @Squawka_Live: No Premier League player has made more errors leading to a goal across all competitions than David De Gea since the start of last season (5). 😬#UELhttps://t.co/4g3ld0CKW4

Hashtags

Football Daily News

@FootyNews · Post #111945 · 20.04.2023 г., 21:10

RT @Squawka_Live: Man Utd in the first leg vs Sevilla: ◉ Score two own goals Man Utd in the second leg vs. Sevilla: ◉ Commit two errors leading to goals Shooting themselves in the foot. 🔫#UELhttps://t.co/Bv4VnTGCbl

Hashtags

Football Daily News

@FootyNews · Post #111943 · 20.04.2023 г., 20:09

RT @Squawka_Live: Erik Lamela created more chances than any other player in the first half vs. Man Utd (3). He also provided the assist for Youssef En-Nesyri, his first for Sevilla this season. 🅰️#UELhttps://t.co/baxztemlkr

Hashtags

Football Daily News

@FootyNews · Post #111941 · 20.04.2023 г., 18:09

RT @Squawka_Live: Sevilla XI: Bono; Navas, Bade, Marcao, Acuna; Gudelj, Fernando; Ocampos, Rakitic, Lamela; En-Nesyri. Man Utd XI: De Gea; Wan-Bissaka, Maguire, Lindelof, Dalot; Eriksen, Casemiro, Sabitzer; Antony, Martial, Sancho. #UEL

Hashtags

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