K2LL33D SHELL

 Apache/2.4.7 (Ubuntu)
 Linux sman1baleendah 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64
 uid=33(www-data) gid=33(www-data) groups=33(www-data)
 safemode : OFF
 MySQL: ON | Perl: ON | cURL: OFF | WGet: ON
  >  / usr / lib / python2.7 / dist-packages / urllib3 /
server ip : 172.67.156.115

your ip : 172.69.214.189

H O M E


Filename/usr/lib/python2.7/dist-packages/urllib3/_collections.py
Size2.83 kb
Permissionrw-r--r--
Ownerroot : root
Create time27-Apr-2025 09:56
Last modified15-Aug-2013 05:43
Last accessed06-Jul-2025 10:38
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
# urllib3/_collections.py
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

from collections import MutableMapping
from threading import RLock

try: # Python 2.7+
from collections import OrderedDict
except ImportError:
from .packages.ordered_dict import OrderedDict


__all__ = ['RecentlyUsedContainer']


_Null = object()


class RecentlyUsedContainer(MutableMapping):
"""
Provides a thread-safe dict-like container which maintains up to
``maxsize`` keys while throwing away the least-recently-used keys beyond
``maxsize``.

:param maxsize:
Maximum number of recent elements to retain.

:param dispose_func:
Every time an item is evicted from the container,
``dispose_func(value)`` is called. Callback which will get called
"""

ContainerCls = OrderedDict

def __init__(self, maxsize=10, dispose_func=None):
self._maxsize = maxsize
self.dispose_func = dispose_func

self._container = self.ContainerCls()
self.lock = RLock()

def __getitem__(self, key):
# Re-insert the item, moving it to the end of the eviction line.
with self.lock:
item = self._container.pop(key)
self._container[key] = item
return item

def __setitem__(self, key, value):
evicted_value = _Null
with self.lock:
# Possibly evict the existing value of 'key'
evicted_value = self._container.get(key, _Null)
self._container[key] = value

# If we didn't evict an existing value, we might have to evict the
# least recently used item from the beginning of the container.
if len(self._container) > self._maxsize:
_key, evicted_value = self._container.popitem(last=False)

if self.dispose_func and evicted_value is not _Null:
self.dispose_func(evicted_value)

def __delitem__(self, key):
with self.lock:
value = self._container.pop(key)

if self.dispose_func:
self.dispose_func(value)

def __len__(self):
with self.lock:
return len(self._container)

def __iter__(self):
raise NotImplementedError('Iteration over this class is unlikely to be threadsafe.')

def clear(self):
with self.lock:
# Copy pointers to all values, then wipe the mapping
# under Python 2, this copies the list of values twice :-|
values = list(self._container.values())
self._container.clear()

if self.dispose_func:
for value in values:
self.dispose_func(value)

def keys(self):
with self.lock:
return self._container.keys()