zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    Web---JSP-EL表达式
    JSP---JavaBean的使用-jsp:useBean标签相关
    Web---JSP注册技术的的演绎(3代)-JSP/EJB/Servlet/POJO/JavaBean
    Web---myAjax(自己写底层)-隐藏帧技术
    JSP---JSP中4个容器-pageContext使用
    JSP---演示ErroPage、isErroPage和jsp:forword标签
    JSP-讲解(生成java类、静态导入与动态导入)
    经典算法面试题目-替换字符串的内容(1.5)
    【Android UI】Android Layout XML属性
    【Android UI】:Fragment官方文档
  • 原文地址:https://www.cnblogs.com/sidianok/p/13920189.html
Copyright © 2011-2022 走看看