@LiveHealthy · Post #6940 · 24.01.2025 г., 00:52
#happiness
Hashtags
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
Пребарај: #happiness
@LiveHealthy · Post #6940 · 24.01.2025 г., 00:52
#happiness
Hashtags
@QuietWorld · Post #6796 · 26.10.2019 г., 08:30
The first recipe to happiness is to avoid lengthy meditation of the past. 🍃 #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6787 · 25.10.2019 г., 08:30
Happiness will never come to those who never appreciate what they already have. #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6756 · 20.10.2019 г., 23:30
Recall it as often as you wish, a happy memory never wears out. —Libbie Fudim #happiness@quietworld🍃
Hashtags
@QuietWorld · Post #6738 · 18.10.2019 г., 23:30
The word 'happiness' would lose its meaning if it were not balanced by sadness. – Carl Jung #happiness@quietworld🍃
Hashtags
@talks · Post #286 · 26.05.2018 г., 11:46
#happiness
Hashtags
@QuietWorld · Post #6772 · 23.10.2019 г., 08:30
Thousands of candles can be lighted from a single candle & the life of the candle will not be shortened. Happiness never decreased by being shared. – Buddha – but of course this doesn't apply to all kinds of happiness though, for example; I would surely be less happy if I were to share a husband with someone🧐 #happiness#thoughts @quietworld🍃
Hashtags
@QuietWorld · Post #6617 · 28.09.2019 г., 03:58
Happiness is not the absence of problems, but the ability to deal with them. #happiness#quotes @quietworld🍃
Hashtags
@QuietWorld · Post #6797 · 26.10.2019 г., 13:00
Happiness doesn't come from doing easy work but from the afterglow of satisfaction that comes after the achievement of a difficult task that demanded our best. #happiness#motivation @quietworld🍃
Hashtags
@QuietWorld · Post #7474 · 08.07.2020 г., 04:02
My ex-schoolmates, ex-collegemates, ex-everything on social media: • getting married • celebrating their first pregnancy • flexing new expensive car • flexing their lover • flexing their expensive pets 😍 Me: • happily flexing the stray kittens I adopted 🥰 The amount of happiness, is same 🥰 Just make sure you know what actually makes you happy 😄💐 What makes you happy? Comment down below.😉 #thoughts#life#happiness @quietworld🍃
Hashtags
@gumiho_wallpaper · Post #866 · 21.10.2024 г., 00:10
#Kdrama #Happiness
Hashtags
@QuietWorld · Post #6739 · 19.10.2019 г., 06:30
"Everyone wants happiness, nobody wants pains, but you can't have the rainbow, without a little rain." #happiness#selfreminder @quietworld🍃
Hashtags