Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
есть кусок валидации: [code] покажи, как бы это выглядело, если бы использовалась…
- User
есть кусок валидации:
codeif 'daemon' in self.\_cfg: self.\_is\_positive('daemon.epoch\_duration', True) self.\_is\_bool('daemon.backup', True) self.\_is\_positive('daemon.max\_failed\_rounds', True) self.\_is\_positive('daemon.account\_balance\_threshold', True) self.\_is\_bool('daemon.log\_quotes', True) self.\_has\_props(('daemon.feed\_provider', )) self.\_has\_provider(self['daemon']['feed\_provider']) self.\_check\_exports('daemon', item\_len\_options=(2, 3)) self.\_check\_endpoint('daemon.health\_endpoint')покажи, как бы это выглядело, если бы использовалась библиотека marshmellow?
- Assistant
Использование библиотеки marshmallow для валидации конфигурации могло бы выглядеть следующим образом:
codefrom marshmallow import Schema, fields class DaemonConfig(Schema): epoch_duration = fields.Int(required=True) backup = fields.Bool(required=True) max_failed_rounds = fields.Int(required=True) account_balance_threshold = fields.Int(required=True) log_quotes = fields.Bool(required=True) feed_provider = fields.Str(required=True) exports = fields.List(fields.Str()) health_endpoint = fields.URL(required=True) class Config(Schema): daemon = fields.Nested(DaemonConfig) cfg = { 'daemon': { 'epoch_duration': 5, 'backup': True, 'max_failed_rounds': 3, 'account_balance_threshold': 100, 'log_quotes': False, 'feed_provider': 'my_provider', 'exports': ['export1', 'export2', 'export3'], 'health_endpoint': 'http://localhost:8000/health', } } schema = Config() result = schema.load(cfg) if result.errors: print(result.errors)