<s> '''<STR_LIT>''' <EOL> import nose <EOL> from python_toolbox import context_management <EOL> from python_toolbox import misc_tools <EOL> from python_toolbox . caching import cache , CachedType , CachedProperty <EOL> @ misc_tools . set_attributes ( i = <NUM_LIT:0> ) <EOL> def counting_func ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return counting_func . i <EOL> finally : <EOL> counting_func . i += <NUM_LIT:1> <EOL> def test ( ) : <EOL> '''<STR_LIT>''' <EOL> class A : <EOL> personality = CachedProperty ( counting_func ) <EOL> assert isinstance ( A . personality , CachedProperty ) <EOL> a1 = A ( ) <EOL> assert a1 . personality == a1 . personality == a1 . personality <EOL> a2 = A ( ) <EOL> assert a2 . personality == a2 . personality == a2 . personality <EOL> assert a2 . personality == a1 . personality + <NUM_LIT:1> <EOL> def test_inheritance ( ) : <EOL> class A : <EOL> personality = CachedProperty ( counting_func ) <EOL> class B ( A ) : <EOL> pass <EOL> assert isinstance ( B . personality , CachedProperty ) <EOL> b1 = B ( ) <EOL> assert b1 . personality == b1 . personality == b1 . personality <EOL> b2 = B ( ) <EOL> assert b2 . personality == b2 . personality == b2 . personality <EOL> assert b2 . personality == b1 . personality + <NUM_LIT:1> <EOL> def test_value ( ) : <EOL> '''<STR_LIT>''' <EOL> class B : <EOL> brrr_property = CachedProperty ( '<STR_LIT>' ) <EOL> assert isinstance ( B . brrr_property , CachedProperty ) <EOL> b1 = B ( ) <EOL> assert b1 . brrr_property == '<STR_LIT>' <EOL> b2 = B ( ) <EOL> assert b2 . brrr_property == '<STR_LIT>' <EOL> def test_as_decorator ( ) : <EOL> '''<STR_LIT>''' <EOL> class B : <EOL> @ CachedProperty <EOL> def personality ( self ) : <EOL> if not hasattr ( B . personality , '<STR_LIT:i>' ) : <EOL> B . personality . i = <NUM_LIT:0> <EOL> try : <EOL> return B . personality . i <EOL> finally : <EOL> B . personality . i = ( B . personality . i + <NUM_LIT:1> ) <EOL> assert isinstance ( B . personality , CachedProperty ) <EOL> b1 = B ( ) <EOL> assert b1 . personality == b1 . personality == b1 . personality <EOL> b2 = B ( ) <EOL> assert b2 . personality == b2 . personality == b2 . personality <EOL> assert b2 . personality == b1 . personality + <NUM_LIT:1> <EOL> def test_with_name ( ) : <EOL> '''<STR_LIT>''' <EOL> class A : <EOL> personality = CachedProperty ( counting_func , name = '<STR_LIT>' ) <EOL> a1 = A ( ) <EOL> assert a1 . personality == a1 . personality == a1 . personality <EOL> a2 = A ( ) <EOL> assert a2 . personality == a2 . personality == a2 . personality <EOL> assert a2 . personality == a1 . personality + <NUM_LIT:1> <EOL> def test_with_wrong_name ( ) : <EOL> '''<STR_LIT>''' <EOL> class A : <EOL> personality = CachedProperty ( counting_func , name = '<STR_LIT>' ) <EOL> a1 = A ( ) <EOL> assert a1 . personality == a1 . meow == a1 . personality - <NUM_LIT:1> == a1 . personality - <NUM_LIT:2> <EOL> a2 = A ( ) <EOL> assert a2 . personality == a2 . meow == a2 . personality - <NUM_LIT:1> == a2 . personality - <NUM_LIT:2> <EOL> def test_on_false_object ( ) : <EOL> '''<STR_LIT>''' <EOL> class C : <EOL> @ CachedProperty <EOL> def personality ( self ) : <EOL> if not hasattr ( C . personality , '<STR_LIT:i>' ) : <EOL> C . personality . i = <NUM_LIT:0> <EOL> try : <EOL> return C . personality . i <EOL> finally : <EOL> C . personality . i = ( C . personality . i + <NUM_LIT:1> ) <EOL> def __bool__ ( self ) : <EOL> return False <EOL> __nonzero__ = __bool__ <EOL> assert isinstance ( C . personality , CachedProperty ) <EOL> c1 = C ( ) <EOL> assert not c1 <EOL> assert c1 . personality == c1 . personality == c1 . personality <EOL> c2 = C ( ) <EOL> assert not c2 <EOL> assert c2 . personality == c2 . personality == c2 . personality <EOL> assert c2 . personality == c1 . personality + <NUM_LIT:1> <EOL> def test_doc ( ) : <EOL> '''<STR_LIT>''' <EOL> class A : <EOL> personality = CachedProperty ( counting_func ) <EOL> assert A . personality . __doc__ == '<STR_LIT>' <EOL> class B : <EOL> personality = CachedProperty ( <EOL> counting_func , <EOL> doc = '''<STR_LIT>''' <EOL> ) <EOL> assert B . personality . __doc__ == '<STR_LIT>' <EOL> class C : <EOL> undocced_property = CachedProperty ( <EOL> lambda self : <NUM_LIT:1> / <NUM_LIT:0> , <EOL> ) <EOL> assert C . undocced_property . __doc__ is None <EOL> def test_decorating ( ) : <EOL> '''<STR_LIT>''' <EOL> class A : <EOL> reentrant_context_manager = CachedProperty ( <EOL> lambda self : context_management . ReentrantContextManager ( ) <EOL> ) <EOL> @ reentrant_context_manager <EOL> def my_method ( self , x , y = <NUM_LIT:3> ) : <EOL> return ( x , y , self . reentrant_context_manager . depth ) <EOL> a = A ( ) <EOL> assert a . my_method ( <NUM_LIT:2> ) == ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> with a . reentrant_context_manager : <EOL> assert a . my_method ( y = <NUM_LIT:7> , x = <NUM_LIT:8> ) == ( <NUM_LIT:8> , <NUM_LIT:7> , <NUM_LIT:2> ) <EOL> with a . reentrant_context_manager : <EOL> assert a . my_method ( y = <NUM_LIT:7> , x = <NUM_LIT:8> ) == ( <NUM_LIT:8> , <NUM_LIT:7> , <NUM_LIT:3> ) <EOL> def test_force_value_not_getter ( ) : <EOL> class A : <EOL> personality = CachedProperty ( counting_func , <EOL> force_value_not_getter = True ) <EOL> a = A ( ) <EOL> assert a . personality == counting_func == a . personality == counting_func </s>
