Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

validate

#pip install danielutils
from danielutils import validate

@validate(str)
def f1(name):
    pass

@validate([int, float])
def f2(number):
    pass

@validate([str, lambda s: len(s) < 10, "short_string must be shorter than 10 letters"])
def f3(short_string):
    pass

#will raise error because duplicate function name in same context
@validate([[int, float], lambda age: age > 0, "age must be bigger then 0"])
def f3(age):
    pass

@validate(None, int, int, None)
def f5(skip, dont_skip, dont_skip2, skip2):
    pass
Comment

validate

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 :: store message sent by user in string discord py 
Python :: list len python 
Python :: set password django 
Python :: program to count the number of occurrences of a elementes in a list python 
Python :: prime numbers 1 to 100 
Python :: python check if character in string 
Python :: add new column to pandas dataframe 
Python :: sign python 
Python :: delete function python 
Python :: if it is square python 
Python :: python key 
Python :: fraction in python 
Python :: Fill in the empty function so that it returns the sum of all the divisors of a number, without including it. A divisor is a number that divides into another without a remainder. 
Python :: rename data columns pandas 
Python :: pip for python 
Python :: self.variable 
Python :: python call function by string 
Python :: python string format_map 
Python :: django filter on related field 
Python :: list append python 3 
Python :: ImportError: cannot import name 
Python :: NEW CALENDAR MODULE 
Python :: what does manage.py do 
Python :: the requested resource was not found on this server. django 
Python :: == in python 
Python :: reverse linked list python 
Python :: print("Hello world!") 
Python :: "scrapy shell" pass cookies to fetch 
Python :: python for loop inside list 
Python :: python << meaning 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =