<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> __author__ = "<STR_LIT>" + "<STR_LIT>" <EOL> __revision__ = "<STR_LIT>" [ <NUM_LIT:11> : - <NUM_LIT:2> ] <EOL> import types <EOL> from types import StringType , InstanceType , ClassType , TypeType <EOL> from pprint import pformat <EOL> import inspect <EOL> _INCLUDE_NAMESPACE_REPR_IN_NOTFOUND_EXCEPTIONS = False <EOL> _ALLOW_WRAPPING_OF_NOTFOUND_EXCEPTIONS = True <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class NotFound ( LookupError ) : <EOL> pass <EOL> def _raiseNotFoundException ( key , namespace ) : <EOL> excString = "<STR_LIT>" % key <EOL> if _INCLUDE_NAMESPACE_REPR_IN_NOTFOUND_EXCEPTIONS : <EOL> excString += '<STR_LIT>' % pformat ( namespace ) <EOL> raise NotFound ( excString ) <EOL> def _wrapNotFoundException ( exc , fullName , namespace ) : <EOL> if not _ALLOW_WRAPPING_OF_NOTFOUND_EXCEPTIONS : <EOL> raise <EOL> else : <EOL> excStr = exc . args [ <NUM_LIT:0> ] <EOL> if excStr . find ( '<STR_LIT>' ) == - <NUM_LIT:1> : <EOL> excStr += "<STR_LIT>" % fullName <EOL> if _INCLUDE_NAMESPACE_REPR_IN_NOTFOUND_EXCEPTIONS : <EOL> excString += '<STR_LIT>' % pformat ( namespace ) <EOL> exc . args = ( excStr , ) <EOL> raise <EOL> def hasKey ( obj , key ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( obj , '<STR_LIT>' ) and obj . has_key ( key ) : <EOL> return True <EOL> elif hasattr ( obj , key ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def valueForKey ( obj , key ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) and obj . has_key ( key ) : <EOL> return obj [ key ] <EOL> elif hasattr ( obj , key ) : <EOL> return getattr ( obj , key ) <EOL> else : <EOL> _raiseNotFoundException ( key , obj ) <EOL> def _valueForName ( obj , name , executeCallables = False ) : <EOL> nameChunks = name . split ( '<STR_LIT:.>' ) <EOL> for i in range ( len ( nameChunks ) ) : <EOL> key = nameChunks [ i ] <EOL> if hasattr ( obj , key ) : <EOL> nextObj = getattr ( obj , key ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) and obj . has_key ( key ) : <EOL> nextObj = obj [ key ] <EOL> else : <EOL> _raiseNotFoundException ( key , obj ) <EOL> if ( executeCallables and callable ( nextObj ) <EOL> and ( type ( nextObj ) not in ( InstanceType , ClassType ) ) ) : <EOL> obj = nextObj ( ) <EOL> else : <EOL> obj = nextObj <EOL> return obj <EOL> def valueForName ( obj , name , executeCallables = False ) : <EOL> try : <EOL> return _valueForName ( obj , name , executeCallables ) <EOL> except NotFound , e : <EOL> _wrapNotFoundException ( e , fullName = name , namespace = obj ) <EOL> def valueFromSearchList ( searchList , name , executeCallables = False ) : <EOL> key = name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> for namespace in searchList : <EOL> if hasKey ( namespace , key ) : <EOL> return _valueForName ( namespace , name , <EOL> executeCallables = executeCallables ) <EOL> _raiseNotFoundException ( key , searchList ) <EOL> def _namespaces ( callerFrame , searchList = None ) : <EOL> yield callerFrame . f_locals <EOL> if searchList : <EOL> for namespace in searchList : <EOL> yield namespace <EOL> yield callerFrame . f_globals <EOL> yield __builtins__ <EOL> def valueFromFrameOrSearchList ( searchList , name , executeCallables = False , <EOL> frame = None ) : <EOL> def __valueForName ( ) : <EOL> try : <EOL> return _valueForName ( namespace , name , executeCallables = executeCallables ) <EOL> except NotFound , e : <EOL> _wrapNotFoundException ( e , fullName = name , namespace = searchList ) <EOL> try : <EOL> if not frame : <EOL> frame = inspect . stack ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> key = name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> for namespace in _namespaces ( frame , searchList ) : <EOL> if hasKey ( namespace , key ) : return __valueForName ( ) <EOL> _raiseNotFoundException ( key , searchList ) <EOL> finally : <EOL> del frame <EOL> def valueFromFrame ( name , executeCallables = False , frame = None ) : <EOL> try : <EOL> if not frame : <EOL> frame = inspect . stack ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> return valueFromFrameOrSearchList ( searchList = None , <EOL> name = name , <EOL> executeCallables = executeCallables , <EOL> frame = frame ) <EOL> finally : <EOL> del frame <EOL> def hasName ( obj , name ) : <EOL> """<STR_LIT>""" <EOL> key = name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if not hasKey ( obj , key ) : <EOL> return False <EOL> try : <EOL> valueForName ( obj , name ) <EOL> return True <EOL> except NotFound : <EOL> return False <EOL> try : <EOL> from _namemapper import NotFound , valueForKey , valueForName , valueFromSearchList , valueFromFrameOrSearchList , valueFromFrame <EOL> C_VERSION = True <EOL> except : <EOL> C_VERSION = False <EOL> class Mixin : <EOL> """<STR_LIT>""" <EOL> def valueForName ( self , name ) : <EOL> return valueForName ( self , name ) <EOL> def valueForKey ( self , key ) : <EOL> return valueForKey ( self , key ) <EOL> def example ( ) : <EOL> class A ( Mixin ) : <EOL> classVar = '<STR_LIT>' <EOL> def method ( self , arg = '<STR_LIT>' ) : <EOL> return arg <EOL> def method2 ( self , arg = '<STR_LIT>' ) : <EOL> return { '<STR_LIT>' : arg } <EOL> def method3 ( self , arg = '<STR_LIT>' ) : <EOL> return arg <EOL> class B ( A ) : <EOL> classBvar = '<STR_LIT>' <EOL> a = A ( ) <EOL> a . one = '<STR_LIT>' <EOL> def function ( whichOne = '<STR_LIT:default>' ) : <EOL> values = { <EOL> '<STR_LIT:default>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return values [ whichOne ] <EOL> a . dic = { <EOL> '<STR_LIT>' : function , <EOL> '<STR_LIT>' : a . method3 , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : a . method3 } <EOL> } <EOL> b = '<STR_LIT>' <EOL> print valueForKey ( a . dic , '<STR_LIT>' ) <EOL> print valueForName ( a , '<STR_LIT>' ) <EOL> print valueForName ( vars ( ) , '<STR_LIT:b>' ) <EOL> print valueForName ( __builtins__ , '<STR_LIT>' ) ( ) <EOL> print valueForName ( vars ( ) , '<STR_LIT>' ) <EOL> print valueForName ( vars ( ) , '<STR_LIT>' , executeCallables = True ) <EOL> print valueForName ( vars ( ) , '<STR_LIT>' , executeCallables = True ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> example ( ) </s>
