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

Резултати

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

Пребарај: #gutenberg

当前筛选 #gutenberg清除筛选
GitHub Trends

@githubtrending · Post #14782 · 03.06.2025 г., 13:00

#javascript#gutenberg#wordpress#wordpress_plugin Gutenberg is a new way to edit and build websites in WordPress. It uses blocks for each piece of content, like paragraphs or images, which can be easily moved around. This makes creating pages visually intuitive and flexible. Gutenberg also offers features like real-time collaboration and a visual interface, making it easier to design unique pages. It's still evolving but offers many benefits, such as responsive design and built-in accessibility features, making your website look good on all devices and accessible to everyone. https://github.com/WordPress/gutenberg

耕读频道

@iGengdu · Post #126 · 11.05.2024 г., 09:48

#英文#外文#电子书#搜索#网站 学术网站监测: https://open-slum.org/ ; https://www.ooopn.com/tool/scihub/ #Anna Archive: 网址1 https://annas-archive.org/ ; 网址2 https://annas-archive.li/ ; 网址3 https://annas-archive.se/ 。 #Library Genesis: https://libgen.bz/ https://libgen.gs/ https://libgen.li/ https://libgen.la/ https://libgen.vg/ https://libgen.rs/ https://libgen.is/ https://libgen.st/ http://gen.lib.rus.ec/ https://libgen.rocks/ https://libgen.pm/ 检测LG有效性网址 。 #Zlibrary: Books: https://z-library.rs , https://z-library.do , z-library.sk 1lib.sk z-lib.gs z-lib.fo (For Spain, Italian, France) z-lib.gl z-lib.fm z-lib.nz z-lib.gd z-lib.help Articles: https://articles.sk , https://z-lib.gs , https://1lib.sk Z-Access: https://go-to-library.sk , https://litera-library.com/search-for-books 权威发布: Zlibrary的X账号 ; https://wikipedia.org/wiki/Z-Library 官网地址公布 (Update 20240701)。 #Sci-Hub 网址1:https://sci-hub.se/ 网址2:https://sci-hub.st/ 网址3:https://sci-hub.ru/ 网址4:https://sci-hub.red/ 网址5:https://sci-hub.box/ 网址6:https://www.sci-hub.in/ 网址7:https://www.sci-hub.cat/ #InternetArchive: https://archive.org/ #Gutenberg: http://www.gutenberg.org/ 说明:个人手动收集,部分参考这里,转发请带源: https://t.me/iGengdu/126 。 📢频道✈️ 群聊 (耕读) 📬投稿