@sgmrt · Post #1703 · 18.04.2023 г., 08:37
[CCL] Train service is resuming progressively. Free regular buses are still available btw #BuonaVista and #HarbourFront. - 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
Пребарај: #harbourfront
@sgmrt · Post #1703 · 18.04.2023 г., 08:37
[CCL] Train service is resuming progressively. Free regular buses are still available btw #BuonaVista and #HarbourFront. - SMRT
Hashtags
@sgmrt · Post #2287 · 14.03.2025 г., 11:34
[CCL] CLEARED: Fault cleared trains are running normally between #HarbourFront, #MarinaBay and #DhobyGhaut. - SMRT
@sgmrt · Post #2286 · 14.03.2025 г., 11:34
[CCL] UPDATE: Fault cleared trains are progressively returning to normal between #HarbourFront, #MarinaBay and #DhobyGhaut. Train service is still available. - SMRT
@sgmrt · Post #2285 · 14.03.2025 г., 11:14
[CCL] UPDATE: Fault cleared trains are progressively returning to normal between #HarbourFront, #MarinaBay and #DhobyGhaut. - SMRT
@sgmrt · Post #2284 · 14.03.2025 г., 11:05
[CCL]: Additional 15 mins train travel time between #HarbourFront, #MarinaBay and #DhobyGhaut due to a signal system fault. - SMRT
@sgmrt · Post #1912 · 28.04.2024 г., 22:34
[CCL] UPDATE: Additional 30mins train travel time between #Bishan and #one-north due to a train fault. Free Regular bus service is available between #Bishan and #HarbourFront. You may also use alternative lines to travel to your destination. - SMRT
Hashtags
@sgmrt · Post #1911 · 28.04.2024 г., 22:34
[CCL] UPDATE: Additional 30 mins train travel time between #Bishan and #one-north due to a train fault. Free Regular bus service is available between #Bishan and #HarbourFront. We apologise for this delay to your journey. - SMRT
Hashtags
@sgmrt · Post #1702 · 18.04.2023 г., 08:23
[CCL] Due to a power fault, please add 20 minutes additional train travel time btw #KentRidge and #Harbourfront. Free regular buses are available btw #BuonaVista and #Harbourfront. - SMRT
@sgmrt · Post #1913 · 28.04.2024 г., 22:39
[CCL] UPDATE: Additional 30 mins train travel time between #PayaLebar and #one-north due to a train fault. Free Regular Bus Service is available between #PayaLebar and #HarbourFront. Bridging bus service is available between #PayaLebar and #BuonaVista. - SMRT
@sgmrt · Post #2183 · 15.12.2024 г., 16:44
[CCL] UPDATE: Due to signaling fault, please expect additional 30 mins travelling time btwn #Farrer Road and #HarbourFront. Free regular bus services are available between #Botanic Gardens and #HarbourFront. - SMRT
Hashtags
@sgmrt · Post #1910 · 28.04.2024 г., 22:19
[CCL] UPDATE: Additional 30mins train travel time between #HollandVillage and #PasirPanjang due to a train fault. Free Regular Bus Service is available between #Bishan and #HarbourFront. We apologise for this delay to your journey. - SMRT