<s> """<STR_LIT>""" <EOL> import decimal <EOL> import datetime <EOL> import re <EOL> import time <EOL> try : <EOL> from collections import MutableSet , Iterable <EOL> def is_iterable ( c ) : <EOL> return isinstance ( c , Iterable ) <EOL> support_setproperty = True <EOL> except ImportError : <EOL> support_setproperty = False <EOL> from couchdbkit . exceptions import BadValueError <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if support_setproperty : <EOL> __all__ += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ALLOWED_PROPERTY_TYPES = set ( [ <EOL> basestring , <EOL> str , <EOL> unicode , <EOL> bool , <EOL> int , <EOL> long , <EOL> float , <EOL> datetime . datetime , <EOL> datetime . date , <EOL> datetime . time , <EOL> decimal . Decimal , <EOL> dict , <EOL> list , <EOL> set , <EOL> type ( None ) <EOL> ] ) <EOL> re_date = re . compile ( '<STR_LIT>' ) <EOL> re_time = re . compile ( '<STR_LIT>' ) <EOL> re_datetime = re . compile ( '<STR_LIT>' ) <EOL> re_decimal = re . compile ( '<STR_LIT>' ) <EOL> class Property ( object ) : <EOL> """<STR_LIT>""" <EOL> creation_counter = <NUM_LIT:0> <EOL> def __init__ ( self , verbose_name = None , name = None , <EOL> default = None , required = False , validators = None , <EOL> choices = None ) : <EOL> """<STR_LIT>""" <EOL> self . verbose_name = verbose_name <EOL> self . name = name <EOL> self . default = default <EOL> self . required = required <EOL> self . validators = validators <EOL> self . choices = choices <EOL> self . creation_counter = Property . creation_counter <EOL> Property . creation_counter += <NUM_LIT:1> <EOL> def __property_config__ ( self , document_class , property_name ) : <EOL> self . document_class = document_class <EOL> if self . name is None : <EOL> self . name = property_name <EOL> def __property_init__ ( self , document_instance , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None : <EOL> value = self . to_json ( self . validate ( value , required = False ) ) <EOL> document_instance . _doc [ self . name ] = value <EOL> def __get__ ( self , document_instance , document_class ) : <EOL> if document_instance is None : <EOL> return self <EOL> value = document_instance . _doc . get ( self . name ) <EOL> if value is not None : <EOL> value = self . _to_python ( value ) <EOL> return value <EOL> def __set__ ( self , document_instance , value ) : <EOL> value = self . validate ( value , required = False ) <EOL> document_instance . _doc [ self . name ] = self . _to_json ( value ) <EOL> def __delete__ ( self , document_instance ) : <EOL> pass <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> default = self . default <EOL> if callable ( default ) : <EOL> default = default ( ) <EOL> return default <EOL> def validate ( self , value , required = True ) : <EOL> """<STR_LIT>""" <EOL> if required and self . empty ( value ) : <EOL> if self . required : <EOL> raise BadValueError ( "<STR_LIT>" % self . name ) <EOL> else : <EOL> if self . choices and value is not None : <EOL> if isinstance ( self . choices , list ) : choice_list = self . choices <EOL> if isinstance ( self . choices , dict ) : choice_list = self . choices . keys ( ) <EOL> if isinstance ( self . choices , tuple ) : choice_list = [ key for ( key , name ) in self . choices ] <EOL> if value not in choice_list : <EOL> raise BadValueError ( '<STR_LIT>' % ( <EOL> self . name , value , choice_list ) ) <EOL> if self . validators : <EOL> if isinstance ( self . validators , ( list , tuple , ) ) : <EOL> for validator in self . validators : <EOL> if callable ( validator ) : <EOL> validator ( value ) <EOL> elif callable ( self . validators ) : <EOL> self . validators ( value ) <EOL> return value <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return ( not value and value != <NUM_LIT:0> ) or value is None <EOL> def _to_python ( self , value ) : <EOL> if value == None : <EOL> return value <EOL> return self . to_python ( value ) <EOL> def _to_json ( self , value ) : <EOL> if value == None : <EOL> return value <EOL> return self . to_json ( value ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return unicode ( value ) <EOL> def to_json ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . to_python ( value ) <EOL> data_type = None <EOL> class StringProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> to_python = unicode <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( StringProperty , self ) . validate ( value , <EOL> required = required ) <EOL> if value is None : <EOL> return value <EOL> if not isinstance ( value , basestring ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % ( self . name , type ( value ) . __name__ ) ) <EOL> return value <EOL> data_type = unicode <EOL> class IntegerProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> to_python = int <EOL> def empty ( self , value ) : <EOL> return value is None <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( IntegerProperty , self ) . validate ( value , <EOL> required = required ) <EOL> if value is None : <EOL> return value <EOL> if value is not None and not isinstance ( value , ( int , long , ) ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' <EOL> % ( self . name , type ( self . data_type ) . __name__ , <EOL> type ( value ) . __name__ ) ) <EOL> return value <EOL> data_type = int <EOL> LongProperty = IntegerProperty <EOL> class FloatProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> to_python = float <EOL> data_type = float <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( FloatProperty , self ) . validate ( value , <EOL> required = required ) <EOL> if value is None : <EOL> return value <EOL> if not isinstance ( value , float ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' <EOL> % ( self . name , type ( value ) . __name__ ) ) <EOL> return value <EOL> Number = FloatProperty <EOL> class BooleanProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> to_python = bool <EOL> data_type = bool <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( BooleanProperty , self ) . validate ( value , <EOL> required = required ) <EOL> if value is None : <EOL> return value <EOL> if value is not None and not isinstance ( value , bool ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' <EOL> % ( self . name , type ( value ) . __name__ ) ) <EOL> return value <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value is None <EOL> class DecimalProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> data_type = decimal . Decimal <EOL> def to_python ( self , value ) : <EOL> return decimal . Decimal ( value ) <EOL> def to_json ( self , value ) : <EOL> return unicode ( value ) <EOL> class DateTimeProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , auto_now = False , auto_now_add = False , <EOL> ** kwds ) : <EOL> super ( DateTimeProperty , self ) . __init__ ( verbose_name , ** kwds ) <EOL> self . auto_now = auto_now <EOL> self . auto_now_add = auto_now_add <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( DateTimeProperty , self ) . validate ( value , required = required ) <EOL> if value is None : <EOL> return value <EOL> if value and not isinstance ( value , self . data_type ) : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , self . data_type . __name__ , type ( value ) . __name__ ) ) <EOL> return value <EOL> def default_value ( self ) : <EOL> if self . auto_now or self . auto_now_add : <EOL> return self . now ( ) <EOL> return Property . default_value ( self ) <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> try : <EOL> value = value . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> value = value [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> value = datetime . datetime . strptime ( value , '<STR_LIT>' ) <EOL> except ValueError , e : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( value , str ( e ) ) ) <EOL> return value <EOL> def to_json ( self , value ) : <EOL> if self . auto_now : <EOL> value = self . now ( ) <EOL> if value is None : <EOL> return value <EOL> return value . replace ( microsecond = <NUM_LIT:0> ) . isoformat ( ) + '<STR_LIT>' <EOL> data_type = datetime . datetime <EOL> @ staticmethod <EOL> def now ( ) : <EOL> return datetime . datetime . utcnow ( ) <EOL> class DateProperty ( DateTimeProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = datetime . date <EOL> @ staticmethod <EOL> def now ( ) : <EOL> return datetime . datetime . now ( ) . date ( ) <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> try : <EOL> value = datetime . date ( * time . strptime ( value , '<STR_LIT>' ) [ : <NUM_LIT:3> ] ) <EOL> except ValueError , e : <EOL> raise ValueError ( '<STR_LIT>' % ( value , <EOL> str ( e ) ) ) <EOL> return value <EOL> def to_json ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> return value . isoformat ( ) <EOL> class TimeProperty ( DateTimeProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = datetime . time <EOL> @ staticmethod <EOL> def now ( self ) : <EOL> return datetime . datetime . now ( ) . time ( ) <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> try : <EOL> value = value . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> value = datetime . time ( * time . strptime ( value , '<STR_LIT>' ) [ <NUM_LIT:3> : <NUM_LIT:6> ] ) <EOL> except ValueError , e : <EOL> raise ValueError ( '<STR_LIT>' % ( value , <EOL> str ( e ) ) ) <EOL> return value <EOL> def to_json ( self , value ) : <EOL> if value is None : <EOL> return value <EOL> return value . replace ( microsecond = <NUM_LIT:0> ) . isoformat ( ) <EOL> class DictProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , default = None , <EOL> required = False , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if default is None : <EOL> default = { } <EOL> Property . __init__ ( self , verbose_name , default = default , <EOL> required = required , ** kwds ) <EOL> data_type = dict <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( DictProperty , self ) . validate ( value , required = required ) <EOL> if value and value is not None : <EOL> if not isinstance ( value , dict ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> value = self . validate_dict_contents ( value ) <EOL> return value <EOL> def validate_dict_contents ( self , value ) : <EOL> try : <EOL> value = validate_dict_content ( value ) <EOL> except BadValueError : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % <EOL> ( self . name , ALLOWED_PROPERTY_TYPES ) ) <EOL> return value <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> value = super ( DictProperty , self ) . default_value ( ) <EOL> if value is None : <EOL> value = { } <EOL> return dict ( value ) <EOL> def to_python ( self , value ) : <EOL> return LazyDict ( value ) <EOL> def to_json ( self , value ) : <EOL> return value_to_json ( value ) <EOL> class StringDictProperty ( DictProperty ) : <EOL> def to_python ( self , value ) : <EOL> return LazyDict ( value , item_type = basestring ) <EOL> def validate_dict_contents ( self , value ) : <EOL> try : <EOL> value = validate_dict_content ( value , basestring ) <EOL> except BadValueError : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % <EOL> ( self . name , basestring ) ) <EOL> return value <EOL> class ListProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , default = None , <EOL> required = False , item_type = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if default is None : <EOL> default = [ ] <EOL> if item_type is not None and item_type not in ALLOWED_PROPERTY_TYPES : <EOL> raise ValueError ( '<STR_LIT>' % ( item_type , ALLOWED_PROPERTY_TYPES ) ) <EOL> self . item_type = item_type <EOL> Property . __init__ ( self , verbose_name , default = default , <EOL> required = required , ** kwds ) <EOL> data_type = list <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( ListProperty , self ) . validate ( value , required = required ) <EOL> if value and value is not None : <EOL> if not isinstance ( value , list ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> value = self . validate_list_contents ( value ) <EOL> return value <EOL> def validate_list_contents ( self , value ) : <EOL> value = validate_list_content ( value , item_type = self . item_type ) <EOL> try : <EOL> value = validate_list_content ( value , item_type = self . item_type ) <EOL> except BadValueError : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % <EOL> ( self . name , ALLOWED_PROPERTY_TYPES ) ) <EOL> return value <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> value = super ( ListProperty , self ) . default_value ( ) <EOL> if value is None : <EOL> value = [ ] <EOL> return list ( value ) <EOL> def to_python ( self , value ) : <EOL> return LazyList ( value , item_type = self . item_type ) <EOL> def to_json ( self , value ) : <EOL> return value_to_json ( value , item_type = self . item_type ) <EOL> class StringListProperty ( ListProperty ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , default = None , <EOL> required = False , ** kwds ) : <EOL> super ( StringListProperty , self ) . __init__ ( verbose_name = verbose_name , <EOL> default = default , required = required , item_type = basestring , ** kwds ) <EOL> class LazyDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , doc , item_type = None , init_vals = None ) : <EOL> dict . __init__ ( self ) <EOL> self . item_type = item_type <EOL> self . doc = doc <EOL> if init_vals is None : <EOL> self . _wrap ( ) <EOL> else : <EOL> for key , value in init_vals . items ( ) : <EOL> self [ key ] = value <EOL> def _wrap ( self ) : <EOL> for key , json_value in self . doc . items ( ) : <EOL> if isinstance ( json_value , dict ) : <EOL> value = LazyDict ( json_value , item_type = self . item_type ) <EOL> elif isinstance ( json_value , list ) : <EOL> value = LazyList ( json_value , item_type = self . item_type ) <EOL> else : <EOL> value = value_to_python ( json_value , self . item_type ) <EOL> dict . __setitem__ ( self , key , value ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if isinstance ( value , dict ) : <EOL> self . doc [ key ] = { } <EOL> value = LazyDict ( self . doc [ key ] , item_type = self . item_type , init_vals = value ) <EOL> elif isinstance ( value , list ) : <EOL> self . doc [ key ] = [ ] <EOL> value = LazyList ( self . doc [ key ] , item_type = self . item_type , init_vals = value ) <EOL> else : <EOL> self . doc . update ( { key : value_to_json ( value , item_type = self . item_type ) } ) <EOL> super ( LazyDict , self ) . __setitem__ ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . doc [ key ] <EOL> super ( LazyDict , self ) . __delitem__ ( key ) <EOL> def pop ( self , key , * args ) : <EOL> default = len ( args ) == <NUM_LIT:1> <EOL> if default : <EOL> self . doc . pop ( key , args [ - <NUM_LIT:1> ] ) <EOL> return super ( LazyDict , self ) . pop ( key , args [ - <NUM_LIT:1> ] ) <EOL> self . doc . pop ( key ) <EOL> return super ( LazyDict , self ) . pop ( key ) <EOL> def setdefault ( self , key , default ) : <EOL> if key in self : <EOL> return self [ key ] <EOL> self . doc . setdefault ( key , value_to_json ( default , item_type = self . item_type ) ) <EOL> super ( LazyDict , self ) . setdefault ( key , default ) <EOL> return default <EOL> def update ( self , value ) : <EOL> for k , v in value . items ( ) : <EOL> self [ k ] = v <EOL> def popitem ( self , value ) : <EOL> new_value = super ( LazyDict , self ) . popitem ( value ) <EOL> self . doc . popitem ( value_to_json ( value , item_type = self . item_type ) ) <EOL> return new_value <EOL> def clear ( self ) : <EOL> self . doc . clear ( ) <EOL> super ( LazyDict , self ) . clear ( ) <EOL> class LazyList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , doc , item_type = None , init_vals = None ) : <EOL> list . __init__ ( self ) <EOL> self . item_type = item_type <EOL> self . doc = doc <EOL> if init_vals is None : <EOL> self . _wrap ( ) <EOL> else : <EOL> del self . doc [ : ] <EOL> for item in init_vals : <EOL> self . append ( item ) <EOL> def _wrap ( self ) : <EOL> for json_value in self . doc : <EOL> if isinstance ( json_value , dict ) : <EOL> value = LazyDict ( json_value , item_type = self . item_type ) <EOL> elif isinstance ( json_value , list ) : <EOL> value = LazyList ( json_value , item_type = self . item_type ) <EOL> else : <EOL> value = value_to_python ( json_value , self . item_type ) <EOL> list . append ( self , value ) <EOL> def __delitem__ ( self , index ) : <EOL> del self . doc [ index ] <EOL> list . __delitem__ ( self , index ) <EOL> def __setitem__ ( self , index , value ) : <EOL> if isinstance ( value , dict ) : <EOL> self . doc [ index ] = { } <EOL> value = LazyDict ( self . doc [ index ] , item_type = self . item_type , init_vals = value ) <EOL> elif isinstance ( value , list ) : <EOL> self . doc [ index ] = [ ] <EOL> value = LazyList ( self . doc [ index ] , item_type = self . item_type , init_vals = value ) <EOL> else : <EOL> self . doc [ index ] = value_to_json ( value , item_type = self . item_type ) <EOL> list . __setitem__ ( self , index , value ) <EOL> def __delslice__ ( self , i , j ) : <EOL> del self . doc [ i : j ] <EOL> list . __delslice__ ( self , i , j ) <EOL> def __getslice__ ( self , i , j ) : <EOL> return LazyList ( self . doc [ i : j ] , self . item_type ) <EOL> def __setslice__ ( self , i , j , seq ) : <EOL> self . doc [ i : j ] = ( value_to_json ( v , item_type = self . item_type ) for v in seq ) <EOL> list . __setslice__ ( self , i , j , seq ) <EOL> def __contains__ ( self , value ) : <EOL> jvalue = value_to_json ( value ) <EOL> for m in self . doc : <EOL> if m == jvalue : return True <EOL> return False <EOL> def append ( self , * args , ** kwargs ) : <EOL> if args : <EOL> assert len ( args ) == <NUM_LIT:1> <EOL> value = args [ <NUM_LIT:0> ] <EOL> else : <EOL> value = kwargs <EOL> index = len ( self ) <EOL> if isinstance ( value , dict ) : <EOL> self . doc . append ( { } ) <EOL> value = LazyDict ( self . doc [ index ] , item_type = self . item_type , init_vals = value ) <EOL> elif isinstance ( value , list ) : <EOL> self . doc . append ( [ ] ) <EOL> value = LazyList ( self . doc [ index ] , item_type = self . item_type , init_vals = value ) <EOL> else : <EOL> self . doc . append ( value_to_json ( value , item_type = self . item_type ) ) <EOL> super ( LazyList , self ) . append ( value ) <EOL> def extend ( self , x ) : <EOL> self . doc . extend ( <EOL> [ value_to_json ( v , item_type = self . item_type ) for v in x ] ) <EOL> super ( LazyList , self ) . extend ( x ) <EOL> def index ( self , x , * args ) : <EOL> x = value_to_json ( x , item_type = self . item_type ) <EOL> return self . doc . index ( x ) <EOL> def insert ( self , i , x ) : <EOL> self . __setslice__ ( i , i , [ x ] ) <EOL> def pop ( self , i = - <NUM_LIT:1> ) : <EOL> del self . doc [ i ] <EOL> v = super ( LazyList , self ) . pop ( i ) <EOL> return value_to_python ( v , item_type = self . item_type ) <EOL> def remove ( self , x ) : <EOL> del self [ self . index ( x ) ] <EOL> def sort ( self , cmp = None , key = None , reverse = False ) : <EOL> self . doc . sort ( cmp , key , reverse ) <EOL> list . sort ( self , cmp , key , reverse ) <EOL> def reverse ( self ) : <EOL> self . doc . reverse ( ) <EOL> list . reverse ( self ) <EOL> if support_setproperty : <EOL> class SetProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , default = None , required = None , <EOL> item_type = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if default is None : <EOL> default = set ( ) <EOL> if item_type is not None and item_type not in ALLOWED_PROPERTY_TYPES : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( item_type , ALLOWED_PROPERTY_TYPES ) ) <EOL> self . item_type = item_type <EOL> super ( SetProperty , self ) . __init__ ( <EOL> verbose_name = verbose_name , default = default , required = required , <EOL> ** kwds ) <EOL> data_type = set <EOL> def validate ( self , value , required = True ) : <EOL> value = super ( SetProperty , self ) . validate ( value , required = required ) <EOL> if value and value is not None : <EOL> if not isinstance ( value , MutableSet ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> value = self . validate_set_contents ( value ) <EOL> return value <EOL> def validate_set_contents ( self , value ) : <EOL> try : <EOL> value = validate_set_content ( value , item_type = self . item_type ) <EOL> except BadValueError : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % <EOL> ( self . name , ALLOWED_PROPERTY_TYPES ) ) <EOL> return value <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> value = super ( SetProperty , self ) . default_value ( ) <EOL> if value is None : <EOL> return set ( ) <EOL> return value . copy ( ) <EOL> def to_python ( self , value ) : <EOL> return LazySet ( value , item_type = self . item_type ) <EOL> def to_json ( self , value ) : <EOL> return value_to_json ( value , item_type = self . item_type ) <EOL> class LazySet ( MutableSet ) : <EOL> """<STR_LIT>""" <EOL> def _map_named_operation ( opname ) : <EOL> fn = getattr ( MutableSet , opname ) <EOL> if hasattr ( fn , '<STR_LIT>' ) : <EOL> fn = fn . im_func <EOL> def method ( self , other , fn = fn ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> other = self . _from_iterable ( other ) <EOL> return fn ( self , other ) <EOL> return method <EOL> issubset = _map_named_operation ( '<STR_LIT>' ) <EOL> issuperset = _map_named_operation ( '<STR_LIT>' ) <EOL> symmetric_difference = _map_named_operation ( '<STR_LIT>' ) <EOL> def __init__ ( self , doc , item_type = None ) : <EOL> self . item_type = item_type <EOL> self . doc = doc <EOL> self . elements = set ( value_to_python ( value , self . item_type ) <EOL> for value in self . doc ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , list ( self ) ) <EOL> @ classmethod <EOL> def _from_iterable ( cls , it ) : <EOL> return cls ( it ) <EOL> def __iand__ ( self , iterator ) : <EOL> for value in ( self . elements - iterator ) : <EOL> self . elements . discard ( value ) <EOL> return self <EOL> def __iter__ ( self ) : <EOL> return iter ( element for element in self . elements ) <EOL> def __len__ ( self ) : <EOL> return len ( self . elements ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . elements <EOL> def __xor__ ( self , other ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> if not is_iterable ( other ) : <EOL> return NotImplemented <EOL> other = self . _from_iterable ( other ) <EOL> return ( self . elements - other ) | ( other - self . elements ) <EOL> def __gt__ ( self , other ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> return NotImplemented <EOL> return other < self . elements <EOL> def __ge__ ( self , other ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> return NotImplemented <EOL> return other <= self . elements <EOL> def __ne__ ( self , other ) : <EOL> return not ( self . elements == other ) <EOL> def add ( self , value ) : <EOL> self . elements . add ( value ) <EOL> if value not in self . doc : <EOL> self . doc . append ( value_to_json ( value , item_type = self . item_type ) ) <EOL> def copy ( self ) : <EOL> return self . elements . copy ( ) <EOL> def difference ( self , other , * args ) : <EOL> return self . elements . difference ( other , * args ) <EOL> def difference_update ( self , other , * args ) : <EOL> for value in other : <EOL> self . discard ( value ) <EOL> for arg in args : <EOL> self . difference_update ( arg ) <EOL> def discard ( self , value ) : <EOL> self . elements . discard ( value ) <EOL> try : <EOL> self . doc . remove ( value ) <EOL> except ValueError : <EOL> pass <EOL> def intersection ( self , other , * args ) : <EOL> return self . elements . intersection ( other , * args ) <EOL> def intersection_update ( self , other , * args ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> other = set ( other ) <EOL> for value in self . elements - other : <EOL> self . discard ( value ) <EOL> for arg in args : <EOL> self . intersection_update ( arg ) <EOL> def symmetric_difference_update ( self , other ) : <EOL> if not isinstance ( other , MutableSet ) : <EOL> other = set ( other ) <EOL> for value in other : <EOL> if value in self . elements : <EOL> self . discard ( value ) <EOL> else : <EOL> self . add ( value ) <EOL> def union ( self , other , * args ) : <EOL> return self . elements . union ( other , * args ) <EOL> def update ( self , other , * args ) : <EOL> self . elements . update ( other , * args ) <EOL> for element in self . elements : <EOL> if element not in self . doc : <EOL> self . doc . append ( <EOL> value_to_json ( element , item_type = self . item_type ) ) <EOL> MAP_TYPES_PROPERTIES = { <EOL> decimal . Decimal : DecimalProperty , <EOL> datetime . datetime : DateTimeProperty , <EOL> datetime . date : DateProperty , <EOL> datetime . time : TimeProperty , <EOL> str : StringProperty , <EOL> unicode : StringProperty , <EOL> bool : BooleanProperty , <EOL> int : IntegerProperty , <EOL> long : LongProperty , <EOL> float : FloatProperty , <EOL> list : ListProperty , <EOL> dict : DictProperty <EOL> } <EOL> if support_setproperty : <EOL> MAP_TYPES_PROPERTIES [ set ] = SetProperty <EOL> def convert_property ( value ) : <EOL> """<STR_LIT>""" <EOL> if type ( value ) in MAP_TYPES_PROPERTIES : <EOL> prop = MAP_TYPES_PROPERTIES [ type ( value ) ] ( ) <EOL> value = prop . to_json ( value ) <EOL> return value <EOL> def value_to_property ( value ) : <EOL> """<STR_LIT>""" <EOL> if type ( value ) in MAP_TYPES_PROPERTIES : <EOL> prop = MAP_TYPES_PROPERTIES [ type ( value ) ] ( ) <EOL> return prop <EOL> else : <EOL> return value <EOL> def validate_list_content ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return [ validate_content ( item , item_type = item_type ) for item in value ] <EOL> def validate_dict_content ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( k , validate_content ( v , <EOL> item_type = item_type ) ) for k , v in value . iteritems ( ) ] ) <EOL> def validate_set_content ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return set ( validate_content ( item , item_type = item_type ) for item in value ) <EOL> def validate_content ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , list ) : <EOL> value = validate_list_content ( value , item_type = item_type ) <EOL> elif isinstance ( value , dict ) : <EOL> value = validate_dict_content ( value , item_type = item_type ) <EOL> elif item_type is not None and not isinstance ( value , item_type ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % item_type ) <EOL> elif type ( value ) not in ALLOWED_PROPERTY_TYPES : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' % <EOL> ( ALLOWED_PROPERTY_TYPES ) ) <EOL> return value <EOL> def dict_to_json ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( k , value_to_json ( v , item_type = item_type ) ) for k , v in value . iteritems ( ) ] ) <EOL> def list_to_json ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return [ value_to_json ( item , item_type = item_type ) for item in value ] <EOL> def value_to_json ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , datetime . datetime ) and is_type_ok ( item_type , datetime . datetime ) : <EOL> value = value . replace ( microsecond = <NUM_LIT:0> ) . isoformat ( ) + '<STR_LIT>' <EOL> elif isinstance ( value , datetime . date ) and is_type_ok ( item_type , datetime . date ) : <EOL> value = value . isoformat ( ) <EOL> elif isinstance ( value , datetime . time ) and is_type_ok ( item_type , datetime . time ) : <EOL> value = value . replace ( microsecond = <NUM_LIT:0> ) . isoformat ( ) <EOL> elif isinstance ( value , decimal . Decimal ) and is_type_ok ( item_type , decimal . Decimal ) : <EOL> value = unicode ( value ) <EOL> elif isinstance ( value , ( list , MutableSet ) ) : <EOL> value = list_to_json ( value , item_type ) <EOL> elif isinstance ( value , dict ) : <EOL> value = dict_to_json ( value , item_type ) <EOL> return value <EOL> def is_type_ok ( item_type , value_type ) : <EOL> return item_type is None or item_type == value_type <EOL> def value_to_python ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> data_type = None <EOL> if isinstance ( value , basestring ) : <EOL> if re_date . match ( value ) and is_type_ok ( item_type , datetime . date ) : <EOL> data_type = datetime . date <EOL> elif re_time . match ( value ) and is_type_ok ( item_type , datetime . time ) : <EOL> data_type = datetime . time <EOL> elif re_datetime . match ( value ) and is_type_ok ( item_type , datetime . datetime ) : <EOL> data_type = datetime . datetime <EOL> elif re_decimal . match ( value ) and is_type_ok ( item_type , decimal . Decimal ) : <EOL> data_type = decimal . Decimal <EOL> if data_type is not None : <EOL> prop = MAP_TYPES_PROPERTIES [ data_type ] ( ) <EOL> try : <EOL> value = prop . to_python ( value ) <EOL> except : <EOL> pass <EOL> elif isinstance ( value , ( list , MutableSet ) ) : <EOL> value = list_to_python ( value , item_type = item_type ) <EOL> elif isinstance ( value , dict ) : <EOL> value = dict_to_python ( value , item_type = item_type ) <EOL> return value <EOL> def list_to_python ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return [ value_to_python ( item , item_type = item_type ) for item in value ] <EOL> def dict_to_python ( value , item_type = None ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( k , value_to_python ( v , item_type = item_type ) ) for k , v in value . iteritems ( ) ] ) </s>
