How to use cached method in Selene

Best Python code snippet using selene_python

CachedSource.js

Source:CachedSource.js Github

copy

Full Screen

1/*2 MIT License http://www.opensource.org/licenses/mit-license.php3 Author Tobias Koppers @sokra4*/5"use strict";6const Source = require("./Source");7const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");8const streamChunksOfRawSource = require("./helpers/streamChunksOfRawSource");9const streamAndGetSourceAndMap = require("./helpers/streamAndGetSourceAndMap");10const mapToBufferedMap = map => {11 if (typeof map !== "object" || !map) return map;12 const bufferedMap = Object.assign({}, map);13 if (map.mappings) {14 bufferedMap.mappings = Buffer.from(map.mappings, "utf-8");15 }16 if (map.sourcesContent) {17 bufferedMap.sourcesContent = map.sourcesContent.map(18 str => str && Buffer.from(str, "utf-8")19 );20 }21 return bufferedMap;22};23const bufferedMapToMap = bufferedMap => {24 if (typeof bufferedMap !== "object" || !bufferedMap) return bufferedMap;25 const map = Object.assign({}, bufferedMap);26 if (bufferedMap.mappings) {27 map.mappings = bufferedMap.mappings.toString("utf-8");28 }29 if (bufferedMap.sourcesContent) {30 map.sourcesContent = bufferedMap.sourcesContent.map(31 buffer => buffer && buffer.toString("utf-8")32 );33 }34 return map;35};36class CachedSource extends Source {37 constructor(source, cachedData) {38 super();39 this._source = source;40 this._cachedSourceType = cachedData ? cachedData.source : undefined;41 this._cachedSource = undefined;42 this._cachedBuffer = cachedData ? cachedData.buffer : undefined;43 this._cachedSize = cachedData ? cachedData.size : undefined;44 this._cachedMaps = cachedData ? cachedData.maps : new Map();45 this._cachedHashUpdate = cachedData ? cachedData.hash : undefined;46 }47 getCachedData() {48 const bufferedMaps = new Map();49 for (const pair of this._cachedMaps) {50 let cacheEntry = pair[1];51 if (cacheEntry.bufferedMap === undefined) {52 cacheEntry.bufferedMap = mapToBufferedMap(53 this._getMapFromCacheEntry(cacheEntry)54 );55 }56 bufferedMaps.set(pair[0], {57 map: undefined,58 bufferedMap: cacheEntry.bufferedMap59 });60 }61 // We don't want to cache strings62 // So if we have a caches sources63 // create a buffer from it and only store64 // if it was a Buffer or string65 if (this._cachedSource) {66 this.buffer();67 }68 return {69 buffer: this._cachedBuffer,70 source:71 this._cachedSourceType !== undefined72 ? this._cachedSourceType73 : typeof this._cachedSource === "string"74 ? true75 : Buffer.isBuffer(this._cachedSource)76 ? false77 : undefined,78 size: this._cachedSize,79 maps: bufferedMaps,80 hash: this._cachedHashUpdate81 };82 }83 originalLazy() {84 return this._source;85 }86 original() {87 if (typeof this._source === "function") this._source = this._source();88 return this._source;89 }90 source() {91 const source = this._getCachedSource();92 if (source !== undefined) return source;93 return (this._cachedSource = this.original().source());94 }95 _getMapFromCacheEntry(cacheEntry) {96 if (cacheEntry.map !== undefined) {97 return cacheEntry.map;98 } else if (cacheEntry.bufferedMap !== undefined) {99 return (cacheEntry.map = bufferedMapToMap(cacheEntry.bufferedMap));100 }101 }102 _getCachedSource() {103 if (this._cachedSource !== undefined) return this._cachedSource;104 if (this._cachedBuffer && this._cachedSourceType !== undefined) {105 return (this._cachedSource = this._cachedSourceType106 ? this._cachedBuffer.toString("utf-8")107 : this._cachedBuffer);108 }109 }110 buffer() {111 if (this._cachedBuffer !== undefined) return this._cachedBuffer;112 if (this._cachedSource !== undefined) {113 if (Buffer.isBuffer(this._cachedSource)) {114 return (this._cachedBuffer = this._cachedSource);115 }116 return (this._cachedBuffer = Buffer.from(this._cachedSource, "utf-8"));117 }118 if (typeof this.original().buffer === "function") {119 return (this._cachedBuffer = this.original().buffer());120 }121 const bufferOrString = this.source();122 if (Buffer.isBuffer(bufferOrString)) {123 return (this._cachedBuffer = bufferOrString);124 }125 return (this._cachedBuffer = Buffer.from(bufferOrString, "utf-8"));126 }127 size() {128 if (this._cachedSize !== undefined) return this._cachedSize;129 if (this._cachedBuffer !== undefined) {130 return (this._cachedSize = this._cachedBuffer.length);131 }132 const source = this._getCachedSource();133 if (source !== undefined) {134 return (this._cachedSize = Buffer.byteLength(source));135 }136 return (this._cachedSize = this.original().size());137 }138 sourceAndMap(options) {139 const key = options ? JSON.stringify(options) : "{}";140 const cacheEntry = this._cachedMaps.get(key);141 // Look for a cached map142 if (cacheEntry !== undefined) {143 // We have a cached map in some representation144 const map = this._getMapFromCacheEntry(cacheEntry);145 // Either get the cached source or compute it146 return { source: this.source(), map };147 }148 // Look for a cached source149 let source = this._getCachedSource();150 // Compute the map151 let map;152 if (source !== undefined) {153 map = this.original().map(options);154 } else {155 // Compute the source and map together.156 const sourceAndMap = this.original().sourceAndMap(options);157 source = sourceAndMap.source;158 map = sourceAndMap.map;159 this._cachedSource = source;160 }161 this._cachedMaps.set(key, {162 map,163 bufferedMap: undefined164 });165 return { source, map };166 }167 streamChunks(options, onChunk, onSource, onName) {168 const key = options ? JSON.stringify(options) : "{}";169 if (170 this._cachedMaps.has(key) &&171 (this._cachedBuffer !== undefined || this._cachedSource !== undefined)172 ) {173 const { source, map } = this.sourceAndMap(options);174 if (map) {175 return streamChunksOfSourceMap(176 source,177 map,178 onChunk,179 onSource,180 onName,181 !!(options && options.finalSource),182 true183 );184 } else {185 return streamChunksOfRawSource(186 source,187 onChunk,188 onSource,189 onName,190 !!(options && options.finalSource)191 );192 }193 }194 const { result, source, map } = streamAndGetSourceAndMap(195 this.original(),196 options,197 onChunk,198 onSource,199 onName200 );201 this._cachedSource = source;202 this._cachedMaps.set(key, {203 map,204 bufferedMap: undefined205 });206 return result;207 }208 map(options) {209 const key = options ? JSON.stringify(options) : "{}";210 const cacheEntry = this._cachedMaps.get(key);211 if (cacheEntry !== undefined) {212 return this._getMapFromCacheEntry(cacheEntry);213 }214 const map = this.original().map(options);215 this._cachedMaps.set(key, {216 map,217 bufferedMap: undefined218 });219 return map;220 }221 updateHash(hash) {222 if (this._cachedHashUpdate !== undefined) {223 for (const item of this._cachedHashUpdate) hash.update(item);224 return;225 }226 const update = [];227 let currentString = undefined;228 const tracker = {229 update: item => {230 if (typeof item === "string" && item.length < 10240) {231 if (currentString === undefined) {232 currentString = item;233 } else {234 currentString += item;235 if (currentString.length > 102400) {236 update.push(Buffer.from(currentString));237 currentString = undefined;238 }239 }240 } else {241 if (currentString !== undefined) {242 update.push(Buffer.from(currentString));243 currentString = undefined;244 }245 update.push(item);246 }247 }248 };249 this.original().updateHash(tracker);250 if (currentString !== undefined) {251 update.push(Buffer.from(currentString));252 }253 for (const item of update) hash.update(item);254 this._cachedHashUpdate = update;255 }256}...

Full Screen

Full Screen

AbstractComponent.js

Source:AbstractComponent.js Github

copy

Full Screen

1/**2 * @private3 * This is the abstract class for {@link Ext.Component}.4 *5 * This should never be overriden.6 */7Ext.define('Ext.AbstractComponent', {8 extend: 'Ext.Evented',9 onClassExtended: function(Class, members) {10 if (!members.hasOwnProperty('cachedConfig')) {11 return;12 }13 var prototype = Class.prototype,14 config = members.config,15 cachedConfig = members.cachedConfig,16 cachedConfigList = prototype.cachedConfigList,17 hasCachedConfig = prototype.hasCachedConfig,18 name, value;19 delete members.cachedConfig;20 prototype.cachedConfigList = cachedConfigList = (cachedConfigList) ? cachedConfigList.slice() : [];21 prototype.hasCachedConfig = hasCachedConfig = (hasCachedConfig) ? Ext.Object.chain(hasCachedConfig) : {};22 if (!config) {23 members.config = config = {};24 }25 for (name in cachedConfig) {26 if (cachedConfig.hasOwnProperty(name)) {27 value = cachedConfig[name];28 if (!hasCachedConfig[name]) {29 hasCachedConfig[name] = true;30 cachedConfigList.push(name);31 }32 config[name] = value;33 }34 }35 },36 getElementConfig: Ext.emptyFn,37 referenceAttributeName: 'reference',38 referenceSelector: '[reference]',39 /**40 * @private41 * Significantly improve instantiation time for Component with multiple references42 * Ext.Element instance of the reference domNode is only created the very first time43 * it's ever used44 */45 addReferenceNode: function(name, domNode) {46 Ext.Object.defineProperty(this, name, {47 get: function() {48 var reference;49 delete this[name];50 this[name] = reference = new Ext.Element(domNode);51 return reference;52 },53 configurable: true54 });55 },56 initElement: function() {57 var prototype = this.self.prototype,58 id = this.getId(),59 referenceList = [],60 cleanAttributes = true,61 referenceAttributeName = this.referenceAttributeName,62 needsOptimization = false,63 renderTemplate, renderElement, element,64 referenceNodes, i, ln, referenceNode, reference,65 configNameCache, defaultConfig, cachedConfigList, initConfigList, initConfigMap, configList,66 elements, name, nameMap, internalName;67 if (prototype.hasOwnProperty('renderTemplate')) {68 renderTemplate = this.renderTemplate.cloneNode(true);69 renderElement = renderTemplate.firstChild;70 }71 else {72 cleanAttributes = false;73 needsOptimization = true;74 renderTemplate = document.createDocumentFragment();75 renderElement = Ext.Element.create(this.getElementConfig(), true);76 renderTemplate.appendChild(renderElement);77 }78 referenceNodes = renderTemplate.querySelectorAll(this.referenceSelector);79 for (i = 0,ln = referenceNodes.length; i < ln; i++) {80 referenceNode = referenceNodes[i];81 reference = referenceNode.getAttribute(referenceAttributeName);82 if (cleanAttributes) {83 referenceNode.removeAttribute(referenceAttributeName);84 }85 if (reference == 'element') {86 referenceNode.id = id;87 this.element = element = new Ext.Element(referenceNode);88 }89 else {90 this.addReferenceNode(reference, referenceNode);91 }92 referenceList.push(reference);93 }94 this.referenceList = referenceList;95 if (!this.innerElement) {96 this.innerElement = element;97 }98 if (renderElement === element.dom) {99 this.renderElement = element;100 }101 else {102 this.addReferenceNode('renderElement', renderElement);103 }104 // This happens only *once* per class, during the very first instantiation105 // to optimize renderTemplate based on cachedConfig106 if (needsOptimization) {107 configNameCache = Ext.Class.configNameCache;108 defaultConfig = this.config;109 cachedConfigList = this.cachedConfigList;110 initConfigList = this.initConfigList;111 initConfigMap = this.initConfigMap;112 configList = [];113 for (i = 0,ln = cachedConfigList.length; i < ln; i++) {114 name = cachedConfigList[i];115 nameMap = configNameCache[name];116 if (initConfigMap[name]) {117 initConfigMap[name] = false;118 Ext.Array.remove(initConfigList, name);119 }120 if (defaultConfig[name] !== null) {121 configList.push(name);122 this[nameMap.get] = this[nameMap.initGet];123 }124 }125 for (i = 0,ln = configList.length; i < ln; i++) {126 name = configList[i];127 nameMap = configNameCache[name];128 internalName = nameMap.internal;129 this[internalName] = null;130 this[nameMap.set].call(this, defaultConfig[name]);131 delete this[nameMap.get];132 prototype[internalName] = this[internalName];133 }134 renderElement = this.renderElement.dom;135 prototype.renderTemplate = renderTemplate = document.createDocumentFragment();136 renderTemplate.appendChild(renderElement.cloneNode(true));137 elements = renderTemplate.querySelectorAll('[id]');138 for (i = 0,ln = elements.length; i < ln; i++) {139 element = elements[i];140 element.removeAttribute('id');141 }142 for (i = 0,ln = referenceList.length; i < ln; i++) {143 reference = referenceList[i];144 this[reference].dom.removeAttribute('reference');145 }146 }147 return this;148 }...

Full Screen

Full Screen

test_functional.py

Source:test_functional.py Github

copy

Full Screen

...52 raise RuntimeError("never called, slots not supported")53# noinspection PyStatementEffect54@unittest.skipIf(sys.version_info >= (3, 8), "Python 3.8+ uses standard library implementation.")55class TestCachedProperty(unittest.TestCase):56 def test_cached(self):57 item = CachedCostItem()58 self.assertEqual(item.cost, 2)59 self.assertEqual(item.cost, 2) # not 360 def test_cached_attribute_name_differs_from_func_name(self):61 item = OptionallyCachedCostItem()62 self.assertEqual(item.get_cost(), 2)63 self.assertEqual(item.cached_cost, 3)64 self.assertEqual(item.get_cost(), 4)65 self.assertEqual(item.cached_cost, 3)66 def test_threaded(self):67 go = threading.Event()68 item = CachedCostItemWait(go)69 num_threads = 370 orig_si = sys.getswitchinterval()...

Full Screen

Full Screen

test_inlinequeryresultcachedvideo.py

Source:test_inlinequeryresultcachedvideo.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# A library that provides a Python interface to the Telegram Bot API4# Copyright (C) 2015-20205# Leandro Toledo de Souza <devs@python-telegram-bot.org>6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU Lesser Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU Lesser Public License for more details.16#17# You should have received a copy of the GNU Lesser Public License18# along with this program. If not, see [http://www.gnu.org/licenses/].19import pytest20from telegram import (InlineKeyboardMarkup, InlineKeyboardButton, InputTextMessageContent,21 InlineQueryResultCachedVideo, InlineQueryResultCachedVoice)22@pytest.fixture(scope='class')23def inline_query_result_cached_video():24 return InlineQueryResultCachedVideo(25 TestInlineQueryResultCachedVideo.id_,26 TestInlineQueryResultCachedVideo.video_file_id,27 TestInlineQueryResultCachedVideo.title,28 caption=TestInlineQueryResultCachedVideo.caption,29 parse_mode=TestInlineQueryResultCachedVideo.parse_mode,30 description=TestInlineQueryResultCachedVideo.description,31 input_message_content=TestInlineQueryResultCachedVideo.input_message_content,32 reply_markup=TestInlineQueryResultCachedVideo.reply_markup)33class TestInlineQueryResultCachedVideo:34 id_ = 'id'35 type_ = 'video'36 video_file_id = 'video file id'37 title = 'title'38 caption = 'caption'39 parse_mode = 'Markdown'40 description = 'description'41 input_message_content = InputTextMessageContent('input_message_content')42 reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton('reply_markup')]])43 def test_expected_values(self, inline_query_result_cached_video):44 assert inline_query_result_cached_video.type == self.type_45 assert inline_query_result_cached_video.id == self.id_46 assert inline_query_result_cached_video.video_file_id == self.video_file_id47 assert inline_query_result_cached_video.title == self.title48 assert inline_query_result_cached_video.description == self.description49 assert inline_query_result_cached_video.caption == self.caption50 assert inline_query_result_cached_video.parse_mode == self.parse_mode51 assert (inline_query_result_cached_video.input_message_content.to_dict()52 == self.input_message_content.to_dict())53 assert (inline_query_result_cached_video.reply_markup.to_dict()54 == self.reply_markup.to_dict())55 def test_to_dict(self, inline_query_result_cached_video):56 inline_query_result_cached_video_dict = inline_query_result_cached_video.to_dict()57 assert isinstance(inline_query_result_cached_video_dict, dict)58 assert (inline_query_result_cached_video_dict['type']59 == inline_query_result_cached_video.type)60 assert inline_query_result_cached_video_dict['id'] == inline_query_result_cached_video.id61 assert (inline_query_result_cached_video_dict['video_file_id']62 == inline_query_result_cached_video.video_file_id)63 assert (inline_query_result_cached_video_dict['title']64 == inline_query_result_cached_video.title)65 assert (inline_query_result_cached_video_dict['description']66 == inline_query_result_cached_video.description)67 assert (inline_query_result_cached_video_dict['caption']68 == inline_query_result_cached_video.caption)69 assert (inline_query_result_cached_video_dict['parse_mode']70 == inline_query_result_cached_video.parse_mode)71 assert (inline_query_result_cached_video_dict['input_message_content']72 == inline_query_result_cached_video.input_message_content.to_dict())73 assert (inline_query_result_cached_video_dict['reply_markup']74 == inline_query_result_cached_video.reply_markup.to_dict())75 def test_equality(self):76 a = InlineQueryResultCachedVideo(self.id_, self.video_file_id, self.title)77 b = InlineQueryResultCachedVideo(self.id_, self.video_file_id, self.title)78 c = InlineQueryResultCachedVideo(self.id_, '', self.title)79 d = InlineQueryResultCachedVideo('', self.video_file_id, self.title)80 e = InlineQueryResultCachedVoice(self.id_, '', '')81 assert a == b82 assert hash(a) == hash(b)83 assert a is not b84 assert a == c85 assert hash(a) == hash(c)86 assert a != d87 assert hash(a) != hash(d)88 assert a != e...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful