Exemplo n.º 1
0
class BarCheckInfo(Mapping):
    SUMMARY = 'summary'
    DETAIL = 'detail'
    PASSED = 'passed'
    FAILED = 'failed'
    WARNING = 'warning'

    def __init__(self, detail, caveat='', pass_msg='', no_warning=''):
        self.passed = detail == pass_msg
        info = {self.SUMMARY: self.PASSED} if self.passed else {self.SUMMARY: self.FAILED, self.DETAIL: detail}

        self.warning_free = caveat == no_warning
        if not self.warning_free:
            info.update({self.WARNING: caveat})

        self._data = MappingProxyType(info)

    def __getitem__(self, key):
        return self._data[key]

    def __len__(self):
        return len(self._data)

    def __iter__(self):
        return iter(self._data)

    def __repr__(self):
        return self._data.__repr__()

    def __str__(self):
        return self._data.__str__()

    def __bool__(self):
        return self.passed and self.warning_free
Exemplo n.º 2
0
# 不可变的映射类型,d的内容可以通过MPT查看, 但是不能通过MPT修改
# dp是动态的,任何队d的修改都会反馈到dp上面

from types import MappingProxyType

d = {1: "A"}
d_proxy = MappingProxyType(d)
print(d_proxy.__repr__())
print(d_proxy[1])
# d_proxy[2] = "B"
d[2] = "b"
print(d_proxy)