Module libnova.common.api.EnumDict

Expand source code
#!/usr/bin/env python
# coding: utf-8

from enum import Enum


class EnumDict(dict):
    def __init__(self, *args, **kwargs):
        self.update(*args, **kwargs)

    def update(self, *args, **kwargs):
        try:
            for key, value in args[0].items():
                self.__setitem__(key, value)
        except Exception:
            super().update(*args, *kwargs)

    def __setitem__(self, key, value):
        string_key = key.value if isinstance(key, Enum) else key
        super().__setitem__(string_key, value)

    def __getitem__(self, item):
        string_item = item.value if isinstance(item, Enum) else item
        return super().__getitem__(string_item)

    def get(self, key, default=None):
        string_key = key.value if isinstance(key, Enum) else key
        return super().get(string_key, default)


if __name__ == "__main__":
    print('This file cannot be executed directly!')

Classes

class EnumDict (*args, **kwargs)

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

Expand source code
class EnumDict(dict):
    def __init__(self, *args, **kwargs):
        self.update(*args, **kwargs)

    def update(self, *args, **kwargs):
        try:
            for key, value in args[0].items():
                self.__setitem__(key, value)
        except Exception:
            super().update(*args, *kwargs)

    def __setitem__(self, key, value):
        string_key = key.value if isinstance(key, Enum) else key
        super().__setitem__(string_key, value)

    def __getitem__(self, item):
        string_item = item.value if isinstance(item, Enum) else item
        return super().__getitem__(string_item)

    def get(self, key, default=None):
        string_key = key.value if isinstance(key, Enum) else key
        return super().get(string_key, default)

Ancestors

  • builtins.dict

Methods

def get(self, key, default=None)

Return the value for key if key is in the dictionary, else default.

Expand source code
def get(self, key, default=None):
    string_key = key.value if isinstance(key, Enum) else key
    return super().get(string_key, default)
def update(self, *args, **kwargs)

D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

Expand source code
def update(self, *args, **kwargs):
    try:
        for key, value in args[0].items():
            self.__setitem__(key, value)
    except Exception:
        super().update(*args, *kwargs)