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

Резултати

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

Пребарај: #zil

当前筛选 #zil清除筛选
Pro Analysis

@proanalysistrader · Post #27924 · 11.09.2024 г., 01:05

#ZIL/USDT analysis : #ZIL is in a downtrend, trading below the 200 EMA. The price is currently experiencing rejection in the resistance zone. It is anticipated to reverse from this point and persist in its bearish structure to test previous lows. TF : 2H Entry : $0.01336 Target : $0.01244 SL : $0.01373

Hashtags

Pro Analysis

@proanalysistrader · Post #27901 · 03.09.2024 г., 13:36

#ZIL/USDT analysis : #ZIL is in a downtrend, trading below the 200 EMA. The price is currently facing rejection in the resistance zone. It is expected to reverse from there and continue its bearish structure to test previous lows. TF : 2H Entry : $0.01356 Target : $0.01272 SL : $0.01413

Hashtags

American Crypto©

@americancryptotrading · Post #27169 · 27.02.2025 г., 20:00

🇺🇸#ZIL/USDT is facing the lowerboundary of the descendingtriangle formation on the weekly chart🔍 Bounce soon🐃 American Crypto©

Hashtags

American Crypto©

@americancryptotrading · Post #27075 · 06.12.2024 г., 17:17

🇺🇸#ZIL/USDT is breaking through the descendingresistance on the weekly chart🧐 Bullish case✈️ American Crypto©

Hashtags

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