@QuietWorld · Post #6230 · 12.03.2019 г., 07:12
Don’t look back and wonder why things went wrong. Don’t regret not doing more. It happened for a reason. Your better days are ahead of you. #motivation#assurance @quietworld🍃
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
Пребарај: #assurance
@QuietWorld · Post #6230 · 12.03.2019 г., 07:12
Don’t look back and wonder why things went wrong. Don’t regret not doing more. It happened for a reason. Your better days are ahead of you. #motivation#assurance @quietworld🍃
Hashtags
@QuietWorld · Post #6459 · 15.05.2019 г., 04:49
#assurance#selfreminder @quietworld🍃
Hashtags
@QuietWorld · Post #6864 · 05.11.2019 г., 10:23
Two years being passive is just a blink of an eye 😉 Two years moving towards a big goal every single day is plenty of time 😮 There is nothing that cannot be accomplished in two years with enough effort 😎 #life#motivation#assurance @quietworld🍃
Hashtags
@QuietWorld · Post #6231 · 12.03.2019 г., 11:15
“When it comes to breaking old habits and starting new ones, remember to be patient with yourself. You’ve got to expect it’s going to take time and effort before you see lasting results.” #quotes#assurance#motivation @quietworld🍃
Hashtags
@QuietWorld · Post #7195 · 16.02.2020 г., 10:05
#assurance#selfcare#selfreminder @quietworld🍃
Hashtags
@QuietWorld · Post #7359 · 08.06.2020 г., 03:08
to anyone who might need this at this moment🌹 #reminder#selfcare#assurance @quieworld🍃
Hashtags
@QuietWorld · Post #7223 · 21.03.2020 г., 04:07
It is not a sin to be upset; with someone, with a situation, with yourself. Give yourself time to absorb what you're feeling until you're ready to move to where there's growth. #selfreminder#selfcare#assurance#wisdom @quietworld🍃
@QuietWorld · Post #6856 · 04.11.2019 г., 13:33
Appreciate where you are in your journey, even if it's not where you want to be - because every season serves a purpose. #life#quotes#assurance#gratitude#happiness @quietworld🍃
@QuietWorld · Post #7338 · 04.06.2020 г., 15:56
Things to Remember🍃 #thingstoremember#quotes#selfreminder#selfcare#life#assurance @quietworld🍃
@QuietWorld · Post #7341 · 05.06.2020 г., 15:02
#thingstoremember#life#selfcare#selfreminder#assurance#solitude @quietworld🍃