@profile_pictures_tm · Post #18415 · 17.10.2024 г., 13:47
#Animation🍿🕶
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
Пребарај: #animation
@profile_pictures_tm · Post #18415 · 17.10.2024 г., 13:47
#Animation🍿🕶
Hashtags
@profile_pictures_tm · Post #13981 · 10.04.2023 г., 17:02
#Animation
Hashtags
@profile_pictures_tm · Post #13842 · 29.03.2023 г., 11:53
#Animation
Hashtags
@BuffFurries · Post #12497 · 12.04.2026 г., 19:18
#animation@YiffArtHub https://x.com/YoseBose/status/2043342267788997078
Hashtags
@BuffFurries · Post #11887 · 15.01.2026 г., 14:20
#animation@YiffArtHub
Hashtags
@BuffFurries · Post #11870 · 15.01.2026 г., 02:43
#animation@YiffArtHub
Hashtags
@BuffFurries · Post #11605 · 09.12.2025 г., 12:46
#animation@YiffArtHub
Hashtags
@profile_pictures_tm · Post #11537 · 16.10.2022 г., 16:01
#Animation
Hashtags
@figma60sec · Post #10723 · 23.03.2026 г., 17:03
SVG Motion — плагин анимирует векторные линии с помощью свойства «Обводка». Figma Community • #animation
Hashtags
@PawStash · Post #3570 · 02.03.2026 г., 22:23
Paw aftercare Mystery box YCH for Roukan #Animation
Hashtags
@viral_today · Post #3326 · 23.06.2025 г., 12:31
Cute animation of a little Lego kitty playing with a yarn ball @Viral_Today / #animation
Hashtags
@viral_today · Post #2278 · 17.04.2024 г., 14:30
Bread is bad for ducks and other waterfowl. Their natural diet consists of pond weed, seeds, insects, worms, small water snails, and even crustaceans, such as crayfish. White bread barely offers any nutrition. It simply acts as a filler. @Viral_Today / #animation
Hashtags