<s> from shinymud . lib . world import World <EOL> import json <EOL> world = World . get_world ( ) <EOL> def to_bool ( val ) : <EOL> """<STR_LIT>""" <EOL> bool_states = { '<STR_LIT:true>' : True , '<STR_LIT:false>' : False , '<STR_LIT:0>' : False , '<STR_LIT:1>' : True } <EOL> if not val : <EOL> return None <EOL> if isinstance ( val , bool ) : <EOL> return val <EOL> val = str ( val ) <EOL> val = val . strip ( ) . lower ( ) <EOL> return bool_states . get ( val ) <EOL> def read_dict ( val ) : <EOL> return dict ( [ thing . split ( '<STR_LIT:=>' ) for thing in val . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> def write_dict ( val ) : <EOL> return "<STR_LIT:U+002C>" . join ( '<STR_LIT:=>' . join ( [ str ( k ) , str ( v ) ] ) for k , v in val . items ( ) ) <EOL> def copy_dict ( val ) : <EOL> return dict ( val . items ( ) ) <EOL> def read_list ( val ) : <EOL> if isinstance ( val , list ) : <EOL> return val <EOL> if not val : <EOL> return [ ] <EOL> return val . split ( '<STR_LIT:U+002C>' ) <EOL> def write_list ( val ) : <EOL> if not val : <EOL> return None <EOL> return '<STR_LIT:U+002C>' . join ( map ( str , val ) ) <EOL> def copy_list ( val ) : <EOL> return val [ : ] <EOL> def read_area ( val ) : <EOL> if isinstance ( val , basestring ) : <EOL> return world . get_area ( val ) <EOL> return val <EOL> def write_area ( val ) : <EOL> if isinstance ( val , basestring ) : <EOL> return val <EOL> return val . name <EOL> def read_merchandise ( val ) : <EOL> return [ read_dict ( each ) for each in val . split ( '<STR_LIT>' ) ] <EOL> def write_merchandise ( val ) : <EOL> lst = [ ] <EOL> for dicts in val : <EOL> if dicts . get ( '<STR_LIT>' ) : <EOL> del dicts [ '<STR_LIT>' ] <EOL> lst . append ( write_dict ( dicts ) ) <EOL> return '<STR_LIT>' . join ( lst ) <EOL> def read_json ( val ) : <EOL> return json . loads ( val ) <EOL> def write_json ( val ) : <EOL> return json . dumps ( val ) <EOL> def write_model ( val ) : <EOL> if isinstance ( val , int ) : <EOL> return val <EOL> return val . dbid <EOL> def read_int_dict ( val ) : <EOL> d = { } <EOL> if val : <EOL> for a in val . split ( '<STR_LIT:U+002C>' ) : <EOL> key , val = a . split ( '<STR_LIT:=>' ) <EOL> d [ key ] = int ( val ) <EOL> return d <EOL> def write_int_dict ( val ) : <EOL> s = [ ] <EOL> if val : <EOL> for key , val in val . items ( ) : <EOL> s . append ( "<STR_LIT>" % ( str ( key ) , str ( val ) ) ) <EOL> return "<STR_LIT:U+002C>" . join ( s ) <EOL> def read_damage ( val ) : <EOL> dmg = [ ] <EOL> if val : <EOL> for d in val . split ( '<STR_LIT:|>' ) : <EOL> dmg . append ( Damage ( d ) ) <EOL> return dmg <EOL> def write_damage ( val ) : <EOL> return '<STR_LIT:|>' . join ( [ str ( d ) for d in val ] ) <EOL> def read_channels ( val ) : <EOL> d = { } <EOL> for pair in val . split ( '<STR_LIT:U+002C>' ) : <EOL> k , v = pair . split ( '<STR_LIT:=>' ) <EOL> d [ k ] = to_bool ( v ) <EOL> return d <EOL> def read_location ( val ) : <EOL> loc = val . split ( '<STR_LIT:U+002C>' ) <EOL> return world . get_location ( loc [ <NUM_LIT:0> ] , loc [ <NUM_LIT:1> ] ) <EOL> def write_location ( val ) : <EOL> if val : <EOL> return '<STR_LIT>' % ( val . area . name , val . id ) <EOL> return None <EOL> def read_int ( val ) : <EOL> try : <EOL> r = int ( val ) <EOL> except ValueError : <EOL> r = <NUM_LIT:0> <EOL> return r <EOL> def read_float ( val ) : <EOL> try : <EOL> r = float ( val ) <EOL> except ValueError : <EOL> r = <NUM_LIT:0.0> <EOL> return r </s>
