我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用collections.OrderedDict.__getitem__()。
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)
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
def __getitem__(self, item): if type(item) is list: return self.reverse[item[0]] else: return dict.__getitem__(self, item)
def __getitem__(self, attr): self.lock() try: val = dict.__getitem__(self, attr) finally: self.unlock() return val
def __getitem__(self, attr): self.lock() try: val = list.__getitem__(self, attr) finally: self.unlock() return val
def __getitem__(self, key): kl = key.lower() if kl not in self.keyMap: raise KeyError(key) return OrderedDict.__getitem__(self, self.keyMap[kl])
def __getitem__(self, key): try: return OrderedDict.__getitem__(self, key) except KeyError: return self.__missing__(key)
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
def __getitem__(self, key, **kwargs): value = OrderedDict.__getitem__(self, key) del self[key] OrderedDict.__setitem__(self, key, value, **kwargs) return value
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
def __contains__(self, key): try: self.__getitem__(key) except KeyError: return False return True
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
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)
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
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
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)
def __getitem__(self, key): try: return dict.__getitem__(self, key) except KeyError: self[key] = ExpandingDict() return dict.__getitem__(self, key)
def __getitem__(self, key): try: return OrderedDict.__getitem__(self, key) except KeyError: self[key] = ExpandingDict() return OrderedDict.__getitem__(self, key)
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
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
def __getitem__(self, item): if self.o is None: return self._wrap(None) return self._wrap(self.o.__getitem__(item))
def __getitem__(self, key): return self._store[key.lower()][1]
def __getitem__(self, key): # We allow fall-through here, so values default to None return self.__dict__.get(key, None)