class LRUCache(object):
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        """
        :rtype: int
        """
        if self.cache.has_key(key):
            val = self.cache[key]
            del self.cache[key]
            self.cache[key] = val
            return val
        return -1

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        if self.cache.has_key(key):
            del self.cache[key]
            self.cache[key] = value
        else:
            if len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)
                self.cache[key] = value
            else:
                self.cache[key] = value
Example #2
1
class RingCache(object):
    def __init__(self, maxEntries=100, isAutoAdd=False):
        self.max = maxEntries
        self.d = OrderedDict()
        self.isAutoAdd = isAutoAdd

    def add(self, k, v=None):
        if self.max <= len(self.d):
            self.d.popitem(last=False)
        if k in self.d:
            del (self.d[k])
        self.d[k] = v

    def get(self, k):
        return self.d[k]

    def remove(self, k):
        del (self.d[k])

    def __contains__(self, k):
        if k in self.d:
            v = self.d[k]
            del (self.d[k])
            self.d[k] = v
            return True
        else:
            if self.isAutoAdd:
                self.add(k)
            return False

    def __len__(self):
        return len(self.d)

    def __repr__(self):
        return self.d.__repr__()
Example #3
1
def convert_dn_to_cobra(dn):
    # Taken from https://gist.github.com/mtimm/7c238d1c682a684867fd
    from cobra.mit.naming import Dn

    imports = []
    cobra_dn = Dn.fromString(dn)
    parentMoOrDn = "''"
    dn_dict = OrderedDict()
    for rn in cobra_dn.rns:
        rn_str = str(rn)
        dn_dict[rn_str] = {}
        dn_dict[rn_str]["namingVals"] = tuple(rn.namingVals)
        dn_dict[rn_str]["moClassName"] = rn.meta.moClassName
        dn_dict[rn_str]["className"] = rn.meta.className
        dn_dict[rn_str]["parentMoOrDn"] = parentMoOrDn
        parentMoOrDn = rn.meta.moClassName
    code = []

    dn_dict.popitem()

    for arn in dn_dict.items():
        if len(arn[1]["namingVals"]) > 0:
            nvals_str = ", " + ", ".join(map(lambda x: "'{0}'".format(str(x)), arn[1]["namingVals"]))
        else:
            nvals_str = ""
        code.append(
            "{0} = {1}({2}{3})".format(arn[1]["moClassName"], arn[1]["className"], arn[1]["parentMoOrDn"], nvals_str)
        )
        package = ".".join(arn[1]["className"].split(".")[0:-1])
        imports.append(package)
    return arn[1]["moClassName"], imports, "\n".join(code)
Example #4
1
class AliasGenerator(object):
    def __init__(self, size=200):
        self.MAX_CACHE = size  # TODO: Make configurable
        self.cache = OrderedDict()

    def generate_alias(self, str_value):
        """
        Generate an alias for the given string.
        :param str_value: the given string
        :return: a 24 character alias string
        """
        alias = self.get_alias(str_value)
        if not alias:
            h = hashlib.md5()
            h.update(str_value.encode("utf-8"))
            alias = h.hexdigest()
            self.cache[str_value] = alias
            if len(self.cache) > self.MAX_CACHE:
                self.cache.popitem(last=False)  # FIFO
        else:
            alias = alias
        return alias

    def needs_alias(self, str_value):
        return len(str_value) > MAX_NAME_LEN or len(RE_CLEANSE.findall(str_value)) > 0

    def get_alias(self, str_value):
        return self.cache.get(str_value)

    def clear_aliases(self):
        self.cache.clear()
Example #5
1
class LRUCache:

    # @param capacity, an integer
    def __init__(self, capacity):
        self.capacity = capacity
        self.history = OrderedDict()

    # @return an integer
    def get(self, key):
        if key in self.history:
            self.set(key, self.history[key])
            return self.history[key]
        else:
            return -1

    # @param key, an integer
    # @param value, an integer
    # @return nothing
    def set(self, key, value):
        if key in self.history:
            del self.history[key]
            self.history[key] = value
        else:
            if len(self.history) == self.capacity:
                self.history.popitem(False)
            self.history[key] = value
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        Two Intervals k & t:
        * Use OrderedDict to remember the index and n/t to shrink the interval to -1, 0, 1.
        * In terms of index difference, since there are i-k and i+k, when scanning from left to right, only consider i-k.

        Alternative algorithms:
        Window + TreeSet (Java) / SortedSet (C++)  # TODO

        :type nums: list[int]
        :rtype: bool
        """
        if k < 1 or t < 0:
            return False

        if t == 0:
            return self.containsNearByDuplicate(nums, k)

        od = OrderedDict()  # keep the window
        for n in nums:
            key = n / t
            for j in (-1, 0, 1):  # (n-t, n, n+t), shrink the interval
                m = od.get(key + j)
                if m is not None and abs(m - n) <= t:  # need to recheck, consider case {1, 7}, t=4
                    return True

            while len(od) >= k:
                od.popitem(False)  # not last, i.e. the first

            od[key] = n

        return False
Example #7
1
class Cache(object):
    def __init__(self):
        self.reset_caches()

    def reset_caches(self):
        self._category_cache = OrderedDict()
        self._search_cache = OrderedDict()

    def search_cache(self, search):
        old = self._search_cache.pop(search, None)
        if old is None or old[0] <= self.db.last_modified():
            matches = self.search_for_books(search) or []
            self._search_cache[search] = (utcnow(), frozenset(matches))
            if len(self._search_cache) > 50:
                self._search_cache.popitem(last=False)
        else:
            self._search_cache[search] = old
        return self._search_cache[search][1]

    def categories_cache(self, restrict_to=frozenset([])):
        base_restriction = self.search_cache("")
        if restrict_to:
            restrict_to = frozenset(restrict_to).intersection(base_restriction)
        else:
            restrict_to = base_restriction
        old = self._category_cache.pop(frozenset(restrict_to), None)
        if old is None or old[0] <= self.db.last_modified():
            categories = self.db.get_categories(ids=restrict_to)
            self._category_cache[restrict_to] = (utcnow(), categories)
            if len(self._category_cache) > 20:
                self._category_cache.popitem(last=False)
        else:
            self._category_cache[frozenset(restrict_to)] = old
        return self._category_cache[restrict_to][1]
Example #8
1
class LRUCache(OrderedDict):
    """不能存储可变类型对象,不能并发访问set()"""

    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        if self.cache.has_key(key):
            value = self.cache.pop(key)
            self.cache[key] = value
        else:
            value = None

        return value

    def set(self, key, value):
        if self.cache.has_key(key):
            value = self.cache.pop(key)
            self.cache[key] = value
        else:
            if len(self.cache) == self.capacity:
                self.cache.popitem(last=False)  # pop出第一个item
                self.cache[key] = value
            else:
                self.cache[key] = value
Example #9
1
def get_treasury_source():
    url = """\
http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData\
"""
    res = requests.get(url, stream=True)
    stream = iter_to_stream(res.text.splitlines())

    elements = ET.iterparse(stream, ("end", "start-ns", "end-ns"))

    namespaces = OrderedDict()
    properties_xpath = [""]

    def updated_namespaces():
        if "" in namespaces and "m" in namespaces:
            properties_xpath[0] = "{%s}content/{%s}properties" % (namespaces[""], namespaces["m"])
        else:
            properties_xpath[0] = ""

    for event, element in elements:
        if event == "end":
            tag = get_localname(element)
            if tag == "entry":
                properties = element.find(properties_xpath[0])
                datum = {get_localname(node): node.text for node in properties if ET.iselement(node)}
                # clear the element after we've dealt with it:
                element.clear()
                yield datum

        elif event == "start-ns":
            namespaces[element[0]] = element[1]
            updated_namespaces()

        elif event == "end-ns":
            namespaces.popitem()
            updated_namespaces()
Example #10
1
class Cache(object):
    def __init__(self, size_limit):
        super(Cache, self).__init__()
        self._c = OrderedDict()
        self._size_limit = size_limit

    def insert(self, k, v):
        """
        add a key and value to the front
        """
        self._c[k] = v
        if len(self._c) > self._size_limit:
            self._c.popitem(last=False)

    def exists(self, k):
        return k in self._c

    def touch(self, k):
        """
        bring a key to the front
        """
        v = self._c[k]
        del self._c[k]
        self._c[k] = v

    def get(self, k):
        return self._c[k]
Example #11
0
class SIMCache(object):
    def __init__(self, size, ttl):
        self.cache = OrderedDict()
        self.size = size
        self.ttl = ttl

    def set_key(self, key, value, ttl=None):
        """Set a key value in the cache with its expiration time.
        If no ttl (in seconds) is provided self.ttl is taken by default.
        If cache length exceeds CACHE_SIZE when adding a key, the oldest (first inserted) key is removed (FIFO)
        """
        self.cache[key] = (time.time() + (ttl or self.ttl), value)
        if len(self.cache) > self.size:
            self.cache.popitem(last=False)

    def get_key(self, key):
        """Retrieve a key value from the cache.
        Returns None if does not exist or the key expired.
        If the key expired it is removed from the cache.
        """
        content = self.cache.get(key, None)
        if content:
            if content[0] > time.time():
                return content[1]
            else:
                del self.cache[key]
        return None

    def clear_keys(self,):
        """Remove all cache keys content
        """
        self.cache = OrderedDict()
Example #12
0
class _MultiCache(object):
    """
    A utility class for caching items in a of a dict-of-dicts
    """

    def __init__(self, first_uid, default_factory=lambda: None, maxcaches=None):
        self._maxcaches = maxcaches
        self.caches = OrderedDict()
        self.add(first_uid, default_factory=default_factory)

    def add(self, uid, default_factory=lambda: None):
        assert isinstance(uid[1], tuple)
        if uid not in self.caches:
            cache = defaultdict(default_factory)
            self.caches[uid] = cache
        else:
            raise Exception("MultiCache.add: uid %s is already in use" % str(uid))

        # remove oldest cache, if necessary
        old_uid = None
        if self._maxcaches and len(self.caches) > self._maxcaches:
            old_uid, v = self.caches.popitem(False)  # removes item in FIFO order
        return old_uid

    def touch(self, uid):
        c = self.caches[uid]
        del self.caches[uid]
        self.caches[uid] = c

    def set_maxcaches(self, newmax):
        self._maxcaches = newmax
        while len(self.caches) > self._maxcaches:
            old_uid, v = self.caches.popitem(False)  # removes item in FIFO order
Example #13
0
class LRUCache(object):
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        """
        :rtype: int
        """
        try:
            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        except KeyError:
            return -1

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        try:
            self.cache.pop(key)
        except KeyError:
            if len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)
        self.cache[key] = value
Example #14
0
class LRUCache:
    # @param capacity, an integer
    def __init__(self, capacity):
        self.capacity = capacity
        # use OrderedDict to simulate LRU cache
        self.cache = OrderedDict()

    def get(self, key):
        if key in self.cache:
            value = self.cache[key]
            # remove key then add it again
            del self.cache[key]
            self.cache[key] = value
            return value
        else:
            return ""

    def set(self, key, value):
        if key in self.cache:
            # remove key if already in cache
            del self.cache[key]
        self.cache[key] = value
        # pop an item (oldest) if cache is full
        if len(self.cache) > self.capacity:
            self.cache.popitem(False)
Example #15
0
class LRUCache:

    # @param capacity, an integer
    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()

    # @return an integer
    def get(self, key):
        if key in self.cache:
            value = self.cache[key]
            del self.cache[key]
            self.cache[key] = value
            return value
        return -1

    # @param key, an integer
    # @param value, an integer
    # @return nothing
    def set(self, key, value):
        if key in self.cache:
            del self.cache[key]
        elif len(self.cache) >= self.capacity:
            self.cache.popitem(False)
        self.cache[key] = value
Example #16
0
class LRUCache(object):

    """ Simple LRU cache for template instance caching.

        in fact, the OrderedDict in collections module or
        @functools.lru_cache is working well too.

    """

    def __init__(self, capacity):
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        """ Return -1 if catched KeyError exception.

        """
        try:
            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        except KeyError:
            return -1

    def set(self, key, value):
        try:
            self.cache.pop(key)
        except KeyError:
            if len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)
        self.cache[key] = value
Example #17
0
class LRUCache(object):
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.capacity = capacity
        self.cache = OrderedDict()

    def get(self, key):
        """
        :rtype: int
        """
        if key in self.cache:
            val = self.cache.pop(key)
            self.cache[key] = val
            return val
        else:
            return -1

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        if key not in self.cache:
            if self.capacity > 0:
                self.capacity -= 1
            else:
                self.cache.popitem(last=False)
        else:
            self.cache.pop(key)
        self.cache[key] = value
Example #18
0
class Density(object):
    def __init__(self, params=None):
        self.params = params

        self.cache = OrderedDict()

        self.max_cache_size = 10000

    def log_p(self, data, params):
        """
        Args:
            data : (nametuple) Data for density.

            params : (nametuple) Parameters in density.

        Kwargs:
            global_params: (namedtuple) Parameters which are shared across all atoms. If this is None it will use the
                                        current value.
        """
        key = (data, params, self.params)

        if key not in self.cache:
            self.cache[key] = self._log_p(data, params)

            if len(self.cache) > self.max_cache_size:
                self.cache.popitem(last=False)

        return self.cache[key]

    def _log_p(self, data, params):
        raise NotImplemented
Example #19
0
class LRUCacheBox(CacheBox):
    """LRU version of the cachebox.

    Defaults to maximum of 1000 keys.

    """

    def __init__(self, func, cache_key_func=None, cache_size=1000):
        super(LRUCacheBox, self).__init__(func, cache_key_func)
        self._cache = OrderedDict()
        self.cache_size = cache_size

    def current_size(self):
        return len(self._cache)

    def check_size(self):
        # http://stackoverflow.com/questions/2437617/ \
        # limiting-the-size-of-a-python-dictionary
        while self.current_size() > self.cache_size:
            self._cache.popitem(False)

    def hard_get(self, *args, **kwargs):
        val = super(LRUCacheBox, self).hard_get(*args, **kwargs)
        self.check_size()
        return val
Example #20
0
class LRUDict(MutableMapping):
    """
  A dictionary of limited size where items are evicted in LRU-order

  inspired by http://stackoverflow.com/a/2438926
  """

    def __init__(self, size, *args, **kwargs):
        self.size = size
        self.dict = OrderedDict(*args, **kwargs)
        while len(self) > self.size:
            self.dict.popitem(last=False)

    def __iter__(self):
        return iter(self.dict)

    def __len__(self):
        return len(self.dict)

    def __getitem__(self, key):
        return self.dict[key]

    def __setitem__(self, key, value):
        if key not in self and len(self) == self.size:
            self.dict.popitem(last=False)
        if key in self:  # need to delete and reinsert to maintain order
            del self[key]
        self.dict[key] = value

    def __delitem__(self, key):
        del self.dict[key]
    def findSubstring(self, s, words):
        sets = OrderedDict()
        dict = defaultdict(int)
        for i in words:
            dict[i] += 1
        width = len(words[0])
        start = 0
        res = []
        for i in range(width, len(s) + 1, width):
            # print i, sets
            tmp = s[i - width : i]
            if tmp in words:
                if tmp in sets:
                    if sets[tmp][1] > dict[tmp]:
                        sets[tmp] = (i, sets[tmp][1] + 1)
                        last = sets.popitem(last=False)
                        while last[0] != tmp:
                            last = sets.popitem(last=False)
                        start = last[1][0]

                else:
                    sets[tmp] = (i, 1)

                if len(sets) == len(words):
                    res.append(start)
                    sets.popitem(last=False)
            else:
                start = i
                sets = OrderedDict()
        return res
def dict_OrderedDict():
    """
    字典是哈希表,默认迭代是无序的,如果希望按照元素添加顺序输出结果,可以用OrderedDict
    :return:
    """
    print ">>>>>dict_OrderedDict:"
    from collections import OrderedDict

    d = dict()
    d["a"] = 1
    d["b"] = 2
    d["c"] = 3

    print u"#非按添加序列输出"
    for k, v in d.items():
        print k, v
    # a 1
    # c 3
    # b 2
    od = OrderedDict()
    od["a"] = 1
    od["b"] = 2
    od["c"] = 3

    print u"#按添加序列输出"
    for k, v in od.items():
        print k, v
    # a 1
    # b 2
    # c 3
    print u"#按LIFO顺序弹出"
    print od.popitem()  # ('c', 3)
    print od.popitem()  # ('c', 3)
    print od.popitem()  # ('a', 1)
Example #23
0
class MemoryRegion(CacheRegion):

    NO_VALUE = {}

    def __init__(self, max_keys):
        self.max_keys = max_keys
        self._cache = OrderedDict()

    def get(self, key):
        value = self._cache.get(key, self.NO_VALUE)
        if value is self.NO_VALUE:
            return False, None
        else:
            return True, value

    def set(self, key, value):
        if key in self._cache:
            getLogger("pymor.core.cache.MemoryRegion").warn("Key already present in cache region, ignoring.")
            return
        if len(self._cache) == self.max_keys:
            self._cache.popitem(last=False)
        self._cache[key] = value

    def clear(self):
        self._cache = OrderedDict()
Example #24
0
class LRUCache(object):
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.capa = capacity
        self.kv = OrderedDict()

    def get(self, key):
        """
        :rtype: int
        """
        if key in self.kv:
            val = self.kv[key]
            del self.kv[key]
            self.kv[key] = val
            return val
        return -1

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        if key in self.kv:
            del self.kv[key]
        self.kv[key] = value
        if len(self.kv) > self.capa:
            self.kv.popitem(False)
Example #25
0
class BTB:
    def __init__(self, w, n, default):
        self.btb = OrderedDict()
        self.width = w
        self.num_entries = n
        self.default_pkt = default
        print "BTB   : %d entries" % self.num_entries

    def predict(self, pc):
        #      print (("pred pc: 0x%x : " % pc) + self.__str__())
        if pc in self.btb:
            return (True, self.btb[pc])
        else:
            return (False, self.default_pkt)

    def update(self, pc, taken, target):
        if not taken:
            return
        self.btb[pc] = target
        if len(self.btb) > self.num_entries:
            self.btb.popitem(last=False)

    def __str__(self):
        string = "("
        for pc in self.btb:
            string += "(0x%x,0x%x.." % (pc, self.btb[pc][0])
        string += ")"
        return string
Example #26
0
def main():
    events = Queue.Queue(MAX_QUEUE)
    wifithread = wifisniffer.Sniffer(events)
    wifithread.daemon = True
    btthread = btsniffer.Sniffer(events)
    btthread.daemon = True
    webthread = web.HoneyServer()
    webthread.daemon = True

    macaddr.init_cache()
    wifithread.start()
    btthread.start()
    webthread.start()

    seen = OrderedDict()

    try:
        while True:
            interface, mac, when = events.get()
            name = '"%s"' % macaddr.identify(mac)
            notes = "Occurred %s" % time.ctime(when)
            if mac in seen:
                del seen[mac]
            else:
                push.note(interface, name, mac, notes)
            if len(seen) >= MAX_REMEMBER:
                seen.popitem()
            seen[mac] = when
    finally:
        macaddr.save_cache()
class _SlideCache(object):
    def __init__(self, cache_size, dz_opts):
        self.cache_size = cache_size
        self.dz_opts = dz_opts
        self._lock = Lock()
        self._cache = OrderedDict()

    def get(self, path):
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide

        osr = OpenSlide(path)
        slide = DeepZoomGenerator(osr, **self.dz_opts)
        try:
            mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
            slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            slide.mpp = 0

        with self._lock:
            if path not in self._cache:
                if len(self._cache) == self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
Example #28
0
    class decorated_function(object):
        def __init__(self, func):
            self.func = func
            self.__name__ = func.__name__
            self.cache = OrderedDict()
            self.size = 0
            self.max_size = max_size

        def __call__(self, *args):
            try:  # this code is not working correctly... (?)
                return self.cache[args]

            except KeyError:
                value = self.func(*args)
                if self.max_size > 0 and self.size > self.max_size:
                    self.cache.popitem(last=False)  # delete instead of pop?

                else:
                    self.size += 1

                self.cache[args] = value
                return self.cache[args]

            except TypeError:
                return self.func(*args)

            finally:
                self.cache[args] = self.cache.pop(args)

        def __repr__(self):
            return self.func.__doc__
Example #29
0
    def generate_rules(self, buff_dict, blacklist={}):
        """Generate rules using the provided buffers and chosen generators
        :param buff_dict: dictionary containing filename and buffers
        :type buff_dict: Dict()
        :param blacklist_dict: dictionary containing filename and buffers
        :type blacklist_dict: Dict()
        :rtype: list of rules
        """
        rules = {}
        ordered = OrderedDict()
        # We want to order the buffers by size
        for k in sorted(buff_dict, key=lambda k: len(d[k]), reverse=True):
            ordered[k] = buff_dict[k]

        if len(ordered) < 2:
            raise ValueError("Must supply at least two buffers")

        comp_buff = ordered.popitem()

        while comp_buff:
            if len(rules) == 0:
                gen_buff = comp_buff
                comp_buff = ordered.popitem()
                rules = self.generate_starting_rules(gen_buff, comp_buff, blacklist)
                continue

            rules = self.generalise_rules(comp_buff, rules, blacklist)
            comp_buff = ordered.popitem()

        return rules
Example #30
0
def dfs(graph, src, dest):
    """Depth-first search graph from src to target, printing all paths."""

    # For each node on the current path, we store an iterator for its
    # adjacency list: this allows us to continue searching when we backtrack.
    path = OrderedDict()
    path[src] = iter(graph[src])

    while path:
        frontier = next(reversed(path), None)
        if frontier == dest:
            print("-".join(path.keys()))
            path.popitem()
            continue

        # search for the next node using the iterator
        it = path[frontier]
        x = next(it, None)
        while x and x in path:
            x = next(it, None)

        if x:
            path[x] = iter(graph[x])
        else:
            path.popitem()