def test_date(): schema = Schema({"date": Date()}) schema({"date": "2016-10-24"}) assert_raises(MultipleInvalid, schema, {"date": "2016-10-2"}) assert_raises(MultipleInvalid, schema, {"date": "2016-10-24Z"})
def test_date_custom_format(): schema = Schema({"date": Date("%Y%m%d")}) schema({"date": "20161024"}) assert_raises(MultipleInvalid, schema, {"date": "2016-10-24"})
company_mapping = { -1: None, 0: 'Maverick Cardio-Telemetry', 1: 'Maverick Water-Telemetry' } providers = ['Amazon', 'NewEgg'] device_types = ['cardio', 'medical', 'telemonitoring'] carriers = ['Mtel', 'Vivacom', 'Telenor'] transfer_schema = Schema({ 'couple': All(int, Number(precision=10)), 'company': All(int, In(company_mapping.keys())) }, required=True) enter_device_schema = Schema({ 'id': All(int, Number(precision=10)), 'delivery_date': All(str, Date()), 'provider': All(str, In(providers)), 'type': All(str, In(device_types)), 'model': str, 'serial': All(int, Number(precision=14)) }, required=True) enter_sim_schema = Schema({ 'delivery_date': All(str, Date()), 'carrier': All(str, In(carriers)), 'number': All(str, Match(r'/08[789]\d{7}/')) # correct regex? }, required=True)
class PyESGConfiguration(JSONSerialisableClass): """ Represents the full pyESG configuration. Attributes: number_of_simulations (int): The number of simulations to produce. number_of_projection_steps (int): The number of time steps to project in the simulations. projection_frequency (str): The frequency of projections. (e.g. 'annually', 'monthly', 'weekly') number_of_batches (int): The number of batches into which the simulations are split during generation. random_seed (int): The random seed to use when generating random samples. economies (list[Economy]): A list of the economies being modelled. correlations (Correlations): The correlations between the random drivers for the asset class models. """ _serialisable_lists = { 'economies': Economy, } _serialisable_attrs = { 'correlations': Correlations, } _validation_schema = Schema({ Required('number_of_simulations'): All(int, Range(min=1)), Required('number_of_projection_steps'): All(int, Range(min=1)), Required('output_file_directory'): IsDir(), Required('output_file_name'): str, Required('projection_frequency'): In(PROJECTION_FREQUENCIES), Required('number_of_batches'): All(int, Range(min=1)), Required('random_seed'): int, Required('start_date'): Date(), Required('economies'): [Economy._validation_schema], Required('correlations'): Correlations._validation_schema, }) def __init__(self, **kwargs): self.number_of_simulations = None # type: int self.number_of_projection_steps = None # type: int self.output_file_directory = None # type: str self.output_file_name = None # type: str self.projection_frequency = None # type: str self.number_of_batches = None # type: int self.random_seed = None # type: int self.start_date = None # type: str self.economies = [] # type: List[Economy] self.correlations = Correlations() # type: Correlations super().__init__(**kwargs) @classmethod def load_from_file(cls, file_path: str) -> 'PyESGConfiguration': """ Loads settings from a pyESG configuration file. Args: file_path: The file path of the pyESG configuration file. Returns: A PyESGConfiguration object containing the settings from the specified configuration file. """ with open(file_path) as config_file: config = json.load(config_file) pyesg_config = cls._decode_json(config) # type: 'PyESGConfiguration' return pyesg_config def save_to_file(self, file_path: str): """ Saves the configuration settings specified to a file. Args: file_path: The file path to which to save the configuration file. """ with open(file_path, 'w') as engine_settings_file: json.dump(self._encode_json(), engine_settings_file, indent=4) def validate(self): """ Validates the configuration. """ json_obj = self._encode_json() self._validation_schema(json_obj)
from voluptuous import Schema, Required, In, All, Optional, Length, Email, Url, Date login_schema = Schema({ Required('phone'): All(str, ), Required('password'): str, }) register_schema = Schema({ Required('phone'): All(str, ), Required('sms_code'): All(str, ), Required('password1'): str, Required('password2'): str, }) sms_schema = Schema({ Required('phone'): All(str, ), }) user_info_schema = Schema({ Optional('id'): int, Optional('avatar'): Url(), Optional('name'): str, Optional('phone'): str, Optional('birthday'): Date(), Optional('gender'): str, Optional('email'): Email(), })