<s> import logging <EOL> import unittest <EOL> from lantz import Driver , DictFeat , Q_ <EOL> from lantz . log import get_logger <EOL> class MemHandler ( logging . Handler ) : <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> self . setFormatter ( logging . Formatter ( style = '<STR_LIT:{>' ) ) <EOL> self . history = list ( ) <EOL> def emit ( self , record ) : <EOL> self . history . append ( self . format ( record ) ) <EOL> class DictFeatTest ( unittest . TestCase ) : <EOL> def assertQuantityEqual ( self , q1 , q2 , msg = None , delta = None ) : <EOL> """<STR_LIT>""" <EOL> delta = <NUM_LIT> if delta is None else delta <EOL> msg = '<STR_LIT>' if msg is None else '<STR_LIT>' % msg <EOL> q1 = Q_ ( q1 ) <EOL> q2 = Q_ ( q2 ) <EOL> d1 = getattr ( q1 , '<STR_LIT>' , None ) <EOL> d2 = getattr ( q2 , '<STR_LIT>' , None ) <EOL> if ( d1 or d2 ) and not ( d1 == d2 ) : <EOL> raise self . failureException ( <EOL> "<STR_LIT>" % ( d1 , d2 , msg ) <EOL> ) <EOL> def test_readonly ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> obj = Spam ( ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertRaises ( AttributeError , setattr , obj , "<STR_LIT>" , <NUM_LIT:3> ) <EOL> self . assertRaises ( AttributeError , delattr , obj , "<STR_LIT>" ) <EOL> def test_writeonly ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> eggs = DictFeat ( ) <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> _eggs2 = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( None ) <EOL> def eggs2 ( self_ , key , value ) : <EOL> self_ . _eggs2 [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertRaises ( AttributeError , lambda x : obj . eggs [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> obj . eggs [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( obj . _eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertRaises ( AttributeError , delattr , obj , "<STR_LIT>" ) <EOL> self . assertRaises ( AttributeError , lambda x : obj . eggs2 [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> obj . eggs2 [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( obj . _eggs2 [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertRaises ( AttributeError , delattr , obj , "<STR_LIT>" ) <EOL> def test_readwrite ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> obj . eggs [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( obj . _eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertRaises ( AttributeError , delattr , obj , "<STR_LIT>" ) <EOL> def test_cache ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { } ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> obj . eggs [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( obj . _eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> obj . _eggs [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_logger ( self ) : <EOL> hdl = MemHandler ( ) <EOL> logger = get_logger ( '<STR_LIT>' ) <EOL> logger . addHandler ( hdl ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> x = obj . eggs [ '<STR_LIT>' ] <EOL> obj . eggs [ '<STR_LIT>' ] = x <EOL> obj . eggs [ '<STR_LIT>' ] = x + <NUM_LIT:1> <EOL> self . assertEqual ( hdl . history , [ '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def test_units ( self ) : <EOL> hdl = MemHandler ( ) <EOL> class Spam ( Driver ) : <EOL> _logger = get_logger ( '<STR_LIT>' ) <EOL> _logger . addHandler ( hdl ) <EOL> _logger . setLevel ( logging . DEBUG ) <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( units = '<STR_LIT:s>' ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertQuantityEqual ( obj . eggs [ '<STR_LIT>' ] , Q_ ( <NUM_LIT> , '<STR_LIT:s>' ) ) <EOL> obj . eggs [ '<STR_LIT>' ] = Q_ ( <NUM_LIT> , '<STR_LIT>' ) <EOL> self . assertQuantityEqual ( obj . eggs [ '<STR_LIT>' ] , Q_ ( <NUM_LIT> / <NUM_LIT:1000> , '<STR_LIT:s>' ) ) <EOL> obj . eggs [ '<STR_LIT>' ] = <NUM_LIT> <EOL> def test_keys ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( keys = ( '<STR_LIT>' , ) ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> obj . eggs [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertEqual ( obj . eggs [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , lambda x : obj . eggs [ '<STR_LIT>' ] , None ) <EOL> def test ( x ) : <EOL> x . eggs [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . assertRaises ( KeyError , test , obj ) <EOL> def test_keys_mapping ( self ) : <EOL> class Spam ( Driver ) : <EOL> _eggs = { '<STR_LIT>' : <NUM_LIT> } <EOL> @ DictFeat ( keys = { <NUM_LIT> : '<STR_LIT>' } ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> self . assertEqual ( obj . eggs [ <NUM_LIT> ] , <NUM_LIT> ) <EOL> obj . eggs [ <NUM_LIT> ] = <NUM_LIT> <EOL> self . assertEqual ( obj . eggs [ <NUM_LIT> ] , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , lambda x : obj . eggs [ '<STR_LIT>' ] , None ) <EOL> self . assertRaises ( KeyError , lambda x : obj . eggs [ '<STR_LIT>' ] , None ) <EOL> def test_of_instance ( self ) : <EOL> class Spam ( Driver ) : <EOL> def __init__ ( self_ ) : <EOL> super ( ) . __init__ ( ) <EOL> self_ . _eggs = { <NUM_LIT:1> : <NUM_LIT:9> } <EOL> @ DictFeat ( ) <EOL> def eggs ( self_ , key ) : <EOL> return self_ . _eggs [ key ] <EOL> @ eggs . setter <EOL> def eggs ( self_ , key , value ) : <EOL> self_ . _eggs [ key ] = value <EOL> obj = Spam ( ) <EOL> obj2 = Spam ( ) <EOL> self . assertEqual ( obj . eggs . instance , obj ) <EOL> self . assertEqual ( obj2 . eggs . instance , obj2 ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { } ) <EOL> self . assertEqual ( obj . eggs [ <NUM_LIT:1> ] , <NUM_LIT:9> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) , { <NUM_LIT:1> : <NUM_LIT:9> } ) <EOL> obj . eggs [ <NUM_LIT:1> ] = <NUM_LIT:10> <EOL> self . assertEqual ( obj . _eggs [ <NUM_LIT:1> ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( obj . eggs [ <NUM_LIT:1> ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) [ <NUM_LIT:1> ] , <NUM_LIT:10> ) <EOL> obj . _eggs = { <NUM_LIT:1> : <NUM_LIT:0> } <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) [ <NUM_LIT:1> ] , <NUM_LIT:10> ) <EOL> self . assertEqual ( obj . eggs [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( obj . recall ( "<STR_LIT>" ) [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( obj2 . recall ( "<STR_LIT>" ) , { } ) <EOL> def test_in_instance ( self ) : <EOL> class Spam ( Driver ) : <EOL> @ DictFeat ( keys = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , units = '<STR_LIT>' ) <EOL> def eggs ( self_ , key ) : <EOL> return <NUM_LIT:9> <EOL> x = Spam ( ) <EOL> y = Spam ( ) <EOL> self . assertEqual ( str ( x . eggs [ <NUM_LIT:1> ] . units ) , '<STR_LIT>' ) <EOL> self . assertEqual ( x . feats . eggs [ <NUM_LIT:1> ] . units , y . feats . eggs [ <NUM_LIT:1> ] . units ) <EOL> self . assertEqual ( x . eggs [ <NUM_LIT:1> ] , y . eggs [ <NUM_LIT:1> ] ) <EOL> x . feats . eggs [ <NUM_LIT:1> ] . units = '<STR_LIT:s>' <EOL> self . assertNotEqual ( x . feats . eggs [ <NUM_LIT:1> ] . units , y . feats . eggs [ <NUM_LIT:1> ] . units ) <EOL> self . assertNotEqual ( x . eggs [ <NUM_LIT:1> ] , y . eggs [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( str ( x . eggs [ <NUM_LIT:1> ] . units ) , '<STR_LIT>' ) <EOL> self . assertEqual ( str ( x . eggs [ <NUM_LIT:2> ] . units ) , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
