SPPAS 4.22

https://sppas.org/

Module sppas.src.utils

Class bidict

Description

A simple bidirectional dictionary.

Implements a 1:1 dictionary. It implies that two keys can't have the same value.

Example
 >>> relation = bidict()
 >>> relation['Alice'] = 'Bob'
 >>> print(relation['Bob'])
 >>> 'Alice'
 >>> print(relation['Alice'])
 >>> 'Bob'

Constructor

View Source
def __init__(self, *args, **kwargs):
    super(bidict, self).__init__(*args, **kwargs)
    inverse = dict()
    for key in self:
        inverse[self[key]] = key
    self.update(inverse)

Overloads

__setitem__

View Source
def __setitem__(self, key, value):
    if key in self:
        del self[key]
    super(bidict, self).__setitem__(key, value)
    super(bidict, self).__setitem__(value, key)

__delitem__

View Source
def __delitem__(self, key):
    value = self[key]
    super(bidict, self).__delitem__(key)
    super(bidict, self).__delitem__(value)