Best Python code snippet using pytest-benchmark
format.js
Source:format.js
...69 .replace(/\r/g, '\\r')70 .replace(/\0/g, '\\0')71 .replace(/\t/g, '\\t')72 }73 function _format(node) {74 if (node === undefined || node === null) return ''75 switch (node.kind) {76 case 'file': return eachOnNewLine(node.declarations.map(_format))77 case 'tag': return join([78 mark('comment', `#${node.value}`),79 node.list ? _format(node.list) : ''80 ])81 case 'import': {82 return separatedBySpace([83 _format(node.keyword),84 _format(node.path),85 ])86 }87 case 'use': {88 return separatedBySpace([89 _format(node.keyword),90 mark('symbol-const', _format(node.path))91 ])92 }93 case 'module': {94 const str = []95 formatTags(str, node.tags)96 str.push(_format(node.keyword))97 str.push(mark('symbol-const', _format(node.name)))98 str.push(_format(node.block))99 return separatedBySpace(str)100 }101 case 'do': {102 return join([103 formatTags(node.tags),104 _format(node.keyword),105 space(),106 mark('symbol-code', _format(node.name)),107 _format(node.parameters),108 node.arrow ? [109 space(),110 _format(node.arrow),111 space(),112 _format(node.returnType),113 ].join('') : '',114 space(),115 _format(node.body)116 ])117 }118 case 'proc': {119 const hasBody = node.body120 return join([121 _format(node.keyword),122 space(),123 mark('symbol-code', _format(node.name)),124 _format(node.parameters),125 node.arrow ? [126 space(),127 _format(node.arrow),128 space(),129 _format(node.returnType),130 ].join('') : '',131 space(hasBody ? 1 : 0),132 formatTags(node.tags, hasBody ? 1 : 0),133 _format(node.body)134 ])135 }136 case 'struct': {137 return join([138 formatTags(node.tags),139 _format(node.keyword),140 space(),141 mark('symbol-code', _format(node.name)),142 _format(node.parameters),143 ])144 }145 case 'block': {146 const begin = _format(node.begin)147 indent()148 const statements = eachOnNewLine(node.statements.map(_format))149 unindent()150 const end = indentation() + _format(node.end)151 return join([begin, newline(), statements, newline(), end])152 }153 case 'list': {154 const str = []155 str.push(_format(node.begin))156 let i = 0;157 for (let item of node.items) {158 let isSep = i % 2;159 if (isSep && i != node.items.length - 1) {160 str.push(_format(item) + space())161 } else {162 str.push(_format(item))163 }164 i++165 }166 str.push(_format(node.end))167 return join(str)168 }169 case 'var': {170 return join([171 formatTags(node.tags),172 _format(node.keyword),173 space(),174 mark(node.keyword.value == 'const' ? 'symbol-const' : 'symbol', _format(node.name)),175 (node.colon) ? [176 _format(node.colon),177 _format(node.type),178 ].join('') : '',179 node.expr ? join([180 space(),181 _format(node.equals),182 space(),183 _format(node.expr),184 _format(node.terminator),185 ]) : '',186 ])187 }188 case 'type alias': {189 return join([190 _format(node.keyword),191 space(),192 mark(node.keyword.value == 'const' ? 'symbol-const' : 'symbol', _format(node.name)),193 (node.colon) ? [194 _format(node.colon),195 _format(node.type),196 ].join('') : '',197 node.expr ? join([198 space(),199 _format(node.equals),200 _format(node.expr),201 _format(node.terminator),202 ]) : '',203 ])204 }205 case 'goto': {206 return separatedBySpace([207 _format(node.keyword),208 mark('symbol-const', _format(node.label))209 ])210 }211 case 'label': {212 return separatedBySpace([213 _format(node.keyword),214 mark('symbol-const', _format(node.label)),215 _format(node.colon),216 ])217 }218 case 'if': {219 return join([220 _format(node.keyword),221 space(),222 _format(node.condition),223 space(),224 _format(node.thenBlock),225 _format(node.elseKeyword),226 _format(node.elseBlock),227 ])228 }229 case 'break': return _format(node.keyword)230 case 'continue': return _format(node.keyword)231 case 'while': {232 return join([233 _format(node.keyword),234 space(),235 _format(node.condition),236 space(),237 _format(node.block),238 ])239 }240 case 'for': {241 return join([242 _format(node.keyword),243 space(),244 _format(node.begin),245 _format(node.preCondition),246 _format(node.terminator1),247 space(),248 _format(node.condition),249 _format(node.terminator2),250 space(),251 _format(node.postCondition),252 _format(node.end),253 space(),254 _format(node.block),255 ])256 }257 case 'each': {258 const str = []259 str.push(_format(node.keyword))260 str.push(mark('symbol', _format(node.item)))261 str.push(_format(node.colon))262 str.push(mark('symbol', _format(node.list)))263 str.push(_format(node.block))264 return separatedBySpace(str)265 }266 case 'binary': {267 const str = []268 str.push(_format(node.lhs))269 str.push(_format(node.op))270 str.push(_format(node.rhs))271 return separatedBySpace(str)272 }273 case 'reference': {274 return mark('symbol', _format(node.name))275 }276 case 'return': {277 const str = []278 str.push(_format(node.keyword))279 str.push(_format(node.expr))280 return separatedBySpace(str)281 }282 case 'parenthesized expression': {283 return join([284 _format(node.open),285 _format(node.expr),286 _format(node.close),287 ])288 }289 case 'offset access': {290 return join([291 _format(node.name),292 _format(node.begin),293 _format(node.index),294 _format(node.end),295 ])296 }297 case 'post unary': {298 return join([299 _format(node.expr),300 _format(node.op),301 ])302 }303 case 'pre unary': {304 return join([305 _format(node.op),306 _format(node.expr),307 ])308 }309 case 'call': {310 const str = []311 str.push(mark('symbol-code', _format(node.name)))312 str.push(_format(node.argumentList))313 return join(str)314 }315 case 'assignment': {316 const str = []317 return separatedBySpace([318 _format(node.name),319 _format(node.operator),320 _format(node.expr),321 ])322 }323 case 'property access': {324 const str = []325 // str.push(mark('symbol-const', _format(node.name)))326 str.push(_format(node.scope))327 str.push(_format(node.dot))328 str.push(_format(node.property))329 return join(str)330 }331 case 'terminated expression': {332 const str = []333 str.push(_format(node.expr))334 str.push(_format(node.terminator))335 return join(str)336 }337 case 'typed symbol': {338 const str = []339 str.push(_format(node.name))340 str.push(_format(node.colon))341 str.push(_format(node.type))342 return join(str)343 }344 // its color will change depending on context, the parent will color it345 case 'symbol': return mark('symbol', node.value)346 case 'operator': return mark(pseudoOperators.has(node.value) ? 'pseudo-operator' : 'operator', node.value)347 case 'number': return mark('number', node.value)348 case 'string': return mark('string', `"${escapeString(node.value)}"`)349 case 'boolean literal': return mark('bool', `${node.value}`)350 // ========== types ==========351 case 'type atom': return _format(node.name)352 case 'type mutable': {353 return join([354 _format(node.mutable),355 _format(node.it)356 ])357 }358 case 'type pointer': return join([359 mark('pseudo-operator', node.pointer.value),360 _format(node.to)361 ])362 case 'type array': {363 return join([364 _format(node.begin),365 _format(node.end),366 _format(node.of)367 ])368 }369 default: throw `unhandled node kind in formatter ${JSON.stringify(node.kind)}`370 }371 }372 assert(Array.isArray(syntaxtree))373 return _formatList(syntaxtree)374 }...
syntaxstyles.py
Source:syntaxstyles.py
2SPDX-FileCopyrightText: 2017 Eliakin Costa <eliakim170@gmail.com>3SPDX-License-Identifier: GPL-2.0-or-later4"""5from PyQt5.QtGui import QColor, QTextCharFormat, QFont6def _format(color, style='', darker=100, lighter=100):7 """Return a QTextCharFormat with the given attributes.8 """9 _color = QColor(color)10 _color = _color.darker(darker)11 _color = _color.lighter(lighter)12 fmt = QTextCharFormat()13 fmt.setForeground(_color)14 if 'bold' in style:15 fmt.setFontWeight(QFont.Bold)16 if 'italic' in style:17 fmt.setFontItalic(True)18 return fmt19class DefaultSyntaxStyle(object):20 # Syntax styles that combines with dark backgrounds21 STYLES = {22 'keyword': _format('cyan'),23 'operator': _format('orange'),24 'brace': _format('gray'),25 'defclass': _format('black', 'bold'),26 'string': _format('magenta'),27 'string2': _format('darkMagenta'),28 'comment': _format('darkGreen', 'italic'),29 'self': _format('black', 'italic'),30 'numbers': _format('brown'),31 'background': _format('white'),32 'foreground': _format('black'),33 }34 def __getitem__(self, key):35 return self.STYLES[key]36class PythonVimSyntaxStyle(object):37 """ It based in the colorschemme of the Vim editor for python code http://www.vim.org/scripts/script.php?script_id=790 """38 # Syntax styles that combines with dark backgrounds39 STYLES = {40 'keyword': _format('yellow', darker=125),41 'operator': _format('magenta', darker=150),42 'brace': _format('white'),43 'defclass': _format('orange', 'bold'),44 'string': _format('green', lighter=160),45 'string2': _format('lightGray', 'italic', darker=120),46 'comment': _format('gray', 'italic'),47 'self': _format('blue', lighter=170),48 'numbers': _format('yellow', lighter=130),49 'background': _format('black'),50 'foreground': _format('white'),51 }52 def __getitem__(self, key):53 return self.STYLES[key]54class BreezeDarkSyntaxStyle(object):55 """ Based on KDE Breeze widget style """56 # A dark syntax style.57 STYLES = {58 'keyword': _format('#eff0f1', 'bold'),59 'operator': _format('#eff0f1'),60 'brace': _format('#eff0f1'),61 'defclass': _format('#27ae60', 'bold'),62 'string': _format('#da4453'),63 'string2': _format('#da4453'),64 'comment': _format('#7f8c8d', 'italic'),65 'self': _format('#3daee9'),66 'numbers': _format('#f67400'),67 'background': _format('#232629'),68 'foreground': _format('#eff0f1'),69 }70 def __getitem__(self, key):71 return self.STYLES[key]72class BreezeLightSyntaxStyle(object):73 """ Based on KDE Breeze widget style """74 # A light syntax style.75 STYLES = {76 'keyword': _format('#31363b', 'bold'),77 'operator': _format('#31363b'),78 'brace': _format('#31363b'),79 'defclass': _format('#27ae60', 'bold'),80 'string': _format('#da4453'),81 'string2': _format('#da4453'),82 'comment': _format('#7f8c8d', 'italic'),83 'self': _format('#3daee9'),84 'numbers': _format('#f67400'),85 'background': _format('#fcfcfc'),86 'foreground': _format('#31363b'),87 }88 def __getitem__(self, key):89 return self.STYLES[key]90class BlenderSyntaxStyle(object):91 """ Based on KDE Breeze widget style """92 # A light syntax style.93 STYLES = {94 'keyword': _format('#606002'),95 'operator': _format('#4c4c4c'),96 'brace': _format('#4c4c4c'),97 'defclass': _format('#000000'),98 'string': _format('#650202'),99 'string2': _format('#650202'),100 'comment': _format('#006432'),101 'self': _format('#000000'),102 'numbers': _format('#0000c8'),103 'background': _format('#999999'),104 'foreground': _format('#000000'),105 }106 def __getitem__(self, key):107 return self.STYLES[key]108class SolarizedDarkSyntaxStyle(object):109 """ Based on http://ethanschoonover.com/solarized """110 # A dark syntax style.111 STYLES = {112 'keyword': _format('#6b9500'),113 'operator': _format('#839496'),114 'brace': _format('#839496'),115 'defclass': _format('#248bd2', 'bold'),116 'string': _format('#29a198'),117 'string2': _format('#29a198'),118 'comment': _format('#586e75', 'italic'),119 'self': _format('#248bd2'),120 'numbers': _format('#b58900'),121 'background': _format('#002a35'),122 'foreground': _format('#839496'),123 }124 def __getitem__(self, key):125 return self.STYLES[key]126class SolarizedLightSyntaxStyle(object):127 """ Based on http://ethanschoonover.com/solarized """128 # A light syntax style.129 STYLES = {130 'keyword': _format('#6b9500'),131 'operator': _format('#839496'),132 'brace': _format('#839496'),133 'defclass': _format('#248bd2', 'bold'),134 'string': _format('#29a198'),135 'string2': _format('#29a198'),136 'comment': _format('#586e75', 'italic'),137 'self': _format('#248bd2'),138 'numbers': _format('#b58900'),139 'background': _format('#fdf6e3'),140 'foreground': _format('#839496'),141 }142 def __getitem__(self, key):...
intex.test.js
Source:intex.test.js
1const _format = require('../index')2test('should return "now"', () => {3 const NOW = new Date()4 expect(_format(NOW, NOW)).toBe('now')5})6test('should return "1 second ago"', () => {7 const NOW = new Date()8 expect(_format(NOW, NOW)).not.toBe('1 second ago')9 expect(_format(new Date(NOW - 1), NOW)).toBe('1 second ago')10 expect(_format(new Date(NOW - 1000), NOW)).toBe('1 second ago')11 expect(_format(new Date(NOW - 1999), NOW)).toBe('1 second ago')12 expect(_format(new Date(NOW - 2000), NOW)).not.toBe('1 second ago')13})14test('should return "2 seconds ago"', () => {15 const NOW = new Date()16 expect(_format(new Date(NOW - 1999), NOW)).not.toBe('2 seconds ago')17 expect(_format(new Date(NOW - 2000), NOW)).toBe('2 seconds ago')18 expect(_format(new Date(NOW - 2000 + 1), NOW)).not.toBe('2 seconds ago')19 expect(_format(new Date(NOW - 2000 + 999), NOW)).not.toBe('2 seconds ago')20 expect(_format(new Date(NOW - 3000), NOW)).not.toBe('2 seconds ago')21})22test('should return "59 seconds ago"', () => {23 const NOW = new Date()24 expect(_format(new Date(NOW - 58 * 1000), NOW)).not.toBe('59 seconds ago')25 expect(_format(new Date(NOW - 59 * 1000), NOW)).toBe('59 seconds ago')26 expect(_format(new Date(NOW - 59 * 1000 - 999), NOW)).toBe('59 seconds ago')27 expect(_format(new Date(NOW - 59 * 1000 - 1), NOW)).toBe('59 seconds ago')28 expect(_format(new Date(NOW - 60 * 1000), NOW)).not.toBe('59 seconds ago')29})30test('should return "1 minute ago"', () => {31 const NOW = new Date()32 expect(_format(new Date(NOW - 60000), NOW)).toBe('1 minute ago')33 expect(_format(new Date(NOW - 61000), NOW)).toBe('1 minute ago')34 expect(_format(new Date(NOW - 2 * 60000 + 1), NOW)).toBe('1 minute ago')35 expect(_format(new Date(NOW - 2 * 60000), NOW)).not.toBe('1 minute ago')36})37test('should return "2 minutes ago"', () => {38 const NOW = new Date()39 expect(_format(new Date(NOW - 2 * 60000 + 1), NOW)).not.toBe('2 minutes ago')40 expect(_format(new Date(NOW - 2 * 60000), NOW)).toBe('2 minutes ago')41 expect(_format(new Date(NOW - 2 * 60000 + 999), NOW)).not.toBe('2 minutes ago')42 expect(_format(new Date(NOW - 3 * 60000), NOW)).not.toBe('2 minutes ago')43})44test('should return "59 minutes ago"', () => {45 const NOW = new Date()46 expect(_format(new Date(NOW - 59 * 60000 + 1), NOW)).not.toBe('59 minutes ago')47 expect(_format(new Date(NOW - 59 * 60000), NOW)).toBe('59 minutes ago')48 expect(_format(new Date(NOW - 59 * 60000 + 999), NOW)).not.toBe('59 minutes ago')49 expect(_format(new Date(NOW - 60 * 60000), NOW)).not.toBe('59 minutes ago')50})51test('should return "1 hour ago"', () => {52 const NOW = new Date()53 expect(_format(new Date(NOW - 60 * 60000), NOW)).toBe('1 hour ago')54 expect(_format(new Date(NOW - 60 * 60000 - 1000), NOW)).toBe('1 hour ago')55 expect(_format(new Date(NOW - 60 * 60000 + 1), NOW)).toBe('1 hour ago')56 expect(_format(new Date(NOW - 2 * 60 * 60000), NOW)).not.toBe('1 hour ago')57})58test('should return "23 hours ago"', () => {59 const NOW = new Date()60 expect(_format(new Date(NOW - 23 * 60 * 60000), NOW)).toBe('23 hours ago')61 expect(_format(new Date(NOW - 23 * 60 * 60000 - 1000), NOW)).toBe('23 hours ago')62 expect(_format(new Date(NOW - 23 * 60 * 60000 + 1), NOW)).not.toBe('23 hours ago')63 expect(_format(new Date(NOW - 24 * 60 * 60000), NOW)).not.toBe('23 hours ago')...
env.js
Source:env.js
1define( function(require, exports, module) {2 var fn = function(){3 this.config = null;4 };5 fn.prototype.getConfig = function() {6 if(this.config == null){7 return seajs.data.env;8 }else{9 return this.config;10 }11 };12 fn.prototype.formatUrl = function(url) {13 if(url==null){14 return "";15 }16 if (url.substring(0, 1) == '/') {17 return this.getConfig().contextPath + url;18 } else {19 return url;20 }21 };22 fn.prototype.formatText = function(str) {23 if (str==null || str.length == 0)24 return "";25 return str.replace(/&/g, ">")26 .replace(/</g, "<")27 .replace(/>/g, ">")28 .replace(/ /g, " ")29 .replace(/\'/g,"'")30 .replace(/\"/g, """)31 .replace(/\n/g, "<br>");32 };33 fn.prototype.parseDate = function(str, format){34 var _format = format || this.getConfig().pattern.datetime;35 var result=new Date();36 if(/(y+)/.test(_format))37 result.setFullYear(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));38 if(/(M+)/.test(_format))39 result.setMonth(parseInt(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length),10)-1);40 if(/(d+)/.test(_format))41 result.setDate(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));42 if(/(h+)/.test(_format))43 result.setHours(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));44 if(/(m+)/.test(_format))45 result.setMinutes(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));46 if(/(s+)/.test(_format))47 result.setSeconds(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));48 if(/(S+)/.test(_format))49 result.setMilliseconds(str.substring(_format.indexOf(RegExp.$1),_format.indexOf(RegExp.$1)+RegExp.$1.length));50 return result;51 };52 fn.prototype.variableResolver = function(str, data) {53 //aaa!{bb}asdf!{cc},æ£åæ¥æ¾æ¿æ¢!{}ä¹é´çå¼ 54 function extend(destination, source) {55 for (var property in source)56 destination[property] = source[property];57 return destination;58 }59 data = extend(data,this.getConfig().config);60 return str.replace(/\!\{([\w\.]*)\}/gi, function (_var, _key) {61 var value = data[_key]; 62 return (value === null || value === undefined) ? "" : value; 63 });64 };65 module.exports = {66 fn : new fn(),67 fnclass : fn,68 win : window69 };...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!