-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
Closed as not planned
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Description
Documentation
Originally suggested by @picnixz's comment, recipe by me:
class BidirectionalDict(UserDict):
def __init__(self, *args, **kwargs):
self.inverse_data = {}
super().__init__(*args, **kwargs)
def __setitem__(self, key, value):
if key in self.data:
if value == (old_value := self.data[key]):
return
del self.inverse_data[old_value]
if value in self.inverse_data:
raise ValueError("Duplicate value")
self.data[key] = value
self.inverse_data[value] = key
def __delitem__(self, key):
value = self.data[key]
del self.data[key]
del self.inverse_data[value]
@property
def inverse(self):
inverse_bidict = BidirectionalDict()
inverse_bidict.data = self.inverse_data
inverse_bidict.inverse_data = self.data
return inverse_bidict
Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Projects
Status
Todo