• AutoDict与AutoOrderedDict


    class DotDict(dict):
        # If the attribut is not found in the usual places try the dict itself
        def __getattr__(self, key):
            if key.startswith('__'):
                return super(DotDict, self).__getattr__(key)
            return self[key]
    
    
    class AutoDict(dict):
        _closed = False
    
        def _close(self):
            self._closed = True
            for key, val in self.items():
                if isinstance(val, (AutoDict, AutoOrderedDict)):
                    val._close()
    
        def _open(self):
            self._closed = False
    
        def __missing__(self, key):
            if self._closed:
                raise KeyError
    
            value = self[key] = AutoDict()
            return value
    
        def __getattr__(self, key):
            if False and key.startswith('_'):
                raise AttributeError
    
            return self[key]
    
        def __setattr__(self, key, value):
            if False and key.startswith('_'):
                self.__dict__[key] = value
                return
    
            self[key] = value
    
    
    class AutoOrderedDict(OrderedDict):
        _closed = False
    
        def _close(self):
            self._closed = True
            for key, val in self.items():
                if isinstance(val, (AutoDict, AutoOrderedDict)):
                    val._close()
    
        def _open(self):
            self._closed = False
    
        def __missing__(self, key):
            if self._closed:
                raise KeyError
    
            # value = self[key] = type(self)()
            value = self[key] = AutoOrderedDict()
            return value
    
        def __getattr__(self, key):
            if key.startswith('_'):
                raise AttributeError
    
            return self[key]
    
        def __setattr__(self, key, value):
            if key.startswith('_'):
                self.__dict__[key] = value
                return
    
            self[key] = value
    
        # Define math operations
        def __iadd__(self, other):
            if type(self) != type(other):
                return type(other)() + other
    
            return self + other
    
        def __isub__(self, other):
            if type(self) != type(other):
                return type(other)() - other
    
            return self - other
    
        def __imul__(self, other):
            if type(self) != type(other):
                return type(other)() * other
    
            return self + other
    
        def __idiv__(self, other):
            if type(self) != type(other):
                return type(other)() // other
    
            return self + other
    
        def __itruediv__(self, other):
            if type(self) != type(other):
                return type(other)() / other
    
            return self + other
    
        def lvalues(self):
            return py3lvalues(self)
    

      

  • 相关阅读:
    【转载】超级实用且不花哨的js代码大全 -----高级应用(一)
    【 Date 对象 参考手册】
    js随机数random()方法
    【转载】js数组的操作
    【转载】js数组和json的区别
    干货----004----MySQL忘记root密码怎么办?
    PHP框架——TP_0001----ThinkPHP常用配置
    干货----003----乱码解决方法
    Python之路【第二十六篇】:xml模块
    Python之路【番外篇1】:使用Python创建照片马赛克
  • 原文地址:https://www.cnblogs.com/sidianok/p/13920189.html
Copyright © 2020-2023  润新知