当前位置: 首页>>代码示例>>Python>>正文


Python LRU.get方法代码示例

本文整理汇总了Python中lru.LRU.get方法的典型用法代码示例。如果您正苦于以下问题:Python LRU.get方法的具体用法?Python LRU.get怎么用?Python LRU.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lru.LRU的用法示例。


在下文中一共展示了LRU.get方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_stats

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
    def test_stats(self):
        for size in SIZES:
            l = LRU(size)
            for i in range(size):
                l[i] = str(i)

            self.assertTrue(l.get_stats() == (0, 0))

            val = l[0]
            self.assertTrue(l.get_stats() == (1, 0))

            val = l.get(0, None)
            self.assertTrue(l.get_stats() == (2, 0))

            val = l.get(-1, None)
            self.assertTrue(l.get_stats() == (2, 1))

            try:
                val = l[-1]
            except:
                pass

            self.assertTrue(l.get_stats() == (2, 2))

            l.clear()
            self.assertTrue(len(l) == 0)
            self.assertTrue(l.get_stats() == (0, 0))
开发者ID:snorfalorpagus,项目名称:lru-dict,代码行数:29,代码来源:test_lru.py

示例2: test_hits

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
    def test_hits(self):
        for size in SIZES:
            l = LRU(size)
            for i in range(size):
                l[i] = str(i)

            val = l[0]
            self.assertTrue(l.get_hits() == 1)
            self.assertTrue(l.get_misses() == 0)

            val = l.get(0, None)
            self.assertTrue(l.get_hits() == 2)
            self.assertTrue(l.get_misses() == 0)

            val = l.get(-1, None)
            self.assertTrue(l.get_hits() == 2)
            self.assertTrue(l.get_misses() == 1)

            try:
                val = l[-1]
            except:
                pass

            self.assertTrue(l.get_hits() == 2)
            self.assertTrue(l.get_misses() == 2)

            l.clear()
            self.assertTrue(len(l) == 0)
            self.assertTrue(l.get_hits() == 0)
            self.assertTrue(l.get_misses() == 0)
开发者ID:jtoberon,项目名称:lru-dict,代码行数:32,代码来源:test_lru.py

示例3: test_get_and_del

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
 def test_get_and_del(self):
     l = LRU(2)
     l[1] = '1'
     self.assertEqual('1', l.get(1))
     self.assertEqual('1', l.get(2, '1'))
     self.assertIsNone(l.get(2))
     self.assertEqual('1', l[1])
     self.assertRaises(KeyError, lambda: l['2'])
     with self.assertRaises(KeyError):
         del l['2']
开发者ID:snorfalorpagus,项目名称:lru-dict,代码行数:12,代码来源:test_lru.py

示例4: __init__

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class topic4:
    def __init__(self, c_hash, c_user, c_words):
        self.topic_count =1
        self.l1 = LRU(c_hash)
        self.l2 = LRU(c_user)

    def set_hashLRU(self,l):
        self.set(self.l1, l)

    def set_userLRU(self,l):
        self.set(self.l2, l)



    def set(self, lru, l):
        for k in l:
            v = lru.get(k,0)
            lru[k]=v+1

    def set_cluster(self, hashtags, users, words):
        for k in hashtags:
            self.l1[k]=self.l1.get(k,0)+1
        for k in users:
            self.l2[k]=self.l2.get(k,0)+1

        self.topic_count+=1

    def get_similarity(self,hashtags,users,words):
        h_sum = 1
        u_sum = 1
        w_sum = 1
        h_match =0
        h_ind =0
        u_ind =0
        w_ind =0
        c=0
        h1 = self.l1.get_size()
        u1 = self.l2.get_size()
        for h in hashtags:
            # l1_items=zip(*self.l1.items())
            h_sum+= self.l1.get(h,0)
            if(self.l1.has_key(h)):
                ind = self.l1.keys().index(h)
                h_ind+= h1 - ind
                h_match+= 1 if ind<250 else 0
        for u in users:
            u_sum+= self.l2.get(u,0)
            if(self.l2.has_key(u)):
                u_ind+= u1 - self.l2.keys().index(u)

        if(h_match !=0):
            c = h_match -1
        # print(h_ind,h1,u_ind,u1,w_ind,w1, h_sum,w_sum,)
        similarity = (h_ind/(h1+1))*(h_sum/sum(self.l1.values() +[1])) + (u_ind/(u1+1))*(u_sum/sum(self.l2.values()+[1]))  +c
        return similarity
开发者ID:nishucsd,项目名称:thesis,代码行数:57,代码来源:clusters2.py

示例5: test_access_within_size

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
 def test_access_within_size(self):
     for size in SIZES:
         l = LRU(size)
         for i in xrange(size):
             l[i] = str(i)
         for i in xrange(size):
             self.assertEquals(l[i], str(i))
             self.assertEquals(l.get(i,None), str(i))
开发者ID:snorfalorpagus,项目名称:lru-dict,代码行数:10,代码来源:test_lru.py

示例6: test_access

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
 def test_access(self):
     for size in SIZES:
         l = LRU(size)
         n = size * 2
         for i in xrange(n):
             l[i] = str(i)
         self._check_kvi(range(n-1,size-1,-1), l)
         for i in xrange(size, n):
             self.assertEquals(l[i], str(i))
             self.assertEquals(l.get(i,None), str(i))
开发者ID:snorfalorpagus,项目名称:lru-dict,代码行数:12,代码来源:test_lru.py

示例7: __init__

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class topic:
    def __init__(self, c_hash, c_user, c_words):
        self.topic_count =1
        self.l1 = LRU(c_hash)
        self.l2 = LRU(c_user)
        self.l3 = LRU(c_words)

    def set_hashLRU(self,l):
        self.set(self.l1, l)

    def set_userLRU(self,l):
        self.set(self.l2, l)

    def set_wordLRU(self,l):
        self.set(self.l3, l)

    def set(self, lru, l):
        for k in l:
            v = lru.get(k,0)
            lru[k]=v+1

    def set_cluster(self, hashtags, users, words):
        for k in hashtags:
            v = self.l1.get(k,0)
            self.l1[k]=v+1
        for k in users:
            v = self.l2.get(k,0)
            self.l2[k]=v+1
        for k in words:
            v = self.l3.get(k,0)
            self.l3[k]=v+1
        self.topic_count+=1

    def get_similarity(self,hashtags,users,words):
        h_sum = 0
        u_sum = 0
        w_sum = 0

        for h in hashtags:
            h_sum+= self.l1.get(h,0)
        for u in users:
            u_sum+= self.l2.get(u,0)
        for w in words:
            w_sum+= self.l3.get(w,0)

        similarity = 0.5*h_sum + 0.2*u_sum + 0.3*w_sum
        return similarity
开发者ID:nishucsd,项目名称:thesis,代码行数:49,代码来源:clusters.py

示例8: __init__

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class topic4:
    def __init__(self, c_hash, c_user, c_words):
        self.topic_count =1
        # self.time = (self.first,self.last)
        self.l1 = LRU(c_hash)
        self.first =""
        self.last=""
        self.lats=[]
        self.longs=[]
        self.l2 = LRU(c_user)
        self.l3 = LRU(c_words)
        self.l4 = LRU(400)
    def set_hashLRU(self,l):
        self.set(self.l1, l)

    def set_userLRU(self,l):
        self.set(self.l2, l)

    def set_wordLRU(self,l):
        self.set(self.l3, l)

    def set(self, lru, l):
        for k in l:
            v = lru.get(k,0)
            lru[k]=v+1

    def set_cluster(self, hashtags, users, words,links, cords):
        for k in hashtags:
            self.l1[k]=self.l1.get(k,0)+1
        for k in users:
            self.l2[k]=self.l2.get(k,0)+1
        for k in words:
            self.l3[k]=self.l3.get(k,0)+1
        for k in links:
            self.l4[k]=self.l4.get(k,0)+1
        if(cords is not None):
            self.lats.append(cords["coordinates"][1])
            self.longs.append(cords["coordinates"][0])
        self.topic_count+=1

    def get_similarity(self,hashtags,users,words):
        h_sum = 1
        u_sum = 1
        w_sum = 1
        h_match =0
        h_ind =0
        u_ind =0
        w_ind =0
        c=0
        h1 = self.l1.get_size()
        u1 = self.l2.get_size()
        w1 = self.l3.get_size()
        for h in hashtags:
            # l1_items=zip(*self.l1.items())
            h_sum+= self.l1.get(h,0)
            if(self.l1.has_key(h)):
                ind = self.l1.keys().index(h)
                h_ind+= h1 - ind
                h_match+= 1 if ind<250 else 0
        for u in users:
            u_sum+= self.l2.get(u,0)
            if(self.l2.has_key(u)):
                u_ind+= u1 - self.l2.keys().index(u)
        for w in words:
            w_sum+= self.l3.get(w,0)
            if(self.l3.has_key(w)):
                w_ind+= w1 - self.l3.keys().index(w)
        if(h_match !=0):
            c = h_match -1
        # print(h_ind,h1,u_ind,u1,w_ind,w1, h_sum,w_sum,)
        similarity = (h_ind/(h1+1))*(h_sum/sum(self.l1.values() +[1])) + (u_ind/(u1+1))*(u_sum/sum(self.l2.values()+[1])) + (w_ind/(w1+1))*(w_sum/sum(self.l3.values()+[1])) +c
        return similarity
    def flush1(self, cache, size):
        if(len(cache.keys())>5):
            tokens = reversed(cache.keys()[5])
            cache.clear()
            for i in tokens:
                cache[i]=1


    def flush(self):
        self.flush1(self.l1,500)
        self.flush1(self.l2, 500)
        self.flush1(self.l3,3500)
        self.topic_count=1
开发者ID:nishucsd,项目名称:thesis,代码行数:87,代码来源:clusters4.py

示例9: MTPFS

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class MTPFS(LoggingMixIn, Operations):   
   def __init__(self, mtp, mountpoint, is_debug=False, logger=None):
      global VERBOSE
      self.mtp = mtp
      self.is_debug = is_debug
      self.tempdir = tempfile.mkdtemp(prefix='pymtpfs')
      if not bool(self.tempdir) or not os.path.exists(self.tempdir):
         self.tempdir = tempfile.gettempdir()
      self.read_timeout = 2
      self.write_timeout = 2      
      self.openfile_t = namedtuple('openfile', 'handle, path, mtp_path, readonly')
      self.openfiles = {}
      self.log = logger
      self.created = LRU(1000) 
      if VERBOSE:         
         print("Mounted %s on %s" % (self.mtp, ))
      self.log.info("Mounted %s on %s" % (self.mtp, mountpoint))
   
   def __openfile_by_path(self, path):
      return next((en for en in self.openfiles.values() if en.mtp_path == path), None)

   def destroy(self, path):
      self.mtp.close()
      for openfile in self.openfiles.values():
         try:
            os.close(openfile.handle)
         except:
            self.log.exception("")
      try:
         if self.tempdir != tempfile.gettempdir():
            shutil.rmtree(self.tempdir)
      except:
         self.log.exception("")
      return 0
      
   def chmod(self, path, mode):
      return 0

   def chown(self, path, uid, gid):
      return 0
   
#   @log_calls
   def getattr(self, path, fh=None):
      attrib = {}
      path = fix_path(path, self.log)
      entry = self.mtp.get_path(path)
      if entry is None:
         entry = self.created.get(path)
      if entry is None:         
         raise FuseOSError(errno.ENOENT)
      else:
         try:
            attrib = entry.get_attributes()
         except Exception, e:
            self.log.exception("")
            attrib = {}
            exmess = ""
            try:
               exmess = str(e.message)
            except:
               exmess = "Unknown"
            self.log.error('Error reading MTP attributes for %s (%s)' % (path, exmess))
            raise FuseOSError(errno.ENOENT)            
      return attrib      
开发者ID:donaldmunro,项目名称:pymtpfs,代码行数:66,代码来源:pymtpfs.py

示例10: MTPStorage

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class MTPStorage(MTPEntry, MTPRefresh):
   def __init__(self, mtp, pstorage=None):
      global PATH_CACHE_SIZE
      MTPRefresh.__init__(self)
      self.mtp = mtp
      self.libmtp = mtp.libmtp
      self.open_device = mtp.open_device
      self.directories = None
      self.contents = LRU(PATH_CACHE_SIZE)
      if pstorage is None:
         MTPEntry.__init__(self, -3, '/')
         self.storage = None
         self.directories = []
         for dirname in self.mtp.get_storage_descriptions():
            #def __init__(self, path, id=-2, storageid=-2, folderid=-2, mtp=None, timestamp=0, is_refresh=True):
            self.directories.append(MTPFolder(path=dirname, id= -3, storageid= -3, folderid= -2, is_refresh=False)) 
         self.root = None
         self.contents[utf8(os.sep)] = self
      else:         
         self.storage = pstorage
         storage = pstorage.contents
         self.type = storage.StorageType
         self.freespace = storage.FreeSpaceInBytes
         self.capacity = storage.MaxCapacity
         path = os.sep + storage.StorageDescription
         MTPEntry.__init__(self, storage.id, path, storageid=None, folderid=0)
         self.root = MTPFolder(path=path, id=0, storageid=storage.id, folderid=0, mtp=self.mtp)
         self.contents[utf8(path)] = self.root
      
   def is_directory(self):
      return True     
   
   def get_attributes(self):
      return { 'st_atime': self.timestamp, 'st_ctime': self.timestamp, 'st_gid': os.getgid(),
               'st_mode': stat.S_IFDIR | 0755, 'st_mtime': self.timestamp, 'st_nlink': 1,
               'st_size': 0, 'st_uid': os.getuid() }
      
   def get_directories(self):
      if self.directories is None:
         if self.root is None:
            return ()
         else:
            return self.root.get_directories()
      else:
         return self.directories
      
   def get_files(self):
      if self.root is None:
         return ()
      else:
         return self.root.get_files()
      
   def add_file(self, file):
      if not self.root is None:
         self.root.add_file(self, file)
               
   def __str__(self):            
      s = "MTPStorage %s: id=%d, device=%s%s" % (self.name, self.id, self.open_device, os.linesep)      
      return s
         
   def find_entry(self, path):
      path = utf8(path)
      self.log.debug('find_entry(%s)' % (path,))
      try:
         if path.strip() == '':
            path = os.sep + self.name 
         entry = self.contents.get(path)
         if entry is None:         
            components = [comp for comp in path.split(os.sep) if len(comp.strip()) != 0]
            if len(components) == 0:
               return None
            if components[0] != self.name:
               raise LookupError('Invalid storage (expected %s, was %s)' % (self.name, components[0]))
            entry = self.__find_entry(self.root, components[1:])
         else:
            if entry.is_directory() and entry.must_refresh:
               entry.refresh()
         return entry
      except:
         self.log.exception("")
         return None
   
   def __find_entry(self, entry, components):
      self.log.debug("__find_entry(%s, %s)" % (entry, str(components)))
      if len(components) == 0:
         return entry
      name = components[0]
      path = entry.path + os.sep + name 
      en = self.contents.get(utf8(path))
      if not en is None:
         if en.is_directory() and en.must_refresh:
            en.refresh()
         return self.__find_entry(en, components[1:])
      en = entry.find_directory(name)
      if not en is None and en.is_directory():
         self.contents[utf8(path)] = en
         if en.must_refresh:
            en.refresh()
         return self.__find_entry(en, components[1:])
      return entry.find_file(name)
#.........这里部分代码省略.........
开发者ID:donaldmunro,项目名称:pymtpfs,代码行数:103,代码来源:mtp.py

示例11: PolygonIndex

# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import get [as 别名]
class PolygonIndex(object):
    include_only_properties = None
    simplify_tolerance = 0.0001
    preserve_topology = True
    persistent_polygons = False
    cache_size = 0
    fix_invalid_polygons = False

    INDEX_FILENAME = None
    POLYGONS_DB_DIR = 'polygons'

    def __init__(self, index=None, polygons=None, polygons_db=None, save_dir=None,
                 index_filename=None,
                 polygons_db_path=None,
                 include_only_properties=None):
        if save_dir:
            self.save_dir = save_dir
        else:
            self.save_dir = None

        if not index_filename:
            index_filename = self.INDEX_FILENAME

        self.index_path = os.path.join(save_dir or '.', index_filename)

        if not index:
            self.create_index(overwrite=True)
        else:
            self.index = index

        if include_only_properties and hasattr(include_only_properties, '__contains__'):
            self.include_only_properties = include_only_properties

        if not polygons and not self.persistent_polygons:
            self.polygons = {}
        elif polygons and not self.persistent_polygons:
            self.polygons = polygons
        elif self.persistent_polygons and self.cache_size > 0:
            self.polygons = LRU(self.cache_size)
            if polygons:
                for key, value in six.iteritems(polygons):
                    self.polygons[key] = value

            self.cache_hits = 0
            self.cache_misses = 0

            self.get_polygon = self.get_polygon_cached

        if not polygons_db_path:
            polygons_db_path = os.path.join(save_dir or '.', self.POLYGONS_DB_DIR)

        if not polygons_db:
            self.polygons_db = LevelDB(polygons_db_path)
        else:
            self.polygons_db = polygons_db

        self.setup()

        self.i = 0

    def create_index(self, overwrite=False):
        raise NotImplementedError('Children must implement')

    def index_polygon(self, polygon):
        raise NotImplementedError('Children must implement')

    def setup(self):
        pass

    def clear_cache(self, garbage_collect=True):
        if self.persistent_polygons and self.cache_size > 0:
            self.polygons.clear()
            if garbage_collect:
                gc.collect()

    def simplify_polygon(self, poly, simplify_tolerance=None, preserve_topology=None):
        if simplify_tolerance is None:
            simplify_tolerance = self.simplify_tolerance
        if preserve_topology is None:
            preserve_topology = self.preserve_topology
        return poly.simplify(simplify_tolerance, preserve_topology=preserve_topology)

    def index_polygon_properties(self, properties):
        pass

    def polygon_geojson(self, poly, properties):
        return {
            'type': 'Feature',
            'geometry': mapping(poly),
        }

    def add_polygon(self, poly, properties, cache=False, include_only_properties=None):
        if include_only_properties is not None:
            properties = {k: v for k, v in properties.iteritems() if k in include_only_properties}

        if not self.persistent_polygons or cache:
            self.polygons[self.i] = prep(poly)

        if self.persistent_polygons:
            self.polygons_db.Put(self.polygon_key(self.i), json.dumps(self.polygon_geojson(poly, properties)))
#.........这里部分代码省略.........
开发者ID:BERENZ,项目名称:libpostal,代码行数:103,代码来源:index.py


注:本文中的lru.LRU.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。