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

Резултати

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

Пребарај: #xbladeskingdom

当前筛选 #xbladeskingdom清除筛选
PT资讯频道

@PrivateTrackerNews · Post #558 · 01.06.2019 г., 05:18

2019/06/01 #开放注册 #OpenSignupTracker #国外PT 整理自电报@PT资讯频道 @privatetrackernews 设计素材类 Tracker's Name: #CGPeers Genre: Other Sign-up Link: https://www.cgpeers.to/register.php Closing date: Open for about 48 hours. Additional information: CGPeers is a Private Torrent Tracker for 3D/Visual Effects / Computer Graphics/Design / Computer Assisted Art material. 电影电视综合站 Tracker's Name: #Twilights Genre: General Sign-up Link: https://twilightsdreams.com/signup.php Closing date: Open for a week. Additional information: Twilights is a Private Torrent Tracker for Movies / TV / General Releases. 音乐站 Tracker's Name: #MusicBox Genre:Music Sign-up Link: https://musicbox.si/?p=signup&pid=16 Closing date: Not In The Near Future Additional information: This is an english torrent site for MP3 music from all genres. 电视电影音乐综合站 Tracker's Name: #CrazyBitchTV Genre:General Sign-up Link: http://cbtv.info/signup.php? Closing date: Not In The Near Future Additional information:CBTV is a brand new general tracker offering the greatest and letest in tv movies and music. They have a great staff and a dj team with an active shout and forums. You maybe surprised to know like i was that the site belongs to Mazy. Do come and have a look for yourselves. Those who signup and come into shout and say hail mazy get 5GB upload and 500BP 印度电影电视综合站 Tracker's Name: #BwTorrents Genre:General Sign-up Link: https://bwtorrents.tv/signup.php Closing date: N/A Additional information: BwTorrents is an Indian Private Torrent Tracker for Movies / TV / General Releases. 综合站 Tracker's Name: #xbladeskingdom Genre:General Sign-up Link: https://xblades-kingdom.net/signup.php Closing date: no closeing date Additional information:We are back on line seedbox torrents we are looking for uploaders and staff mods #开放申请注册 #OpenApplicationSignupTracker 漫画 Tracker's Name: #32Pages (Open for Application Signup) Genre:Other Sign-up Link: https://32pag.es/application.php Closing date: Open for a few days Additional information:32Pages (ComicBT) is a Private Torrent Tracker for Comic Books. 苹果电脑软件 Tracker's Name: #MacTorrents (Open for Application Signup) Genre: Other Sign-up Link: https://mac-torrents.me/register.php Additional information: Mac Torrents is a Private Torrent Tracker for Mac Software Releases. 德国综合站 Tracker's Name: #BiTTiTAN (Open for Application Signup) Genre:General Sign-up Link: https://bit-titan.net/invite_ask.php Closing date: N/A Additional information:BiT-TiTAN is a German Private Torrent Tracker for General Content