File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 22from inspect import signature , isbuiltin
33from typing import TypeVar , Callable , cast
44from warnings import warn
5+ from collections import UserDict
56
67from multimethod import multimethod , DispatchError
78
@@ -83,3 +84,35 @@ def get_arity(f: TCallable) -> int:
8384 rv = f .__code__ .co_argcount - n_defaults
8485
8586 return rv
87+
88+
89+ K = TypeVar ("K" )
90+ V = TypeVar ("V" )
91+
92+
93+ class BiDict (UserDict [K , V ]):
94+ """
95+ Bi-directional dictionary.
96+ """
97+
98+ _inv = dict [V , list [K ]]
99+
100+ def __init__ (self , * args , ** kwargs ):
101+
102+ self ._inv = {}
103+
104+ super ().__init__ (* args , ** kwargs )
105+
106+ def __setitem__ (self , k : K , v : V ):
107+
108+ super ().__setitem__ (k , v )
109+
110+ if v in self ._inv :
111+ self ._inv [v ].append (k )
112+ else :
113+ self ._inv [v ] = [k ]
114+
115+ @property
116+ def inv (self ) -> dict [V , list [K ]]:
117+
118+ return self ._inv
You can’t perform that action at this time.
0 commit comments