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 слични објави

Пребарај: #twilio

当前筛选 #twilio清除筛选
403 Forbidden

@forbid_403 · Post #360 · 06.07.2024 г., 03:15

Twilio 在骇客放出数据后确认 Authy 用户手机号码遭到泄露。受影响用户数约 3300 万。 Authy 是 Twilio 的 2FA 软件,需要手机号码注册。Twilio 的部分服务只支持此 App 作为 2FA 方式。 securityweek.com/~ #Twilio#Privacy

AI & Law

@ai_and_law · Post #96 · 28.08.2023 г., 07:04

Twilio Introduces "Nutrition Labels" to Boost Trust in AI Hello AI&Law society! Twilio, a communication tool services company, is taking a proactive step to enhance transparency and trust in its artificial intelligence services. The company is introducing "nutrition labels" for its AI models, providing information about the data used for training, the purpose of data collection, and more. The labels aim to offer users a clear understanding of how their data will be utilized. The "nutrition labels" highlight key aspects such as which AI models are being employed, whether customer data is used for training, and whether human involvement is integrated. Additionally, Twilio offers an online tool that enables other companies to generate similar labels for their AI products. Twilio's initiative aligns with broader industry efforts to ensure responsible and transparent AI practices. Salesforce, for instance, is also unveiling an acceptable use policy for its generative AI technologies, specifying prohibited practices and emphasizing responsible AI usage. #AI#Transparency#DataPrivacy#Trust#ResponsibleAI#Twilio#Salesforce