Esempio n. 1
0
 def test_is_container(self):
     self.assertEqual(
         all( [ is_container(e) for e in [ 
             dict(), 
             set(), 
             frozenset(),
             list(),
             VectorDict(bool, {}),
             (e for e in range(0,10))
             ]]),
      True
      )
Esempio n. 2
0
    def at(self, path, apply_here = None, copy = False):
        """
        gets to the mentioned path eventually apply a lambda on the value
        and return the node, 
        and copy it if mentioned. 

 >>> intricated = convert_tree( { 'a' : { 'a' : { 'b' : { 'c' :  1 } } } } )
 >>> pt = intricated.at( ( 'a', 'a', 'b' ) )
 >>> pt
 defaultdict(<class 'vector_dict.VectorDict.VectorDict'>, {'c': 1})
 >>> pt['c'] = 2
 >>> intricated.tprint()
 {
    a : {
        a : {
            b : {
                c : 2,
            },
        },
    },
 }
 >>> intricated.at( ( 'a', 'a', 'b' ), lambda x : x * -2  )
 defaultdict(<class 'vector_dict.VectorDict.VectorDict'>, {'c': -4})
 >>> intricated.pprint()
 a->a->b->c = -4

        """
        here = self
        if apply_here and not( is_function(apply_here) ):
            raise Exception("second argument must be a function to apply on path")
        if not len(path):
            if apply_here:
                raise Exception("cant apply a function on root of %r" % self)
            if copy:
                return  self.copy() 
            return   self
        for e in path[:-1]:
            if not hasattr(here, "__getitem__"):
                raise IndexError("this path dont exists")
            if not here.__getitem__(e):
                raise Exception("Path %r does not exists in the tree" %path ) 
            here = here.__getitem__(e)
        if not apply_here is None:
            here.__setitem__(path[-1],apply_here(here[path[-1]]))
        value = here[path[ -1 ] ]

        if copy and is_container(value):
            return here[path[-1]].copy()
        return value