<s> from __future__ import absolute_import <EOL> from . base_properties import DefaultProperty <EOL> from . utils import check_type , SimpleDict <EOL> class JsonArray ( list ) : <EOL> def __init__ ( self , _obj = None , wrapper = None , type_config = None ) : <EOL> super ( JsonArray , self ) . __init__ ( ) <EOL> self . _obj = check_type ( _obj , list , <EOL> '<STR_LIT>' ) <EOL> assert type_config is not None <EOL> self . _type_config = type_config <EOL> self . _wrapper = ( <EOL> wrapper or <EOL> DefaultProperty ( type_config = self . _type_config ) <EOL> ) <EOL> for item in self . _obj : <EOL> super ( JsonArray , self ) . append ( self . _wrapper . wrap ( item ) ) <EOL> def validate ( self , required = True ) : <EOL> for obj in self : <EOL> self . _wrapper . validate ( obj , required = required ) <EOL> def append ( self , wrapped ) : <EOL> wrapped , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> self . _obj . append ( unwrapped ) <EOL> super ( JsonArray , self ) . append ( wrapped ) <EOL> def __delitem__ ( self , i ) : <EOL> super ( JsonArray , self ) . __delitem__ ( i ) <EOL> del self . _obj [ i ] <EOL> def __setitem__ ( self , i , wrapped ) : <EOL> wrapped , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> self . _obj [ i ] = unwrapped <EOL> super ( JsonArray , self ) . __setitem__ ( i , wrapped ) <EOL> def extend ( self , wrapped_list ) : <EOL> if wrapped_list : <EOL> wrapped_list , unwrapped_list = zip ( <EOL> * map ( self . _wrapper . unwrap , wrapped_list ) <EOL> ) <EOL> else : <EOL> unwrapped_list = [ ] <EOL> self . _obj . extend ( unwrapped_list ) <EOL> super ( JsonArray , self ) . extend ( wrapped_list ) <EOL> def insert ( self , index , wrapped ) : <EOL> wrapped , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> self . _obj . insert ( index , unwrapped ) <EOL> super ( JsonArray , self ) . insert ( index , wrapped ) <EOL> def remove ( self , value ) : <EOL> i = self . index ( value ) <EOL> super ( JsonArray , self ) . remove ( value ) <EOL> self . _obj . pop ( i ) <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> self . _obj . pop ( index ) <EOL> return super ( JsonArray , self ) . pop ( index ) <EOL> def sort ( self , cmp = None , key = None , reverse = False ) : <EOL> zipped = zip ( self , self . _obj ) <EOL> if key : <EOL> new_key = lambda pair : key ( pair [ <NUM_LIT:0> ] ) <EOL> zipped . sort ( key = new_key , reverse = reverse ) <EOL> elif cmp : <EOL> new_cmp = lambda pair1 , pair2 : cmp ( pair1 [ <NUM_LIT:0> ] , pair2 [ <NUM_LIT:0> ] ) <EOL> zipped . sort ( cmp = new_cmp , reverse = reverse ) <EOL> else : <EOL> zipped . sort ( reverse = reverse ) <EOL> wrapped_list , unwrapped_list = zip ( * zipped ) <EOL> while self : <EOL> self . pop ( ) <EOL> super ( JsonArray , self ) . extend ( wrapped_list ) <EOL> self . _obj . extend ( unwrapped_list ) <EOL> def reverse ( self ) : <EOL> self . _obj . reverse ( ) <EOL> super ( JsonArray , self ) . reverse ( ) <EOL> def __fix_slice ( self , i , j ) : <EOL> length = len ( self ) <EOL> if j < <NUM_LIT:0> : <EOL> j += length <EOL> if i < <NUM_LIT:0> : <EOL> i += length <EOL> if i > length : <EOL> i = length <EOL> if j > length : <EOL> j = length <EOL> return i , j <EOL> def __setslice__ ( self , i , j , sequence ) : <EOL> i , j = self . __fix_slice ( i , j ) <EOL> for _ in range ( j - i ) : <EOL> self . pop ( i ) <EOL> for k , wrapped in enumerate ( sequence ) : <EOL> self . insert ( i + k , wrapped ) <EOL> def __delslice__ ( self , i , j ) : <EOL> i , j = self . __fix_slice ( i , j ) <EOL> for _ in range ( j - i ) : <EOL> self . pop ( i ) <EOL> class JsonDict ( SimpleDict ) : <EOL> def __init__ ( self , _obj = None , wrapper = None , type_config = None ) : <EOL> super ( JsonDict , self ) . __init__ ( ) <EOL> self . _obj = check_type ( _obj , dict , '<STR_LIT>' ) <EOL> assert type_config is not None <EOL> self . _type_config = type_config <EOL> self . _wrapper = ( <EOL> wrapper or <EOL> DefaultProperty ( type_config = self . _type_config ) <EOL> ) <EOL> for key , value in self . _obj . items ( ) : <EOL> self [ key ] = self . __wrap ( key , value ) <EOL> def validate ( self , required = True ) : <EOL> for obj in self . values ( ) : <EOL> self . _wrapper . validate ( obj , required = required ) <EOL> def __wrap ( self , key , unwrapped ) : <EOL> return self . _wrapper . wrap ( unwrapped ) <EOL> def __unwrap ( self , key , wrapped ) : <EOL> return self . _wrapper . unwrap ( wrapped ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if isinstance ( key , int ) : <EOL> key = unicode ( key ) <EOL> wrapped , unwrapped = self . __unwrap ( key , value ) <EOL> self . _obj [ key ] = unwrapped <EOL> super ( JsonDict , self ) . __setitem__ ( key , wrapped ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . _obj [ key ] <EOL> super ( JsonDict , self ) . __delitem__ ( key ) <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , int ) : <EOL> key = unicode ( key ) <EOL> return super ( JsonDict , self ) . __getitem__ ( key ) <EOL> class JsonSet ( set ) : <EOL> def __init__ ( self , _obj = None , wrapper = None , type_config = None ) : <EOL> super ( JsonSet , self ) . __init__ ( ) <EOL> if isinstance ( _obj , set ) : <EOL> _obj = list ( _obj ) <EOL> self . _obj = check_type ( _obj , list , '<STR_LIT>' ) <EOL> assert type_config is not None <EOL> self . _type_config = type_config <EOL> self . _wrapper = ( <EOL> wrapper or <EOL> DefaultProperty ( type_config = self . _type_config ) <EOL> ) <EOL> for item in self . _obj : <EOL> super ( JsonSet , self ) . add ( self . _wrapper . wrap ( item ) ) <EOL> def validate ( self , required = True ) : <EOL> for obj in self : <EOL> self . _wrapper . validate ( obj , required = required ) <EOL> def add ( self , wrapped ) : <EOL> wrapped , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> if wrapped not in self : <EOL> self . _obj . append ( unwrapped ) <EOL> super ( JsonSet , self ) . add ( wrapped ) <EOL> def remove ( self , wrapped ) : <EOL> wrapped , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> if wrapped in self : <EOL> self . _obj . remove ( unwrapped ) <EOL> super ( JsonSet , self ) . remove ( wrapped ) <EOL> else : <EOL> raise KeyError ( wrapped ) <EOL> def discard ( self , wrapped ) : <EOL> try : <EOL> self . remove ( wrapped ) <EOL> except KeyError : <EOL> pass <EOL> def pop ( self ) : <EOL> for wrapped in self : <EOL> break <EOL> else : <EOL> raise KeyError ( ) <EOL> wrapped_ , unwrapped = self . _wrapper . unwrap ( wrapped ) <EOL> assert wrapped is wrapped_ <EOL> self . remove ( unwrapped ) <EOL> return wrapped <EOL> def clear ( self ) : <EOL> while self : <EOL> self . pop ( ) <EOL> def __ior__ ( self , other ) : <EOL> for wrapped in other : <EOL> self . add ( wrapped ) <EOL> return self <EOL> def update ( self , * args ) : <EOL> for wrapped_list in args : <EOL> self |= set ( wrapped_list ) <EOL> union_update = update <EOL> def __iand__ ( self , other ) : <EOL> for wrapped in list ( self ) : <EOL> if wrapped not in other : <EOL> self . remove ( wrapped ) <EOL> return self <EOL> def intersection_update ( self , * args ) : <EOL> for wrapped_list in args : <EOL> self &= set ( wrapped_list ) <EOL> def __isub__ ( self , other ) : <EOL> for wrapped in list ( self ) : <EOL> if wrapped in other : <EOL> self . remove ( wrapped ) <EOL> return self <EOL> def difference_update ( self , * args ) : <EOL> for wrapped_list in args : <EOL> self -= set ( wrapped_list ) <EOL> def __ixor__ ( self , other ) : <EOL> removed = set ( ) <EOL> for wrapped in list ( self ) : <EOL> if wrapped in other : <EOL> self . remove ( wrapped ) <EOL> removed . add ( wrapped ) <EOL> self . update ( other - removed ) <EOL> return self <EOL> def symmetric_difference_update ( self , * args ) : <EOL> for wrapped_list in args : <EOL> self ^= set ( wrapped_list ) </s>
