def evernote(self, bot_user: BotUser=None) -> EvernoteApi: if bot_user is None: return get_cached_object(self._evernote_apis_cache, None, constructor=lambda: evernote_api) access_token = bot_user.evernote.access.token sandbox = self.config.get("debug", True) return get_cached_object(self._evernote_apis_cache, bot_user.id, constructor=lambda: EvernoteApi(access_token, sandbox))
def test_get_cached_object(self): cache = {} result = get_cached_object(cache, None, constructor=lambda: {"default": 1}) self.assertEqual(result, {"default": 1}) result = get_cached_object(cache, "x", constructor=lambda: {"x": 2}) self.assertEqual(result, {"x": 2}) result = get_cached_object(cache, "y", constructor=lambda: {"y": 3}) self.assertEqual(result, {"y": 3}) self.assertEqual(len(cache), 3) result = get_cached_object(cache, "x") self.assertEqual(len(cache), 3) self.assertEqual(result, {"x": 2}) failed = False try: get_cached_object(cache, "invalid") except KeyError: failed = True self.assertTrue(failed)