Skip to content

bidict

Base classes

Name Children Inherits
bidict
bidict
The main bidirectional mapping type.

⋔ Inheritance diagram

graph TD
  1473299735968["utils.bidict"]
  1473299734016["bidict.bidict"]
  1473299749632["bidict.MutableBidict"]
  1473289094048["bidict.BidictBase"]
  1473289096000["bidict.BidirectionalMapping"]
  1473222540848["abc.Mapping"]
  1473222211728["abc.Collection"]
  1473222226368["abc.Sized"]
  140713234304496["builtins.object"]
  1473222223440["abc.Iterable"]
  1473222220512["abc.Container"]
  1473230937744["typing.Generic"]
  1473289090144["bidict.MutableBidirectionalMapping"]
  1473222543776["abc.MutableMapping"]
  1473299734016 --> 1473299735968
  1473299749632 --> 1473299734016
  1473289094048 --> 1473299749632
  1473289096000 --> 1473289094048
  1473222540848 --> 1473289096000
  1473222211728 --> 1473222540848
  1473222226368 --> 1473222211728
  140713234304496 --> 1473222226368
  1473222223440 --> 1473222211728
  140713234304496 --> 1473222223440
  1473222220512 --> 1473222211728
  140713234304496 --> 1473222220512
  1473230937744 --> 1473289096000
  140713234304496 --> 1473230937744
  1473289090144 --> 1473299749632
  1473289096000 --> 1473289090144
  1473222543776 --> 1473289090144
  1473222540848 --> 1473222543776
  1473230937744 --> 1473289090144

🛈 DocStrings

Bases: bidict[KT, VT]

Source code in prettyqt\utils\__init__.py
class bidict(bdct.bidict[KT, VT]):
    def __init__(self, *args, **kwargs):
        match args:
            case (dict(),):
                super().__init__(args[0])
            case _:
                super().__init__(kwargs)

    def __getitem__(self, item) -> VT:
        try:
            return super().__getitem__(item)
        except KeyError as e:
            raise InvalidParamError(item, list(self.keys())) from e

    def get_list(self, flag: enum.Enum) -> list[KT]:
        return [k for k, v in self.items() if v & flag]

    def get_dict(self, flag: enum.Enum) -> dict[KT, VT]:
        return {k: v & flag for k, v in self.items()}

    # def get_flag(self, **kwargs) -> dict[str, Any]:
    #     for k, v in kwargs.items():
    #     if isinstance(flag, enum.Enum):
    #         flag = flag.value
    #     return {k: v & flag for k, v in self.items()}

    def merge_flags(self, flags: Iterable[KT]) -> VT:
        return functools.reduce(operator.ior, [self[t] for t in flags])

    def get_enum_value(self, value: KT | VT) -> VT:
        return self[value] if isinstance(value, str) else value

    def get_str_value(self, value: KT | VT) -> KT:
        return value if isinstance(value, str) else self.inverse[value]