def __init__(self, capacity=512): """ :param capacity: The Maximum number of key-value pairs can be cached. :type capacity: `int` """ super(LRUCache, self).__init__() self.capacity = capacity self.cache = OrderedDict()
class LRUCache(object): def __init__(self, capacity=512): """ :param capacity: The Maximum number of key-value pairs can be cached. :type capacity: `int` """ super(LRUCache, self).__init__() self.capacity = capacity self.cache = OrderedDict() def get(self, key): """ Look up the value in cache using the associated key. Returns the value if found. Raises :class:`KeyError` otherwise. :param key: The key used to look up the cache. :type key: `unicode` :return: The value associated with the key if exists. :raises: :class:`KeyError` if the key doesn't exist. """ value = self.cache.pop(key) self.cache[key] = value return value def set(self, key, value=None): """ Store the key-value pair to cache. :param key: The key associated with the value to be stored. It's used to look up the cache. :type key: `unicode` :param value: The value to be stored. :type value: varies """ try: self.cache.pop(key) except KeyError: if len(self.cache) >= self.capacity: self.cache.popitem(last=False) self.cache[key] = value
def expected_stream_type_params(expected_stream_type): """The stream_type-related params that we expect to pass to request methods. :rtype: :class:`OrderedDict` """ return OrderedDict(stream_type=expected_stream_type)
def __init__(self, data, files): fields = OrderedDict() for k in data: fields[k] = data[k] for k in files: fields[k] = files[k] super(MultipartStream, self).__init__(fields)
def enum_members(Enum1, Enum2_1, Enum2_2): members = OrderedDict() for enum_member_name, enum_class in zip(enum_member_names, [Enum1, Enum2_1, Enum2_2]): members[enum_member_name] = enum_class[enum_member_name] return members