<s> import copy , os , pickle <EOL> def _dict_merge ( a , b ) : <EOL> if not isinstance ( b , dict ) or not isinstance ( a , dict ) : <EOL> return b <EOL> result = copy . deepcopy ( a ) <EOL> for k , v in b . iteritems ( ) : <EOL> if k in result and isinstance ( result [ k ] , dict ) : <EOL> result [ k ] = _dict_merge ( result [ k ] , v ) <EOL> else : <EOL> result [ k ] = copy . deepcopy ( v ) <EOL> return result <EOL> def _expand_dict ( d , keylist ) : <EOL> for k in keylist [ : - <NUM_LIT:1> ] : <EOL> if k not in d or not isinstance ( d [ k ] , dict ) : <EOL> d [ k ] = { } <EOL> d = d [ k ] <EOL> return d <EOL> class Properties ( ) : <EOL> def __init__ ( self , save_path = None , ** kwargs ) : <EOL> self . _dict = kwargs . get ( '<STR_LIT>' , { } ) <EOL> self . save_path = save_path <EOL> if self . save_path : <EOL> if os . path . exists ( self . save_path ) : <EOL> with open ( self . save_path ) as f : <EOL> self . _dict = pickle . load ( f ) <EOL> def __getitem__ ( self , subkey ) : <EOL> keylist = subkey . split ( '<STR_LIT:/>' ) <EOL> d = self . _dict <EOL> for k in keylist [ : - <NUM_LIT:1> ] : <EOL> d = d [ k ] <EOL> return d . get ( keylist [ - <NUM_LIT:1> ] , { } ) <EOL> def __setitem__ ( self , subkey , value ) : <EOL> '''<STR_LIT>''' <EOL> keylist = subkey . split ( '<STR_LIT:/>' ) <EOL> d = _expand_dict ( self . _dict , keylist ) <EOL> last_key = keylist [ - <NUM_LIT:1> ] <EOL> d [ last_key ] = _dict_merge ( d . get ( last_key , { } ) , value ) <EOL> if self . save_path : <EOL> dirname = os . path . dirname ( self . save_path ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> with open ( self . save_path , '<STR_LIT:w>' ) as f : <EOL> pickle . dump ( self . _dict , f ) <EOL> def export ( self ) : <EOL> """<STR_LIT>""" <EOL> return copy . deepcopy ( self . _dict ) <EOL> def update_in ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self [ "<STR_LIT:/>" . join ( args [ : - <NUM_LIT:1> ] ) ] = args [ - <NUM_LIT:1> ] <EOL> def get_in ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self [ "<STR_LIT:/>" . join ( args ) ] </s>
