<s> import re <EOL> from pprint import pformat <EOL> def e ( string , * args ) : <EOL> """<STR_LIT>""" <EOL> return string . format ( * [ pformat ( arg ) for arg in args ] ) <EOL> def one_of ( * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> and isinstance ( args [ <NUM_LIT:0> ] , list ) : <EOL> items = args [ <NUM_LIT:0> ] <EOL> else : <EOL> items = list ( args ) <EOL> def validate ( value ) : <EOL> if not value in items : <EOL> return e ( "<STR_LIT>" , value , items ) <EOL> return validate <EOL> def gte ( min_value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if value < min_value : <EOL> return e ( "<STR_LIT>" , value , min_value ) <EOL> return validate <EOL> def lte ( max_value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if value > max_value : <EOL> return e ( "<STR_LIT>" , value , max_value ) <EOL> return validate <EOL> def gt ( gt_value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if value <= gt_value : <EOL> return e ( "<STR_LIT>" , value , gt_value ) <EOL> return validate <EOL> def lt ( lt_value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if value >= lt_value : <EOL> return e ( "<STR_LIT>" , value , lt_value ) <EOL> return validate <EOL> def between ( min_value , max_value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if value < min_value : <EOL> return e ( "<STR_LIT>" , <EOL> value , min_value ) <EOL> if value > max_value : <EOL> return e ( "<STR_LIT>" , <EOL> value , max_value ) <EOL> return validate <EOL> def length ( min = None , max = None ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> if min and len ( value ) < min : <EOL> return e ( "<STR_LIT>" , value , min ) <EOL> if max and len ( value ) > max : <EOL> return e ( "<STR_LIT>" , value , max ) <EOL> return validate <EOL> def match ( pattern ) : <EOL> """<STR_LIT>""" <EOL> regex = re . compile ( pattern ) <EOL> def validate ( value ) : <EOL> if not regex . match ( value ) : <EOL> return e ( "<STR_LIT>" , value , pattern ) <EOL> return validate <EOL> def is_email ( ) : <EOL> """<STR_LIT>""" <EOL> email = ( <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT>' <EOL> ur '<STR_LIT:@>' <EOL> ur '<STR_LIT>' <EOL> ) <EOL> regex = re . compile ( email , re . IGNORECASE | re . UNICODE ) <EOL> def validate ( value ) : <EOL> if not regex . match ( value ) : <EOL> return e ( "<STR_LIT>" , value ) <EOL> return validate <EOL> def is_url ( ) : <EOL> """<STR_LIT>""" <EOL> regex = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> def validate ( value ) : <EOL> if not regex . match ( value ) : <EOL> return e ( "<STR_LIT>" , value ) <EOL> return validate <EOL> def each_item ( * validators ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> for item in value : <EOL> for validator in validators : <EOL> error = validator ( item ) <EOL> if error : <EOL> return error <EOL> return None <EOL> return validate <EOL> def distinct ( ) : <EOL> """<STR_LIT>""" <EOL> def validate ( value ) : <EOL> for i , item in enumerate ( value ) : <EOL> if item in value [ i + <NUM_LIT:1> : ] : <EOL> return e ( "<STR_LIT>" , value ) <EOL> return validate </s>
