[Python-Dev] d.get_key(key) -> key?

Barry Scott barry@barrys-emacs.org
Wed, 5 Jun 2002 23:07:10 +0100


Why not store the key as part of the value.

	d[a] = a

	d[b] => a

If you need more info in the value put a class instance or a
tuple with the key as part of the value.

	d[a] = (a,1)

		BArry


-----Original Message-----
From: python-dev-admin@python.org [mailto:python-dev-admin@python.org]On
Behalf Of Daniel 'eikeon' Krech
Sent: 05 June 2002 20:43
To: python-dev@python.org
Subject: [Python-Dev] d.get_key(key) -> key?



While attempting to "intern" the nodes in our rdflib's triple store I have
come across the following question.

Is there or could there be an efficient way to get an existing key from a
dictionary given a key that is == but whose id is not. For example:

    given a==b and id(a)!=id(b) and d[a] = 1

what is the best way to:

    d.get_key(b) -> a

--eikeon, http://eikeon.com/


PS: Here is the code where I am trying to get rid of multiple instances of
equivalent nodes:


http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/redfoot/rdflib-1.0/rdflib/sto
re/memory.py?rev=1.1.1.1&content-type=text/vnd.viewcvs-markup


and a not so efficient first attempt:

# (could use a s.get(e2)->e1 as well given e1==e2 and id(e1)!=id(e2))

class Set(object):
    def __init__(self):
        self.__set = []

    def add(self, obj):
        e = self.get(obj)
        if e: # already have equivalent element, so return the one we have
            return e
        else:
            self.__set.append(obj)
            return obj

    def get(self, obj):
        if obj in self.__set:
            for e in self.__set:
                if e==obj:
                    return e
        return None

class Intern(object):

    def __init__(self):
        super(Intern, self).__init__()
        self.__nodes = Set()

    def add(self, subject, predicate, object):
        subject = self.__nodes.add(subject)
        predicate = self.__nodes.add(predicate)
        object = self.__nodes.add(object)
        super(Intern, self).add(subject, predicate, object)




_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev