@sgmrt · Post #2349 · 11.05.2025 г., 09:49
[NSL] CLEARED: Train service between #Yishun and #Sembawang have resumed. Free regular bus service has ended. - SMRT
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
Пребарај: #sembawang
@sgmrt · Post #2349 · 11.05.2025 г., 09:49
[NSL] CLEARED: Train service between #Yishun and #Sembawang have resumed. Free regular bus service has ended. - SMRT
Hashtags
@sgmrt · Post #2346 · 11.05.2025 г., 09:04
[NSL] UPDATE: Due to a track point fault, please expect an additional 10 mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. If you have any enquiries, please kindly - SMRT
Hashtags
@sgmrt · Post #2339 · 11.05.2025 г., 07:14
[NSL] UPDATE: Due to a track point fault, please expect an additional 10mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Our staff is attending to it now. We apologise - SMRT
Hashtags
@sgmrt · Post #2338 · 11.05.2025 г., 06:59
[NSL] UPDATE: Due to a track point fault, please expect an additional 10mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. We apologise for the delay to your journey. - SMRT
Hashtags
@sgmrt · Post #2337 · 11.05.2025 г., 06:44
[NSL] UPDATE: Due to a track point fault, please expect an additional 10mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. - SMRT
Hashtags
@sgmrt · Post #2336 · 11.05.2025 г., 06:29
[NSL]: Due to a track point fault, please expect an additional 10mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. - SMRT
Hashtags
@sgmrt · Post #2347 · 11.05.2025 г., 09:19
[NSL] UPDATE: Please expect an additional 10 mins train travel time between #Yishun and #Sembawang due to a track point fault. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Passengers travelling towards #Orchard and - SMRT
@sgmrt · Post #2340 · 11.05.2025 г., 07:24
[NSL] UPDATE: Due to a track point fault, please expect an additional 10mins train travel time between #Yishun and #Sembawang. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Passengers travelling towards #Orchard and - SMRT
@sgmrt · Post #2345 · 11.05.2025 г., 08:44
[NSL] UPDATE: Please expect an additional 10mins train travel time between #Yishun and #Sembawang due to a track point fault. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Passengers travelling towards #Orchard and #Marina - SMRT
@sgmrt · Post #2343 · 11.05.2025 г., 08:09
[NSL] UPDATE: Please expect an additional 10mins train travel time between #Yishun and #Sembawang due to a track point fault. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Passengers travelling towards #Orchard and #Marina - SMRT
@sgmrt · Post #2341 · 11.05.2025 г., 07:39
[NSL] UPDATE: Please expect an additional 10mins train travel time between #Yishun and #Sembawang due to a track point fault. Free regular bus service is available between #Yishun and #Woodlands. Train service is still available. Passengers travelling towards #Orchard and #Marina - SMRT