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


Python Cache.get_or_set方法代码示例

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


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

示例1: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_or_set [as 别名]
class Web:

    handlers = {
        'snl.no': SNL,
        'ndla.no': NDLA,
        'general': General
    }

    def __init__(self, http_client=None):
        self.cache = Cache()

    def get_handler(self, url):
        '''Returns an appropriate handler for the url.'''
        hostname = urlparse(url).hostname
        return self.handlers.get(hostname, self.handlers['general'])

    def retrieve_and_parse(self, url):
        '''Requests and parses response.'''
        handler_class = self.get_handler(url)
        handler = handler_class(url)
        data = handler.request()
        website = handler.parse(data)
        return website

    def read(self, url):
        '''Returns a Website with metadata.'''
        if not url.startswith('http'):
            url = 'http://%s' % url
        website = self.cache.get_or_set(url, lambda: self.retrieve_and_parse(url))
        return website
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:32,代码来源:web.py

示例2: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_or_set [as 别名]
class Nasjonalbiblioteket:

    HOST = 'http://www.nb.no'
    READ = HOST + '/nbsok/content/reference'
    SEARCH = HOST + '/services/search/v2/search'

    def __init__(self, http_client=None):
        self.cache = Cache()
        self.http_client = http_client or HTTPClient()
        self.logger = getLogger(self.__class__.__name__)

    def extract_identifiers(self, xml):
        '''Extracts identifiers from the sesamids in the entry tags.'''
        sesam_identifiers = AtomParser(xml).sesam_identifiers
        urls = [sesam_id.text for sesam_id in sesam_identifiers]
        return urls

    def extract_fields(self, endnote):
        '''Extracts fields from EndNote text.'''
        endnote_parser = EndNoteParser()
        fields = endnote_parser.parse(endnote)
        return fields

    def read(self, identifier):
        '''Reads metadata for an identifier.'''
        parameters = ('id', identifier), ('format', 'enw')
        raw_endnote = self.cache.get_or_set(identifier,
            lambda: self.http_client.get(self.READ, parameters)
        )
        return self.extract_fields(raw_endnote) if raw_endnote else {}

    def read_multiple(self, identifiers):
        '''Concurrently retrieves metadata for identifiers.'''
        with ThreadPoolExecutor() as concurrent:
            endnotes = concurrent.map(self.read, identifiers)
            return list(endnotes)

    def search(self, query):
        '''Finds identifiers that matches query.'''
        self.logger.info(query)
        parameters = ('itemsPerPage', 10), ('q', query)
        xml = self.cache.get_or_set(query,
            lambda: self.http_client.get(self.SEARCH, parameters)
        )
        return self.extract_identifiers(xml) if xml else []
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:47,代码来源:nasjonalbiblioteket.py

示例3: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_or_set [as 别名]
class Oria:

    HOST = 'https://bibsys-almaprimo.hosted.exlibrisgroup.com'
    READ = HOST + '/primo_library/libweb/action/PushToAction.do'
    SEARCH = HOST + '/primo_library/libweb/action/search.do'

    def __init__(self, http_client=None):
        self.cache = Cache()
        self.http_client = http_client or HTTPClient()
        self.logger = getLogger(self.__class__.__name__)

    def extract_identifiers(self, html):
        '''Extracts BIBSYS identifiers from HTML.'''
        parser = HTMLResultParser()
        parser.feed(html or '')
        identifiers = [identifier for identifier in parser.identifiers \
            if identifier.startswith('BIBSYS')]
        return identifiers

    def search(self, query):
        '''Finds identifiers that matches query.'''
        self.logger.info(query)
        parameters = ('fn', 'search'), ('vl(freeText0)', query)
        identifiers = self.cache.get_or_set(query, lambda:
            self.extract_identifiers(self.http_client.get(self.SEARCH, parameters))
        )
        return identifiers or []

    def read(self, identifier):
        '''Reads metadata for an identifier.'''
        endnote_parser = EndNoteParser()
        data = {'encode': 'UTF-8'}
        parameters = ('pushToType', 'EndNote'), ('docs', identifier)
        parsed_endnote = self.cache.get_or_set(identifier, lambda: 
            endnote_parser.parse(self.http_client.post(self.READ, parameters, data))
        )
        return OriaConverter.convert(parsed_endnote)

    def read_multiple(self, identifiers):
        '''Concurrently retrieves metadata for identifiers.'''
        with ThreadPoolExecutor() as concurrent:
            endnotes = concurrent.map(self.read, identifiers)
            return list(endnotes)
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:45,代码来源:oria.py

示例4: test_returns_existing_value_if_key_exist

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_or_set [as 别名]
 def test_returns_existing_value_if_key_exist(self):
     cache = Cache()
     value = cache.set('foo', 'rab')
     value = cache.get_or_set('foo', lambda: 'bar')
     self.assertEqual(value, 'rab')
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:7,代码来源:test_cache.py

示例5: test_returns_value_from_value_func_if_no_key_exist

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_or_set [as 别名]
 def test_returns_value_from_value_func_if_no_key_exist(self):
     cache = Cache()
     value = cache.get_or_set('foo', lambda: 'bar')
     self.assertEqual(value, 'bar')
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:6,代码来源:test_cache.py


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