<s> from copy import copy <EOL> from xierpa3 . toolbox . parsers . c_json import cjson <EOL> from basenode import BaseNode <EOL> from xierpa3 . toolbox . transformer import TX <EOL> class State ( BaseNode ) : <EOL> u"""<STR_LIT>""" <EOL> def __init__ ( self , attributes = None , form = None , ** args ) : <EOL> u"""<STR_LIT>""" <EOL> self . _attributes = self . getNewAttributes ( attributes , ( args , form ) ) <EOL> def __getattr__ ( self , name ) : <EOL> u"""<STR_LIT>""" <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> return self . __dict__ . get ( name ) <EOL> return self [ name ] <EOL> def __setattr__ ( self , name , value ) : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> self . __dict__ [ name ] = value <EOL> self [ name ] = value <EOL> def __setitem__ ( self , name , value ) : <EOL> u"""<STR_LIT>""" <EOL> self . _attributes [ name ] = value <EOL> set = __setitem__ <EOL> def __getitem__ ( self , name ) : <EOL> u"""<STR_LIT>""" <EOL> value = self . _attributes . get ( name ) <EOL> return value <EOL> get = __getitem__ <EOL> def getState ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self . __class__ ( attributes = copy ( self . getAttributes ( ) ) ) <EOL> _copy = getState <EOL> def _getUniqueAttrsId ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return hash ( ` self . _attributes ` ) <EOL> @ classmethod <EOL> def _fromDict ( cls , d ) : <EOL> return cls ( d ) <EOL> @ classmethod <EOL> def _fromObject ( cls , o ) : <EOL> if isinstance ( o , ( list , tuple ) ) : <EOL> result = [ ] <EOL> for d in o : <EOL> result . append ( cls . _fromObject ( d ) ) <EOL> elif isinstance ( o , dict ) : <EOL> result = cls ( ) <EOL> for key , value in o . items ( ) : <EOL> result [ key ] = cls . _fromObject ( value ) <EOL> else : <EOL> result = o <EOL> return result <EOL> @ classmethod <EOL> def _asObject ( cls , o ) : <EOL> if isinstance ( o , basestring ) : <EOL> result = o <EOL> elif isinstance ( o , ( long , int , float ) ) : <EOL> result = o <EOL> elif o is None : <EOL> result = '<STR_LIT:None>' <EOL> elif isinstance ( o , cls ) : <EOL> result = { } <EOL> for name , attr in o . getAttributes ( ) . items ( ) : <EOL> result [ name ] = cls . _asObject ( attr ) <EOL> elif isinstance ( o , ( list , tuple ) ) : <EOL> result = [ ] <EOL> for oo in o : <EOL> result . append ( cls . _asObject ( oo ) ) <EOL> elif isinstance ( o , dict ) : <EOL> result = { } <EOL> for key , item in o . items ( ) : <EOL> result [ TX . asString ( key ) ] = cls . _asObject ( item ) <EOL> else : <EOL> result = ` o ` <EOL> return result <EOL> @ classmethod <EOL> def _fromJson ( cls , s ) : <EOL> u"""<STR_LIT>""" <EOL> assert isinstance ( s , basestring ) <EOL> return cls . _fromObject ( cjson . encode ( s ) ) <EOL> def _asJson ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return cjson . decode ( self . _asObject ( self ) ) </s>
