synchronized lists

Nick Perkins nperkins7 at home.com
Sat Jul 21 20:31:11 EDT 2001


"Andreas Kremer" <andreas3004 at hotmail.com> wrote in message
news:9j8m49$22v$00$1 at news.t-online.com...
> Hi,
>
> i have the following design problem:
>
> in different classes i have lists which consist of elements pointing to
> instances of other classes with lists. What i need to acchieve is that if
an
> instance of one class is pointing towards another instance, the latter
needs
> a pointer to the former in its list, too, to know who is pointing at it.
The
> result would be at least two lists in each instance, one with pointers
> outwards to other instances, the other with pointer to instances which
point
> to this instance.
>
> I was thinking about a superclass of  synchronized lists, but that design
> did not look "nice". Also a connector doesnt seem to be the best idea. Has
> anybody an idea about how to acchieve this goal?
>
> Thanks in advance, Andreas.
>
>


How about keeping all such 'connections' in sort-of dictionary-like thingy,
where all mappings work both ways.  That is, if you connect A to B, then B
is automatically connected to A...

Here is a quick shot at it:


class Connections:
    def __init__(self):
        self._dict = {}

    def connect(self, A, B):
        self._connect(A,B)
        self._connect(B,A)

    def _connect(self, A, B):
        try:
            self._dict[A].append(B)
        except KeyError:
            self._dict[A] = [B]

    def disconnect(self,A,B):
        self._disconnect(A,B)
        self._disconnect(B,A)

    def _disconnect(self,A,B):
        try:
            self._dict[A].remove(B)
        except:
            pass  # ?

    def get_connections(self,A):
        try:
            return self._dict[A]
        except KeyError:
            return []



if __name__=="__main__":
    con = Connections()

    class thing:
        def __init__(self,name):
            self.name = name
        def __repr__(self):
            return "<thing %s>" %(self.name)

    A = thing('a')
    B = thing('b')
    C = thing('c')

    con.connect(A,B)
    con.connect(B,C)

    print "A connects to", con.get_connections(A)
    print "B connects to", con.get_connections(B)
    print "C connects to", con.get_connections(C)

# OUTPUT:
A connects to [<thing b>]
B connects to [<thing a>, <thing c>]
C connects to [<thing b>]

    con.disconnect(A,B)
    con.connect(C,A)

    print "A connects to", con.get_connections(A)
    print "B connects to", con.get_connections(B)
    print "C connects to", con.get_connections(C)

# OUTPUT:
A connects to [<thing c>]
B connects to [<thing c>]
C connects to [<thing b>, <thing a>]


At least this approach confines all of the 'two-way mapping' logic to a
single class, rather than having to make sure that A and B always sort it
out themselves.

I'm sure other people have done such two-way mappings before, (come to think
of it, it's basically just an undirected graph), and there is surely a
better implementation than mine..


Of course, this would require all instances which want to 'participate', to
have a reference to the same instance of class 'Connections', but that is
another problem.. ( class member, global, mix-in superclass?...hmmm)








More information about the Python-list mailing list