<s> from json import JSONEncoder <EOL> class DynamicJSONEncoder ( JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def default ( self , o ) : <EOL> if hasattr ( o , '<STR_LIT>' ) : <EOL> return o . __json__ ( ) <EOL> return super ( DynamicJSONEncoder , self ) . default ( o ) <EOL> try : <EOL> from sqlalchemy import inspect <EOL> from sqlalchemy . orm . state import InstanceState <EOL> except ImportError as e : <EOL> def __nomodule ( * args , ** kwargs ) : raise e <EOL> inspect = __nomodule <EOL> InstanceState = __nomodule <EOL> def get_entity_propnames ( entity ) : <EOL> """<STR_LIT>""" <EOL> ins = entity if isinstance ( entity , InstanceState ) else inspect ( entity ) <EOL> return set ( <EOL> ins . mapper . column_attrs . keys ( ) + <EOL> ins . mapper . relationships . keys ( ) <EOL> ) <EOL> def get_entity_loaded_propnames ( entity ) : <EOL> """<STR_LIT>""" <EOL> ins = inspect ( entity ) <EOL> keynames = get_entity_propnames ( ins ) <EOL> if not ins . transient : <EOL> keynames -= ins . unloaded <EOL> if ins . expired : <EOL> keynames |= ins . expired_attributes <EOL> return keynames <EOL> class JsonSerializableBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __json__ ( self , exluded_keys = set ( ) ) : <EOL> return { name : getattr ( self , name ) <EOL> for name in get_entity_loaded_propnames ( self ) - exluded_keys } </s>
