views:

165

answers:

2

My problem is:

I would like to add to a Composite class Leaf objects created at runtime inside a Composite routine like this:

def update(self, tp, msg, stt):
    """It updates composite objects
    """
    d = Leaf()
    d.setDict(tp, msg, stt)
    self.append_child(d)

    return self.status()

Inside main:

import lib.composite
c = Composite()
for i in range(0,10):
    c.update(str(i), msg, stt)

and the Composite is:

class Composite(Component):
    def __init__(self, *args, **kw):
        super(Composite, self).__init__()
        self.children = []

    def append_child(self, child):
        self.children.append(child)

    def update(self, tp, msg, stt):
        d = Leaf()
        d.setDict(tp, msg, stt)
        self.append_child(d)
        return self.status()

    def status(self):
        for child in self.children:
            ret = child.status()
            if type(child) == Leaf:
                p_out("Leaf: %s has value %s" % (child, ret))

class Component(object):
    def __init__(self, *args, **kw):
        if type(self) == Component:
            raise NotImplementedError("Component couldn't be "
                                      "instantiated directly")

    def status(self, *args, **kw):
        raise NotImplementedError("Status method "
                                  "must be implemented")

class Leaf(Component):

    def __init__(self):
        super(Leaf, self).__init__()
        self._dict  = {}

    def setDict(self, type, key, value)
        self._dict = { type : { key : value } }

    def status(self):
        return self._dict

But in this way I found always that my composite has just one leaf ("d") added even if update was called many times.

How can I code such a routine such to be able to fill composite at runtime?

A: 

What is doing the append_child? I think it should store the leafs in a list. Does it?

Update: you shouldn't pass self as first argument in the main function. I think that it raises an exception.

See code below that seems to work ok

class Component(object):
    def __init__(self, *args, **kw):
        pass

    def setDict(self, *args, **kw):
        pass

class Leaf(Component):
    def __init__(self, *args, **kw):
        Component.__init__(self, *args, **kw)

class Composite(Component):
    def __init__(self, *args, **kw):
        Component.__init__(self, *args, **kw)
        self.children = []

    def update(self, tp, msg, stt):
        """It updates composite objects
        """
        d = Leaf()
        d.setDict(tp, msg, stt)
        self.append_child(d)

        return 0

    def append_child(self, child):
        self.children.append(child)

    def remove_child(self, child):
        self.children.remove(child)

c =Composite()
for i in range(0,10):
    c.update(str(i), "", 0)
print len(c.children)
luc
yes is what it does
DrFalk3n
@Luc: corrected It was just cut and paste error
DrFalk3n
+3  A: 

"But in this way I found always that my composite has just one leaf ("d") added even if update was called many times."

No, that code makes Composite having ten children.

>>> c.children
[<__main__.Leaf object at 0xb7da77ec>, <__main__.Leaf object at 0xb7da780c>,
 <__main__.Leaf object at 0xb7da788c>, <__main__.Leaf object at 0xb7da78ac>,
 <__main__.Leaf object at 0xb7da78cc>, <__main__.Leaf object at 0xb7da792c>,
 <__main__.Leaf object at 0xb7da794c>, <__main__.Leaf object at 0xb7da798c>,
 <__main__.Leaf object at 0xb7da79ac>, <__main__.Leaf object at 0xb7da79cc>]

So why you think it only has one is strange.

Lennart Regebro
"Mumble Mumble" let me think and do some trial but I really have no idea of this strange behaviour
DrFalk3n
ok my main was inside a bad used singletonic class. Apologize!!!I suggest to remove this question
DrFalk3n
@DrFalk3n, you should be able to delete your question (using the little 'delete' link just below the tag[s]).
Alex Martelli
No I am not able "to many answers and upvote". What can I do?
DrFalk3n