Python collections.OrderedDict 模块,__getitem__() 实例源码

我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用collections.OrderedDict.__getitem__()

项目:wbot    作者:ciknight    | 项目源码 | 文件源码
def __getitem__(self, key, with_age=False):
        """ Return the item of the dict.
        Raises a KeyError if key is not in the map.
        """
        with self.lock:
            try:
                item = OrderedDict.__getitem__(self, key)
            except KeyError:
                raise KeyError(key)

            if not self.key_time_map.get(key, None):
                return item
            item_age = (self.key_time_map[key].get('expire_time', 0)) - time.time()
            if item_age > 0:
                if with_age:
                    return item, item_age
                else:
                    return item
            else:
                del self[key]
                raise KeyError(key)
项目:wbot    作者:ciknight    | 项目源码 | 文件源码
def pop(self, key, default=None):
        """ Get item from the dict and remove it.
        Return default if expired or does not exist. Never raise KeyError.
        """
        with self.lock:
            try:
                item = OrderedDict.__getitem__(self, key)
                expire_time = self.key_time_map.get(key,{}).get('expire_time', None)
                if not expire_time:
                    del self[key]
                    del self.key_time_map[key]
                    return item

                if expire_time - time.time() > 0:
                    del self[key]
                    return item
                return default

            except KeyError:
                return default
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, item):
        if type(item) is list:
            return self.reverse[item[0]]
        else:
            return dict.__getitem__(self, item)
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, attr):
        self.lock()
        try:
            val = dict.__getitem__(self, attr)
        finally:
            self.unlock()
        return val
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, attr):
        self.lock()
        try:
            val = list.__getitem__(self, attr)
        finally:
            self.unlock()
        return val
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, key):
        kl = key.lower()
        if kl not in self.keyMap:
            raise KeyError(key)
        return OrderedDict.__getitem__(self, self.keyMap[kl])
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, item):
        if type(item) is list:
            return self.reverse[item[0]]
        else:
            return dict.__getitem__(self, item)
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, attr):
        self.lock()
        try:
            val = list.__getitem__(self, attr)
        finally:
            self.unlock()
        return val
项目:NeoAnalysis    作者:neoanalysis    | 项目源码 | 文件源码
def __getitem__(self, key):
        kl = key.lower()
        if kl not in self.keyMap:
            raise KeyError(key)
        return OrderedDict.__getitem__(self, self.keyMap[kl])
项目:ssbio    作者:SBRG    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:Sublime-uroboroSQL-formatter    作者:future-architect    | 项目源码 | 文件源码
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value
项目:pydecor    作者:mplanchard    | 项目源码 | 文件源码
def __getitem__(self, key, **kwargs):
        value = OrderedDict.__getitem__(self, key)
        del self[key]
        OrderedDict.__setitem__(self, key, value, **kwargs)
        return value
项目:pydecor    作者:mplanchard    | 项目源码 | 文件源码
def __getitem__(self, key):
        value, last_time = dict.__getitem__(self, key)
        now = time()
        if self._max_age and now - last_time > self._max_age:
            del self[key]
            raise KeyError(key)
        else:
            return value
项目:pydecor    作者:mplanchard    | 项目源码 | 文件源码
def __contains__(self, key):
        try:
            self.__getitem__(key)
        except KeyError:
            return False
        return True
项目:uroboroSQL-formatter    作者:future-architect    | 项目源码 | 文件源码
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value
项目:codenn    作者:sriniiyer    | 项目源码 | 文件源码
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value
项目:Comparative-Annotation-Toolkit    作者:ComparativeGenomicsToolkit    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:pigroom    作者:youjiajia    | 项目源码 | 文件源码
def __contains__(self, key):
        """ Return True if the dict has a key, else return False. """
        try:
            with self.lock:
                item = OrderedDict.__getitem__(self, key)
                if time.time() - item[1] < self.max_age:
                    return True
                else:
                    del self[key]
        except KeyError:
            pass
        return False
项目:pigroom    作者:youjiajia    | 项目源码 | 文件源码
def __getitem__(self, key, with_age=False):
        """ Return the item of the dict.
        Raises a KeyError if key is not in the map.
        """
        with self.lock:
            item = OrderedDict.__getitem__(self, key)
            item_age = time.time() - item[1]
            if item_age < self.max_age:
                if with_age:
                    return item[0], item_age
                else:
                    return item[0]
            else:
                del self[key]
                raise KeyError(key)
项目:pigroom    作者:youjiajia    | 项目源码 | 文件源码
def pop(self, key, default=None):
        """ Get item from the dict and remove it.
        Return default if expired or does not exist. Never raise KeyError.
        """
        with self.lock:
            try:
                item = OrderedDict.__getitem__(self, key)
                del self[key]
                return item[0]
            except KeyError:
                return default
项目:pigroom    作者:youjiajia    | 项目源码 | 文件源码
def get(self, key, default=None, with_age=False):
        " Return the value for key if key is in the dictionary, else default. "
        try:
            return self.__getitem__(key, with_age)
        except KeyError:
            if with_age:
                return default, None
            else:
                return default
项目:mappyfile    作者:geographika    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            key = key.lower()  # all keys should be lower-case to make editing easier
        except AttributeError:
            return self.__missing__(key)
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:artemis    作者:QUVA-Lab    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            self[key] = ExpandingDict()
            return dict.__getitem__(self, key)
项目:artemis    作者:QUVA-Lab    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            self[key] = ExpandingDict()
            return OrderedDict.__getitem__(self, key)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:wbot    作者:ciknight    | 项目源码 | 文件源码
def __contains__(self, key):
        """ Return True if the dict has a key, else return False. """
        try:
            with self.lock:
                item = OrderedDict.__getitem__(self, key)
                expire_time = self.key_time_map[key].get('expire_time',None)
                if expire_time and expire_time >0:
                    return True
                else:
                    del self[key]
        except KeyError:
            pass
        return False
项目:wbot    作者:ciknight    | 项目源码 | 文件源码
def set_ttl(self, key, seconds):
        is_have = OrderedDict.__getitem__(self,key)
        if is_have:
            expire_time = time.time() + seconds
            self.key_time_map[key] = {"time": time.time(), "max_age": 0, "expire_time": expire_time}
            key_ttl = expire_time - time.time()
            #self.__setitem__(key,is_have)
            #OrderedDict.__setitem__(self, key,is_have)
            if key_ttl > 0:
                return key_ttl
        return None
项目:wbot    作者:ciknight    | 项目源码 | 文件源码
def get(self, key, default=None, with_age=False):
        " Return the value for key if key is in the dictionary, else default. "
        try:
            return self.__getitem__(key, with_age)
        except KeyError:
            if with_age:
                return default, None
            else:
                return default
项目:plumeria    作者:sk89q    | 项目源码 | 文件源码
def __getitem__(self, item):
        if self.o is None:
            return self._wrap(None)
        return self._wrap(self.o.__getitem__(item))
项目:plumeria    作者:sk89q    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:plumeria    作者:sk89q    | 项目源码 | 文件源码
def __getitem__(self, key):
        return self._store[key.lower()][1]
项目:plumeria    作者:sk89q    | 项目源码 | 文件源码
def __getitem__(self, key):
        # We allow fall-through here, so values default to None

        return self.__dict__.get(key, None)
项目:PyTib    作者:Esukhia    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)
项目:Hawkeye    作者:tozhengxq    | 项目源码 | 文件源码
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value
项目:devito    作者:opesci    | 项目源码 | 文件源码
def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)