<s> import random , unittest , collections <EOL> from treedict import TreeDict , getTree <EOL> import treedict <EOL> from copy import deepcopy , copy <EOL> from hashlib import md5 <EOL> import random <EOL> from treedict . treedict import _ldist <EOL> from common import * <EOL> class TestRetrieval ( unittest . TestCase ) : <EOL> def test_existance_01 ( self ) : <EOL> p = sample_tree ( ) <EOL> self . assert_ ( "<STR_LIT>" not in p ) <EOL> self . assert_ ( not p . has_key ( "<STR_LIT>" ) ) <EOL> def test_existance_02 ( self ) : <EOL> p = sample_tree ( ) <EOL> self . assert_ ( <NUM_LIT> not in p ) <EOL> self . assert_ ( not p . has_key ( <NUM_LIT> ) ) <EOL> def test_existance_03 ( self ) : <EOL> p = sample_tree ( ) <EOL> self . assert_ ( None not in p ) <EOL> self . assert_ ( not p . has_key ( None ) ) <EOL> def test_existance_04 ( self ) : <EOL> p = sample_tree ( ) <EOL> self . assert_ ( "<STR_LIT>" not in p ) <EOL> self . assert_ ( not p . has_key ( "<STR_LIT>" ) ) <EOL> def test_existance_05 ( self ) : <EOL> p = sample_tree ( ) <EOL> self . assert_ ( p not in p ) <EOL> self . assert_ ( not p . has_key ( p ) ) <EOL> def test_existance_06_dangling_node ( self ) : <EOL> p = makeTDInstance ( '<STR_LIT>' ) <EOL> p . a <EOL> self . assert_ ( '<STR_LIT:a>' not in p ) <EOL> def test_existance_06b_dangling_node ( self ) : <EOL> p = makeTDInstance ( '<STR_LIT>' ) <EOL> p . b = <NUM_LIT> <EOL> p . a <EOL> self . assert_ ( '<STR_LIT:b>' in p ) <EOL> self . assert_ ( '<STR_LIT:a>' not in p ) <EOL> def test_existance_06c_dangling_node ( self ) : <EOL> p = makeTDInstance ( '<STR_LIT>' ) <EOL> p . b = <NUM_LIT> <EOL> p . a <EOL> p . aa . b . c <EOL> p . bb . c . d = None <EOL> self . assert_ ( '<STR_LIT:a>' not in p ) <EOL> self . assert_ ( '<STR_LIT:b>' in p ) <EOL> self . assert_ ( '<STR_LIT>' not in p ) <EOL> self . assert_ ( '<STR_LIT>' in p ) <EOL> def testExistanceThroughLink ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . a . b . link = p . d <EOL> p . d . v = <NUM_LIT:1> <EOL> self . assert_ ( '<STR_LIT>' in p ) <EOL> def testContains_01 ( self ) : <EOL> p1 = makeTDInstance ( ) <EOL> p1 . a . b = <NUM_LIT> <EOL> self . assert_ ( '<STR_LIT:a>' in p1 ) <EOL> self . assert_ ( '<STR_LIT>' in p1 ) <EOL> self . assert_ ( '<STR_LIT:b>' not in p1 ) <EOL> self . assert_ ( '<STR_LIT:b>' in p1 . a ) <EOL> def testContains_02 ( self ) : <EOL> p1 = makeTDInstance ( ) <EOL> p1 . a . b = <NUM_LIT> <EOL> p1 . d <EOL> self . assert_ ( '<STR_LIT:a>' in p1 ) <EOL> self . assert_ ( '<STR_LIT>' in p1 ) <EOL> self . assert_ ( '<STR_LIT:d>' not in p1 ) <EOL> def testForwardReference ( self ) : <EOL> p = makeTDInstance ( '<STR_LIT:test>' ) <EOL> p . a . b . c = p . d . e . f <EOL> p . d . e . f . g = <NUM_LIT:10> <EOL> self . assert_ ( p . a . b . c . g == <NUM_LIT:10> ) <EOL> def testDefaultValue_01 ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . a . b = <NUM_LIT> <EOL> self . assert_ ( p . get ( '<STR_LIT>' ) == <NUM_LIT> ) <EOL> self . assert_ ( p . get ( '<STR_LIT>' , default_value = <NUM_LIT:1> ) == <NUM_LIT> ) <EOL> self . assert_ ( p . get ( '<STR_LIT>' , default_value = <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> self . assert_ ( p . get ( '<STR_LIT>' , default_value = None ) is None ) <EOL> def testRetrieve_01_NonExistantBranchFromFrozenTree ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . a . b . c = <NUM_LIT:1> <EOL> p . freeze ( ) <EOL> self . assert_ ( p . a . b . c == <NUM_LIT:1> ) <EOL> self . assertRaises ( AttributeError , lambda : p . a . d ) <EOL> def testRetrieve_02_NonExistantBranchFromFrozenTree_control ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . a . b . c = <NUM_LIT:1> <EOL> self . assert_ ( p . a . b . c == <NUM_LIT:1> ) <EOL> p . a . d <EOL> def testRetrieve_03_ThroughLink ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . a . b . link = p . d <EOL> p . d . v = <NUM_LIT:1> <EOL> self . assert_ ( p [ "<STR_LIT>" ] == <NUM_LIT:1> ) <EOL> self . assert_ ( p . get ( "<STR_LIT>" ) == <NUM_LIT:1> ) <EOL> def testRetrieve_04_ThroughMultipleLinks ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . l7 . v = <NUM_LIT:1> <EOL> p . l6 = p . l7 <EOL> p . l5 = p . l6 <EOL> p . l4 = p . l5 <EOL> p . l3 = p . l4 <EOL> p . l2 = p . l3 <EOL> p . l1 = p . l2 <EOL> self . assert_ ( p . l7 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l6 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l5 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l4 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l3 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l2 . v == <NUM_LIT:1> ) <EOL> self . assert_ ( p . l1 . v == <NUM_LIT:1> ) <EOL> def testNonexistantValues_01 ( self ) : <EOL> p = makeTDInstance ( ) <EOL> self . assertRaises ( KeyError , lambda : p [ "<STR_LIT:a>" ] ) <EOL> def testNonexistantValues_02 ( self ) : <EOL> p = makeTDInstance ( ) <EOL> self . assertRaises ( KeyError , lambda : p [ <NUM_LIT:0> ] ) <EOL> def testNonexistantValues_03 ( self ) : <EOL> p = makeTDInstance ( ) <EOL> self . assertRaises ( KeyError , lambda : p [ None ] ) <EOL> def testNonexistantValues_04 ( self ) : <EOL> p = makeTDInstance ( ) <EOL> p . freeze ( ) <EOL> self . assertRaises ( AttributeError , lambda : p . a ) <EOL> def testConvertTo_01 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . z = <NUM_LIT:3> <EOL> t . a . x = <NUM_LIT:1> <EOL> t . a . y = <NUM_LIT:2> <EOL> self . assert_ ( t . convertTo ( ) == t . convertTo ( '<STR_LIT>' ) ) <EOL> self . assert_ ( t . convertTo ( ) == { '<STR_LIT:a>' : { '<STR_LIT:x>' : <NUM_LIT:1> , '<STR_LIT:y>' : <NUM_LIT:2> } , '<STR_LIT:z>' : <NUM_LIT:3> } ) <EOL> def testConvertTo_02 ( self ) : <EOL> t = random_tree ( <NUM_LIT:200> ) <EOL> d = t . convertTo ( '<STR_LIT>' ) <EOL> t2 = TreeDict . fromdict ( d , expand_nested = True ) <EOL> self . assert_ ( t == t2 ) <EOL> def testConvertTo_03_self_linked_01 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . makeBranch ( '<STR_LIT:b>' ) <EOL> t . a . b = t . b <EOL> t . b . a = t . a <EOL> d = t . convertTo ( '<STR_LIT>' ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:a>' ] ) is dict ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] ) is dict ) <EOL> self . assert_ ( d [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] is d [ '<STR_LIT:b>' ] ) <EOL> self . assert_ ( d [ '<STR_LIT:b>' ] [ '<STR_LIT:a>' ] is d [ '<STR_LIT:a>' ] ) <EOL> def testConvertTo_03_self_linked_02 ( self ) : <EOL> t = random_selflinked_tree ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> d = t . convertTo ( '<STR_LIT>' ) <EOL> t2 = TreeDict . fromdict ( d , expand_nested = True ) <EOL> t . attach ( recursive = True ) <EOL> t2 . attach ( recursive = True ) <EOL> self . assert_ ( t == t2 ) <EOL> def testConvertTo_04_root_linked_01 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . a = t <EOL> d = t . convertTo ( '<STR_LIT>' ) <EOL> self . assert_ ( d [ '<STR_LIT:a>' ] is d ) <EOL> def testConvertTo_04_root_linked_02 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . a . b . c = t <EOL> t . a . b . x = t . a <EOL> d = t . convertTo ( '<STR_LIT>' ) <EOL> self . assert_ ( d [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] is d ) <EOL> self . assert_ ( d [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] [ '<STR_LIT:x>' ] is d [ '<STR_LIT:a>' ] ) <EOL> def testConvertTo_05_only_local_as_values_01 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . x . y = <NUM_LIT:1> <EOL> t . a . b . c = <NUM_LIT:1> <EOL> t . a . b . d = t . a . b <EOL> t . a . b . xl = t . x <EOL> t . a . xl = t . x <EOL> d = t . a . convertTo ( '<STR_LIT>' , convert_values = False ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] [ '<STR_LIT:d>' ] ) is dict ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] ) is dict ) <EOL> self . assert_ ( d [ '<STR_LIT:b>' ] is d [ '<STR_LIT:b>' ] [ '<STR_LIT:d>' ] ) <EOL> self . assert_ ( d [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] == <NUM_LIT:1> ) <EOL> self . assert_ ( isinstance ( d [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] , TreeDict ) ) <EOL> self . assert_ ( isinstance ( d [ '<STR_LIT>' ] , TreeDict ) ) <EOL> self . assert_ ( d [ '<STR_LIT>' ] is d [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] ) <EOL> def testConvertTo_05_only_local_as_values_01_control ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . x . y = <NUM_LIT:1> <EOL> t . a . b . c = <NUM_LIT:1> <EOL> t . a . b . d = t . a . b <EOL> t . a . b . xl = t . x <EOL> t . a . xl = t . x <EOL> d = t . a . convertTo ( '<STR_LIT>' , convert_values = True ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] [ '<STR_LIT:d>' ] ) is dict ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] ) is dict ) <EOL> self . assert_ ( d [ '<STR_LIT:b>' ] is d [ '<STR_LIT:b>' ] [ '<STR_LIT:d>' ] ) <EOL> self . assert_ ( d [ '<STR_LIT:b>' ] [ '<STR_LIT:c>' ] == <NUM_LIT:1> ) <EOL> self . assert_ ( type ( d [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] ) is dict ) <EOL> self . assert_ ( type ( d [ '<STR_LIT>' ] ) is dict ) <EOL> self . assert_ ( d [ '<STR_LIT>' ] is d [ '<STR_LIT:b>' ] [ '<STR_LIT>' ] ) <EOL> def testConvertTo_05_only_local_as_values_02 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . x . y = <NUM_LIT:1> <EOL> t . a . b . c = <NUM_LIT:1> <EOL> a_refs = random_node_list ( <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT:0.5> ) <EOL> x_refs = random_node_list ( <NUM_LIT:1> , <NUM_LIT:100> , <NUM_LIT:0.5> ) <EOL> for n in a_refs : <EOL> t . a [ n ] = t . a . b <EOL> for n in x_refs : <EOL> t . a [ n ] = t . x <EOL> d = t . a . convertTo ( '<STR_LIT>' , convert_values = False ) <EOL> def get_value ( d , n ) : <EOL> for n in n . split ( '<STR_LIT:.>' ) : <EOL> d = d [ n ] <EOL> return d <EOL> for n in a_refs : <EOL> self . assert_ ( type ( get_value ( d , n ) ) is dict ) <EOL> self . assert_ ( get_value ( d , n ) is d [ '<STR_LIT:b>' ] ) <EOL> for n in x_refs : <EOL> self . assert_ ( isinstance ( get_value ( d , n ) , TreeDict ) ) <EOL> self . assert_ ( get_value ( d , n ) is t . x ) <EOL> def testConvertTo_06_prune_empty_01 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . makeBranch ( '<STR_LIT:a>' ) <EOL> d = t . convertTo ( '<STR_LIT>' , prune_empty = True ) <EOL> self . assert_ ( d == { } ) <EOL> def testConvertTo_06_prune_empty_02 ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . a . x = <NUM_LIT:1> <EOL> t . a . makeBranch ( '<STR_LIT:b>' ) <EOL> d = t . convertTo ( '<STR_LIT>' , prune_empty = False ) <EOL> self . assert_ ( d == { '<STR_LIT:a>' : { '<STR_LIT:x>' : <NUM_LIT:1> , '<STR_LIT:b>' : { } } } ) <EOL> d2 = t . convertTo ( '<STR_LIT>' , prune_empty = True ) <EOL> self . assert_ ( d2 == { '<STR_LIT:a>' : { '<STR_LIT:x>' : <NUM_LIT:1> } } ) <EOL> def testConvertTo_07_lists ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . a . b = [ <NUM_LIT:1> , makeTDInstance ( x = <NUM_LIT:1> ) ] <EOL> d = t . convertTo ( '<STR_LIT>' , expand_lists = False ) <EOL> self . assert_ ( d == { '<STR_LIT:a>' : { '<STR_LIT:b>' : [ <NUM_LIT:1> , makeTDInstance ( x = <NUM_LIT:1> ) ] } } ) <EOL> d2 = t . convertTo ( '<STR_LIT>' , expand_lists = True ) <EOL> self . assert_ ( d2 == { '<STR_LIT:a>' : { '<STR_LIT:b>' : [ <NUM_LIT:1> , { '<STR_LIT:x>' : <NUM_LIT:1> } ] } } ) <EOL> def testConvertTo_08_self_referencing_lists ( self ) : <EOL> t = makeTDInstance ( ) <EOL> t . a = [ t ] <EOL> d = t . convertTo ( '<STR_LIT>' , expand_lists = False ) <EOL> self . assert_ ( d [ '<STR_LIT:a>' ] [ <NUM_LIT:0> ] is t ) <EOL> d2 = t . convertTo ( '<STR_LIT>' , expand_lists = True ) <EOL> self . assert_ ( d2 [ '<STR_LIT:a>' ] [ <NUM_LIT:0> ] is d2 ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
