@PixelExperience · Post #9428 · 06.03.2024 г., 11:24
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 14 📆 Build date: March 5, 2024 16:58 📂 File size: 1.31 GB ⬇️ Download now #lake#pixelexperience
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
Пребарај: #lake
@PixelExperience · Post #9428 · 06.03.2024 г., 11:24
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 14 📆 Build date: March 5, 2024 16:58 📂 File size: 1.31 GB ⬇️ Download now #lake#pixelexperience
Hashtags
@PixelExperience · Post #9399 · 29.01.2024 г., 11:04
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 📆 Build date: January 27, 2024 15:44 📂 File size: 1.16 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9398 · 29.01.2024 г., 11:04
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: January 27, 2024 16:41 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9275 · 28.11.2023 г., 19:19
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: November 28, 2023 16:40 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9274 · 28.11.2023 г., 19:19
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 📆 Build date: November 28, 2023 15:48 📂 File size: 1.16 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9112 · 28.09.2023 г., 11:40
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 📆 Build date: September 27, 2023 22:34 📂 File size: 1.16 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9111 · 28.09.2023 г., 11:40
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: September 27, 2023 23:17 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9027 · 16.09.2023 г., 21:52
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 📆 Build date: September 16, 2023 16:58 📂 File size: 1.16 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #9026 · 16.09.2023 г., 21:52
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: September 16, 2023 17:48 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #8953 · 19.08.2023 г., 20:54
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 📆 Build date: August 18, 2023 20:41 📂 File size: 1.16 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #8952 · 19.08.2023 г., 20:54
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: August 18, 2023 22:17 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags
@PixelExperience · Post #8861 · 14.08.2023 г., 18:18
➡️New build available for Motorola Moto G7 Plus (lake) 👤 by jro1979oliver ℹ️ Version: 13 (Plus edition) 📆 Build date: August 14, 2023 13:04 📂 File size: 1.17 GB ⬇️ Download now 💬 View discussion #lake#pixelexperience
Hashtags