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

Резултати

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

Пребарај: #microwave

当前筛选 #microwave清除筛选
Universe Mysteries 🪐

@cosmomyst · Post #612 · 09.01.2026 г., 22:21

🪐 In 2022, scientists used data from the Atacama Cosmology Telescope in Chile to make the sharpest-ever map of the cosmic microwave background’s subtle temperature patterns. This ancient light, which blankets galaxies like our Milky Way, holds whisper-faint traces left by the first matter clumps—revealing how gravity, even in the universe’s earliest moments, started pulling atoms together to form all the cosmic structures we see today. ✨ #microwave⚡#background⚡#universe⚡#gravity⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #207 · 01.09.2025 г., 20:11

🪐 The cosmic microwave background (CMB) is a faint glow that fills the universe, left over from when the cosmos was just 380,000 years old. Tiny temperature differences in the CMB, first mapped in detail by the Planck satellite, reveal subtle "hot" and "cold" spots—clues to how galaxies, like the Milky Way and Andromeda, began to form from small ripples in the early universe. ✨ #microwave⚡#background⚡#universe⚡#Planck⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#cosmos⚡#space 👉subscribe Universe Mysteries ​

Universe Mysteries 🪐

@cosmomyst · Post #589 · 29.12.2025 г., 22:21

🪐 The cosmic microwave background contains faint "Sunyaev-Zel’dovich effects," which happen when the ancient microwave light passes through hot gas in galaxy clusters like the Bullet Cluster. This process boosts the energy of some photons (light particles), leaving tiny shadows in the background glow—allowing astronomers to map both distant galaxy clusters and the hot plasma between galaxies by studying these subtle marks in the oldest light in the universe. ✨ #microwave⚡#background⚡#galaxyclusters⚡#plasma⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​