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

Пребарај: #secureai

当前筛选 #secureai清除筛选
AI & Law

@ai_and_law · Post #299 · 03.05.2024 г., 07:04

USA: New Bill Targets AI Security with Breach Tracking and Counter-AI Measures Senators Warner and Tillis have introduced the Secure Artificial Intelligence Act, aiming to bolster security measures surrounding AI systems. The bill proposes an AI Security Center within the National Security Agency (NSA) to manage a database recording all AI security breaches, including close calls ("near-misses"). The legislation establishes a dedicated unit within the NSA to lead research on "counter-AI" techniques. This includes methods to identify and prevent manipulation of AI systems, such as: ✅Data Poisoning: Malicious insertion of code into training data to skew an AI model's output. ✅Evasion Attacks: Altering data used by AI models to intentionally confuse them. ✅Privacy-Based Attacks: Exploiting vulnerabilities in AI systems to compromise user privacy. ✅Abuse Attacks: Misusing AI models for malicious purposes. The Secure Artificial Intelligence Act will undergo committee review before potential consideration by the full Senate. #SecureAI

Hashtags

AI & Law

@ai_and_law · Post #35 · 21.06.2023 г., 07:04

£54 million boost to develop secure and trustworthy AI research The UK government has announced a significant investment of £54 million to support the development of secure and trustworthy AI. The funding will be allocated to various projects and initiatives focused on enhancing the security and trustworthiness of AI systems. This includes advancing research on AI algorithms, data privacy, and cybersecurity measures. The aim is to address critical challenges such as algorithmic bias, data protection, and ethical considerations in AI development and deployment. #AIresearch#SecureAI#TrustworthyAI#UKgovernment#Innovation#EthicalAI#DataPrivacy#Cybersecurity