Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python schema

from schema import Schema, And, Use, Optional, SchemaError

'''
pip install schema required
name required must be a string and strip leading and trailing whitespaces
age required must be a int and greater than or equal to 18 and less than or equal to 99
gender is optional and must be string and with the choice of male or female converting the gender to lowercase
'''
schema = Schema([{'name': And(str, str.strip),
                  'age':  And(Use(int), lambda n: 18 <= n <= 99),
                  Optional('gender'): And(str, Use(str.lower), lambda s: s in ('male', 'female'))}])

data = [{'name': 'Codecaine', 'age': '21', 'gender': 'Female'},
        {'name': 'Sam', 'age': '42'},
        {'name': 'Sacha', 'age': '20', 'gender': 'male'}]

try:
    validated = schema.validate(data)
    print(validated)
except SchemaError as err:
    print(err)
Comment

PREVIOUS NEXT
Code Example
Python :: django add user to group 
Python :: how to download chatterbot 
Python :: sum of the number in a list in python 
Python :: how to create multiple variables in a loop python 
Python :: python wsgi 
Python :: python len 
Python :: sklearn.metrics accuracy_score 
Python :: installing python 3 to linux 
Python :: how to find the average in python 
Python :: python buffer 
Python :: flatten lists python 
Python :: hash password python 
Python :: python input - how to read a number 
Python :: migration django 
Python :: generator comprehension python 
Python :: reference variable python 
Python :: float in python 
Python :: pytest-mock tutorial 
Python :: python beautifulsoup get attibute 
Python :: partition python 
Python :: python input().strip() 
Python :: javascript or python 
Python :: what is manage.py 
Python :: python os check if file with extension exists 
Python :: sort array numpy 
Python :: python 3.4 release date 
Python :: python infinite loop 
Python :: select first row of every group pandas 
Python :: discord.py 8ball 
Python :: how to create file organizer using python 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =