<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> import collections <EOL> import datetime as dt <EOL> import uuid <EOL> import warnings <EOL> import decimal <EOL> from operator import attrgetter <EOL> from marshmallow import validate , utils , class_registry <EOL> from marshmallow . base import FieldABC , SchemaABC <EOL> from marshmallow . utils import missing as missing_ <EOL> from marshmallow . compat import text_type , basestring <EOL> from marshmallow . exceptions import ValidationError <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> MISSING_ERROR_MESSAGE = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> _RECURSIVE_NESTED = '<STR_LIT>' <EOL> class Field ( FieldABC ) : <EOL> """<STR_LIT>""" <EOL> _CHECK_ATTRIBUTE = True <EOL> _creation_index = <NUM_LIT:0> <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:null>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , default = missing_ , attribute = None , load_from = None , dump_to = None , <EOL> error = None , validate = None , required = False , allow_none = None , load_only = False , <EOL> dump_only = False , missing = missing_ , error_messages = None , ** metadata ) : <EOL> self . default = default <EOL> self . attribute = attribute <EOL> self . load_from = load_from <EOL> self . dump_to = dump_to <EOL> self . validate = validate <EOL> if utils . is_iterable_but_not_string ( validate ) : <EOL> if not utils . is_generator ( validate ) : <EOL> self . validators = validate <EOL> else : <EOL> self . validators = list ( validate ) <EOL> elif callable ( validate ) : <EOL> self . validators = [ validate ] <EOL> elif validate is None : <EOL> self . validators = [ ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . required = required <EOL> if allow_none is None : <EOL> if missing is None : <EOL> self . allow_none = True <EOL> else : <EOL> self . allow_none = False <EOL> else : <EOL> self . allow_none = allow_none <EOL> self . load_only = load_only <EOL> self . dump_only = dump_only <EOL> self . missing = missing <EOL> self . metadata = metadata <EOL> self . _creation_index = Field . _creation_index <EOL> Field . _creation_index += <NUM_LIT:1> <EOL> messages = { } <EOL> for cls in reversed ( self . __class__ . __mro__ ) : <EOL> messages . update ( getattr ( cls , '<STR_LIT>' , { } ) ) <EOL> messages . update ( error_messages or { } ) <EOL> self . error_messages = messages <EOL> def __repr__ ( self ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( ClassName = self . __class__ . __name__ , self = self ) ) <EOL> def get_value ( self , attr , obj , accessor = None , default = missing_ ) : <EOL> """<STR_LIT>""" <EOL> attribute = getattr ( self , '<STR_LIT>' , None ) <EOL> accessor_func = accessor or utils . get_value <EOL> check_key = attr if attribute is None else attribute <EOL> return accessor_func ( check_key , obj , default ) <EOL> def _validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> kwargs = { } <EOL> for validator in self . validators : <EOL> try : <EOL> if validator ( value ) is False : <EOL> self . fail ( '<STR_LIT>' ) <EOL> except ValidationError as err : <EOL> kwargs . update ( err . kwargs ) <EOL> if isinstance ( err . messages , dict ) : <EOL> errors . append ( err . messages ) <EOL> else : <EOL> errors . extend ( err . messages ) <EOL> if errors : <EOL> raise ValidationError ( errors , ** kwargs ) <EOL> def fail ( self , key , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> msg = self . error_messages [ key ] <EOL> except KeyError : <EOL> class_name = self . __class__ . __name__ <EOL> msg = MISSING_ERROR_MESSAGE . format ( class_name = class_name , key = key ) <EOL> raise AssertionError ( msg ) <EOL> if isinstance ( msg , basestring ) : <EOL> msg = msg . format ( ** kwargs ) <EOL> raise ValidationError ( msg ) <EOL> def _validate_missing ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is missing_ : <EOL> if hasattr ( self , '<STR_LIT>' ) and self . required : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if value is None : <EOL> if hasattr ( self , '<STR_LIT>' ) and self . allow_none is not True : <EOL> self . fail ( '<STR_LIT:null>' ) <EOL> def serialize ( self , attr , obj , accessor = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _CHECK_ATTRIBUTE : <EOL> value = self . get_value ( attr , obj , accessor = accessor ) <EOL> if value is missing_ : <EOL> if hasattr ( self , '<STR_LIT:default>' ) : <EOL> if callable ( self . default ) : <EOL> return self . default ( ) <EOL> else : <EOL> return self . default <EOL> else : <EOL> value = None <EOL> return self . _serialize ( value , attr , obj ) <EOL> def deserialize ( self , value , attr = None , data = None ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_missing ( value ) <EOL> if getattr ( self , '<STR_LIT>' , False ) is True and value is None : <EOL> return None <EOL> output = self . _deserialize ( value , attr , data ) <EOL> self . _validate ( output ) <EOL> return output <EOL> def _add_to_schema ( self , field_name , schema ) : <EOL> """<STR_LIT>""" <EOL> self . parent = self . parent or schema <EOL> self . name = self . name or field_name <EOL> def _serialize ( self , value , attr , obj ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def _deserialize ( self , value , attr , data ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> @ property <EOL> def context ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . parent . context <EOL> @ property <EOL> def root ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = self <EOL> while hasattr ( ret , '<STR_LIT>' ) and ret . parent : <EOL> ret = ret . parent <EOL> return ret <EOL> class Raw ( Field ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Nested ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , nested , default = missing_ , exclude = tuple ( ) , only = None , ** kwargs ) : <EOL> self . nested = nested <EOL> self . only = only <EOL> self . exclude = exclude <EOL> self . many = kwargs . get ( '<STR_LIT>' , False ) <EOL> self . __schema = None <EOL> self . __updated_fields = False <EOL> super ( Nested , self ) . __init__ ( default = default , ** kwargs ) <EOL> @ property <EOL> def schema ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . only , basestring ) : <EOL> only = ( self . only , ) <EOL> else : <EOL> only = self . only <EOL> context = getattr ( self . parent , '<STR_LIT>' , { } ) <EOL> if not self . __schema : <EOL> if isinstance ( self . nested , SchemaABC ) : <EOL> self . __schema = self . nested <EOL> self . __schema . context . update ( context ) <EOL> elif isinstance ( self . nested , type ) and issubclass ( self . nested , SchemaABC ) : <EOL> self . __schema = self . nested ( many = self . many , <EOL> only = only , exclude = self . exclude , context = context ) <EOL> elif isinstance ( self . nested , basestring ) : <EOL> if self . nested == _RECURSIVE_NESTED : <EOL> parent_class = self . parent . __class__ <EOL> self . __schema = parent_class ( many = self . many , only = only , <EOL> exclude = self . exclude , context = context ) <EOL> else : <EOL> schema_class = class_registry . get_class ( self . nested ) <EOL> self . __schema = schema_class ( many = self . many , <EOL> only = only , exclude = self . exclude , context = context ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . nested . __class__ ) ) <EOL> self . __schema . ordered = getattr ( self . parent , '<STR_LIT>' , False ) <EOL> return self . __schema <EOL> def _serialize ( self , nested_obj , attr , obj ) : <EOL> schema = self . schema <EOL> if nested_obj is None : <EOL> return None <EOL> if not self . __updated_fields : <EOL> schema . _update_fields ( obj = nested_obj , many = self . many ) <EOL> self . __updated_fields = True <EOL> ret = schema . dump ( nested_obj , many = self . many , <EOL> update_fields = not self . __updated_fields ) . data <EOL> if isinstance ( self . only , basestring ) : <EOL> if self . many : <EOL> return utils . pluck ( ret , key = self . only ) <EOL> else : <EOL> return ret [ self . only ] <EOL> return ret <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if self . many and not utils . is_collection ( value ) : <EOL> self . fail ( '<STR_LIT:type>' , input = value , type = value . __class__ . __name__ ) <EOL> data , errors = self . schema . load ( value ) <EOL> if errors : <EOL> raise ValidationError ( errors , data = data ) <EOL> return data <EOL> def _validate_missing ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is missing_ and hasattr ( self , '<STR_LIT>' ) : <EOL> if self . nested == _RECURSIVE_NESTED : <EOL> self . fail ( '<STR_LIT>' ) <EOL> errors = self . _check_required ( ) <EOL> if errors : <EOL> raise ValidationError ( errors ) <EOL> else : <EOL> super ( Nested , self ) . _validate_missing ( value ) <EOL> def _check_required ( self ) : <EOL> errors = { } <EOL> if self . required : <EOL> for field_name , field in self . schema . fields . items ( ) : <EOL> if not field . required : <EOL> continue <EOL> error_field_name = field . load_from or field_name <EOL> if ( <EOL> isinstance ( field , Nested ) and <EOL> self . nested != _RECURSIVE_NESTED and <EOL> field . nested != _RECURSIVE_NESTED <EOL> ) : <EOL> errors [ error_field_name ] = field . _check_required ( ) <EOL> else : <EOL> try : <EOL> field . _validate_missing ( field . missing ) <EOL> except ValidationError as ve : <EOL> errors [ error_field_name ] = ve . messages <EOL> if self . many and errors : <EOL> errors = { <NUM_LIT:0> : errors } <EOL> if not errors : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return errors <EOL> class List ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , cls_or_instance , ** kwargs ) : <EOL> super ( List , self ) . __init__ ( ** kwargs ) <EOL> if isinstance ( cls_or_instance , type ) : <EOL> if not issubclass ( cls_or_instance , FieldABC ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . container = cls_or_instance ( ) <EOL> else : <EOL> if not isinstance ( cls_or_instance , FieldABC ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . container = cls_or_instance <EOL> def get_value ( self , attr , obj , accessor = None ) : <EOL> """<STR_LIT>""" <EOL> value = super ( List , self ) . get_value ( attr , obj , accessor = accessor ) <EOL> if self . container . attribute : <EOL> if utils . is_collection ( value ) : <EOL> return [ <EOL> self . container . get_value ( self . container . attribute , each ) <EOL> for each in value <EOL> ] <EOL> return self . container . get_value ( self . container . attribute , value ) <EOL> return value <EOL> def _add_to_schema ( self , field_name , schema ) : <EOL> super ( List , self ) . _add_to_schema ( field_name , schema ) <EOL> self . container . parent = self <EOL> self . container . name = field_name <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> if utils . is_collection ( value ) : <EOL> return [ self . container . _serialize ( each , attr , obj ) for each in value ] <EOL> return [ self . container . _serialize ( value , attr , obj ) ] <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if not utils . is_collection ( value ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> result = [ ] <EOL> errors = { } <EOL> for idx , each in enumerate ( value ) : <EOL> try : <EOL> result . append ( self . container . deserialize ( each ) ) <EOL> except ValidationError as e : <EOL> result . append ( e . data ) <EOL> errors . update ( { idx : e . messages } ) <EOL> if errors : <EOL> raise ValidationError ( errors , data = result ) <EOL> return result <EOL> class String ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> return super ( String , self ) . __init__ ( * args , ** kwargs ) <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> return utils . ensure_text_type ( value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if not isinstance ( value , basestring ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return utils . ensure_text_type ( value ) <EOL> class UUID ( String ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def _deserialize ( self , value , attr , data ) : <EOL> try : <EOL> return uuid . UUID ( value ) <EOL> except ( ValueError , AttributeError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class Number ( Field ) : <EOL> """<STR_LIT>""" <EOL> num_type = float <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , as_string = False , ** kwargs ) : <EOL> self . as_string = as_string <EOL> super ( Number , self ) . __init__ ( ** kwargs ) <EOL> def _format_num ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> return self . num_type ( value ) <EOL> def _validated ( self , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _format_num ( value ) <EOL> except ( TypeError , ValueError ) as err : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def serialize ( self , attr , obj , accessor = None ) : <EOL> """<STR_LIT>""" <EOL> ret = Field . serialize ( self , attr , obj , accessor = accessor ) <EOL> return str ( ret ) if ( self . as_string and ret is not None ) else ret <EOL> def _serialize ( self , value , attr , obj ) : <EOL> return self . _validated ( value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> return self . _validated ( value ) <EOL> class Integer ( Number ) : <EOL> """<STR_LIT>""" <EOL> num_type = int <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class Decimal ( Number ) : <EOL> """<STR_LIT>""" <EOL> num_type = decimal . Decimal <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , places = None , rounding = None , allow_nan = False , as_string = False , ** kwargs ) : <EOL> self . places = decimal . Decimal ( ( <NUM_LIT:0> , ( <NUM_LIT:1> , ) , - places ) ) if places is not None else None <EOL> self . rounding = rounding <EOL> self . allow_nan = allow_nan <EOL> super ( Decimal , self ) . __init__ ( as_string = as_string , ** kwargs ) <EOL> def _format_num ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> num = decimal . Decimal ( value ) <EOL> if self . allow_nan : <EOL> if num . is_nan ( ) : <EOL> return decimal . Decimal ( '<STR_LIT>' ) <EOL> else : <EOL> if num . is_nan ( ) or num . is_infinite ( ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if self . places is not None and num . is_finite ( ) : <EOL> num = num . quantize ( self . places , rounding = self . rounding ) <EOL> return num <EOL> def _validated ( self , value ) : <EOL> try : <EOL> return super ( Decimal , self ) . _validated ( value ) <EOL> except decimal . InvalidOperation : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class Boolean ( Field ) : <EOL> """<STR_LIT>""" <EOL> truthy = set ( ( '<STR_LIT:t>' , '<STR_LIT:T>' , '<STR_LIT:true>' , '<STR_LIT:True>' , '<STR_LIT>' , '<STR_LIT:1>' , <NUM_LIT:1> , True ) ) <EOL> falsy = set ( ( '<STR_LIT:f>' , '<STR_LIT:F>' , '<STR_LIT:false>' , '<STR_LIT:False>' , '<STR_LIT>' , '<STR_LIT:0>' , <NUM_LIT:0> , <NUM_LIT:0.0> , False ) ) <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> elif value in self . truthy : <EOL> return True <EOL> elif value in self . falsy : <EOL> return False <EOL> return bool ( value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if not self . truthy : <EOL> return bool ( value ) <EOL> else : <EOL> try : <EOL> if value in self . truthy : <EOL> return True <EOL> elif value in self . falsy : <EOL> return False <EOL> except TypeError : <EOL> pass <EOL> self . fail ( '<STR_LIT>' ) <EOL> class FormattedString ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> _CHECK_ATTRIBUTE = False <EOL> def __init__ ( self , src_str , * args , ** kwargs ) : <EOL> Field . __init__ ( self , * args , ** kwargs ) <EOL> self . src_str = text_type ( src_str ) <EOL> def _serialize ( self , value , attr , obj ) : <EOL> try : <EOL> data = utils . to_marshallable_type ( obj ) <EOL> return self . src_str . format ( ** data ) <EOL> except ( TypeError , IndexError ) as error : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class Float ( Number ) : <EOL> """<STR_LIT>""" <EOL> num_type = float <EOL> class DateTime ( Field ) : <EOL> """<STR_LIT>""" <EOL> DATEFORMAT_SERIALIZATION_FUNCS = { <EOL> '<STR_LIT>' : utils . isoformat , <EOL> '<STR_LIT>' : utils . isoformat , <EOL> '<STR_LIT>' : utils . rfcformat , <EOL> '<STR_LIT>' : utils . rfcformat , <EOL> } <EOL> DATEFORMAT_DESERIALIZATION_FUNCS = { <EOL> '<STR_LIT>' : utils . from_iso , <EOL> '<STR_LIT>' : utils . from_iso , <EOL> '<STR_LIT>' : utils . from_rfc , <EOL> '<STR_LIT>' : utils . from_rfc , <EOL> } <EOL> DEFAULT_FORMAT = '<STR_LIT>' <EOL> localtime = False <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , format = None , ** kwargs ) : <EOL> super ( DateTime , self ) . __init__ ( ** kwargs ) <EOL> self . dateformat = format <EOL> def _add_to_schema ( self , field_name , schema ) : <EOL> super ( DateTime , self ) . _add_to_schema ( field_name , schema ) <EOL> self . dateformat = self . dateformat or schema . opts . dateformat <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> self . dateformat = self . dateformat or self . DEFAULT_FORMAT <EOL> format_func = self . DATEFORMAT_SERIALIZATION_FUNCS . get ( self . dateformat , None ) <EOL> if format_func : <EOL> try : <EOL> return format_func ( value , localtime = self . localtime ) <EOL> except ( AttributeError , ValueError ) as err : <EOL> self . fail ( '<STR_LIT>' , input = value ) <EOL> else : <EOL> return value . strftime ( self . dateformat ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if not value : <EOL> raise self . fail ( '<STR_LIT>' ) <EOL> self . dateformat = self . dateformat or self . DEFAULT_FORMAT <EOL> func = self . DATEFORMAT_DESERIALIZATION_FUNCS . get ( self . dateformat ) <EOL> if func : <EOL> try : <EOL> return func ( value ) <EOL> except ( TypeError , AttributeError , ValueError ) : <EOL> raise self . fail ( '<STR_LIT>' ) <EOL> elif self . dateformat : <EOL> try : <EOL> return dt . datetime . strptime ( value , self . dateformat ) <EOL> except ( TypeError , AttributeError , ValueError ) : <EOL> raise self . fail ( '<STR_LIT>' ) <EOL> elif utils . dateutil_available : <EOL> try : <EOL> return utils . from_datestring ( value ) <EOL> except TypeError : <EOL> raise self . fail ( '<STR_LIT>' ) <EOL> else : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise self . fail ( '<STR_LIT>' ) <EOL> class LocalDateTime ( DateTime ) : <EOL> """<STR_LIT>""" <EOL> localtime = True <EOL> class Time ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> try : <EOL> ret = value . isoformat ( ) <EOL> except AttributeError : <EOL> self . fail ( '<STR_LIT>' , input = value ) <EOL> if value . microsecond : <EOL> return ret [ : <NUM_LIT:12> ] <EOL> return ret <EOL> def _deserialize ( self , value , attr , data ) : <EOL> """<STR_LIT>""" <EOL> if not value : <EOL> self . fail ( '<STR_LIT>' ) <EOL> raise err <EOL> try : <EOL> return utils . from_iso_time ( value ) <EOL> except ( AttributeError , TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class Date ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> try : <EOL> return value . isoformat ( ) <EOL> except AttributeError : <EOL> self . fail ( '<STR_LIT>' , input = value ) <EOL> return value <EOL> def _deserialize ( self , value , attr , data ) : <EOL> """<STR_LIT>""" <EOL> if not value : <EOL> self . fail ( '<STR_LIT>' ) <EOL> try : <EOL> return utils . from_iso_date ( value ) <EOL> except ( AttributeError , TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class TimeDelta ( Field ) : <EOL> """<STR_LIT>""" <EOL> DAYS = '<STR_LIT>' <EOL> SECONDS = '<STR_LIT>' <EOL> MICROSECONDS = '<STR_LIT>' <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , precision = '<STR_LIT>' , error = None , ** kwargs ) : <EOL> precision = precision . lower ( ) <EOL> units = ( self . DAYS , self . SECONDS , self . MICROSECONDS ) <EOL> if precision not in units : <EOL> msg = '<STR_LIT>' . format ( * units ) <EOL> raise ValueError ( msg ) <EOL> self . precision = precision <EOL> super ( TimeDelta , self ) . __init__ ( error = error , ** kwargs ) <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if value is None : <EOL> return None <EOL> try : <EOL> days = value . days <EOL> if self . precision == self . DAYS : <EOL> return days <EOL> else : <EOL> seconds = days * <NUM_LIT> + value . seconds <EOL> if self . precision == self . SECONDS : <EOL> return seconds <EOL> else : <EOL> return seconds * <NUM_LIT:10> ** <NUM_LIT:6> + value . microseconds <EOL> except AttributeError : <EOL> self . fail ( '<STR_LIT>' , input = value ) <EOL> def _deserialize ( self , value , attr , data ) : <EOL> try : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> kwargs = { self . precision : value } <EOL> try : <EOL> return dt . timedelta ( ** kwargs ) <EOL> except OverflowError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class Dict ( Field ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if isinstance ( value , collections . Mapping ) : <EOL> return value <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> class ValidatedField ( Field ) : <EOL> """<STR_LIT>""" <EOL> def _validated ( self , value ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _serialize ( self , value , * args , ** kwargs ) : <EOL> ret = super ( ValidatedField , self ) . _serialize ( value , * args , ** kwargs ) <EOL> return self . _validated ( ret ) <EOL> class Url ( ValidatedField , String ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def __init__ ( self , relative = False , ** kwargs ) : <EOL> String . __init__ ( self , ** kwargs ) <EOL> self . relative = relative <EOL> self . validators . insert ( <NUM_LIT:0> , validate . URL ( <EOL> relative = self . relative , <EOL> error = self . error_messages [ '<STR_LIT>' ] <EOL> ) ) <EOL> def _validated ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return validate . URL ( <EOL> relative = self . relative , <EOL> error = self . error_messages [ '<STR_LIT>' ] <EOL> ) ( value ) <EOL> class Email ( ValidatedField , String ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> String . __init__ ( self , * args , ** kwargs ) <EOL> self . validators . insert ( <NUM_LIT:0> , validate . Email ( error = self . error_messages [ '<STR_LIT>' ] ) ) <EOL> def _validated ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> return validate . Email ( <EOL> error = self . error_messages [ '<STR_LIT>' ] <EOL> ) ( value ) <EOL> class Method ( Field ) : <EOL> """<STR_LIT>""" <EOL> _CHECK_ATTRIBUTE = False <EOL> def __init__ ( self , serialize = None , deserialize = None , method_name = None , ** kwargs ) : <EOL> if method_name is not None : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , DeprecationWarning ) <EOL> self . serialize_method_name = self . method_name = serialize or method_name <EOL> self . deserialize_method_name = deserialize <EOL> super ( Method , self ) . __init__ ( ** kwargs ) <EOL> def _serialize ( self , value , attr , obj ) : <EOL> if not self . serialize_method_name : <EOL> return missing_ <EOL> method = utils . callable_or_raise ( <EOL> getattr ( self . parent , self . serialize_method_name , None ) <EOL> ) <EOL> try : <EOL> return method ( obj ) <EOL> except AttributeError : <EOL> pass <EOL> return missing_ <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if self . deserialize_method_name : <EOL> try : <EOL> method = utils . callable_or_raise ( <EOL> getattr ( self . parent , self . deserialize_method_name , None ) <EOL> ) <EOL> return method ( value ) <EOL> except AttributeError : <EOL> pass <EOL> return value <EOL> class Function ( Field ) : <EOL> """<STR_LIT>""" <EOL> _CHECK_ATTRIBUTE = False <EOL> def __init__ ( self , serialize = None , deserialize = None , func = None , ** kwargs ) : <EOL> if func : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , DeprecationWarning ) <EOL> serialize = func <EOL> super ( Function , self ) . __init__ ( ** kwargs ) <EOL> self . serialize_func = self . func = serialize and utils . callable_or_raise ( serialize ) <EOL> self . deserialize_func = deserialize and utils . callable_or_raise ( deserialize ) <EOL> def _serialize ( self , value , attr , obj ) : <EOL> try : <EOL> return self . _call_or_raise ( self . serialize_func , obj , attr ) <EOL> except AttributeError : <EOL> pass <EOL> return missing_ <EOL> def _deserialize ( self , value , attr , data ) : <EOL> if self . deserialize_func : <EOL> return self . _call_or_raise ( self . deserialize_func , value , attr ) <EOL> return value <EOL> def _call_or_raise ( self , func , value , attr ) : <EOL> if len ( utils . get_func_args ( func ) ) > <NUM_LIT:1> : <EOL> if self . parent . context is None : <EOL> msg = '<STR_LIT>' . format ( attr ) <EOL> raise ValidationError ( msg ) <EOL> return func ( value , self . parent . context ) <EOL> else : <EOL> return func ( value ) <EOL> class Constant ( Field ) : <EOL> """<STR_LIT>""" <EOL> _CHECK_ATTRIBUTE = False <EOL> def __init__ ( self , constant , ** kwargs ) : <EOL> super ( Constant , self ) . __init__ ( ** kwargs ) <EOL> self . constant = constant <EOL> self . missing = constant <EOL> self . default = constant <EOL> def _serialize ( self , value , * args , ** kwargs ) : <EOL> return self . constant <EOL> def _deserialize ( self , value , * args , ** kwargs ) : <EOL> return self . constant <EOL> URL = Url <EOL> Str = String <EOL> Bool = Boolean <EOL> Int = Integer </s>
