[Answer]-Python: encode/decode a string in Django

1👍

Try a two-way dictonary which is a nice way to do that task. An working example looks like that:

class TwoWayDict(dict):
    def __setitem__(self, key, value):
        # Remove any previous connections with these values
        if key in self:
            del self[key]
        if value in self:
            del self[value]
        dict.__setitem__(self, key, value)
        dict.__setitem__(self, value, key)

    def __delitem__(self, key):
        dict.__delitem__(self, self[key])
        dict.__delitem__(self, key)

    def __len__(self):
        """Returns the number of connections"""
        # The int() call is for Python 3
        return int(dict.__len__(self) / 2)

d = TwoWayDict()
for i in range(10):
    d["FOOBAR-efkem4x-dnj3mn-efjn2j-lf4m2l"+str(i)] = str(i)



for i in range(10):
    print d['FOOBAR-efkem4x-dnj3mn-efjn2j-lf4m2l'+str(i)], "<->", d[str(i)]

Of course you can use any other numerical hash aswell.

Leave a comment