Best Python code snippet using localstack_python
jsonPath.js
Source:jsonPath.js
1dojo.provide("dojox.jsonPath.tests.jsonPath");2dojo.require("dojox.jsonPath");3dojox.jsonPath.tests.error = function(t, d, errData){4 // summary:5 // The error callback function to be used for all of the tests.6 d.errback(errData); 7}8dojox.jsonPath.tests.testData= {9 store: {10 "book": [ 11 { 12 "category":"reference",13 "author":"Nigel Rees",14 "title":"Sayings of the Century",15 "price":8.9516 },17 { 18 "category":"fiction",19 "author":"Evelyn Waugh",20 "title":"Sword of Honour",21 "price":12.9922 },23 { 24 "category":"fiction",25 "author":"Herman Melville",26 "title":"Moby Dick",27 "isbn":"0-553-21311-3",28 "price":8.9929 },30 { 31 "category":"fiction",32 "author":"J. R. R. Tolkien",33 "title":"The Lord of the Rings",34 "isbn":"0-395-19395-8",35 "price":22.9936 }37 ],38 "bicycle": {39 "color":"red",40 "price":19.9541 }42 },43 "symbols":{"@.$;":5}44}45doh.register("dojox.jsonPath.tests.jsonPath", 46 [47 {48 name: "$.store.book[*].author",49 runTest: function(t) {50 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));51 var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]';52 doh.assertEqual(success,result);53 }54 },55 {56 name: "$..author",57 runTest: function(t) {58 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));59 var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]';60 doh.assertEqual(success,result);61 62 }63 },64 {65 name: "$.store.*",66 runTest: function(t) {67 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));68 var success = '[[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}]';69 doh.assertEqual(success,result);70 }71 },72 {73 name: "$.store..price",74 runTest: function(t) {75 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));76 var success = '[8.95,12.99,8.99,22.99,19.95]';77 doh.assertEqual(success,result);78 }79 },80 {81 name: "$..book[(@.length-1)]",82 runTest: function(t) {83 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));84 var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]';85 doh.assertEqual(success,result);86 }87 },88 {89 name: "$..book[-1:]",90 runTest: function(t) {91 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));92 var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]';93 doh.assertEqual(success,result);94 }95 },96 {97 name: "$..book[0,1]",98 runTest: function(t) {99 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));100 var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]';101 doh.assertEqual(success,result);102 }103 },104 {105 name: "$..book[:2]",106 runTest: function(t) {107 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));108 var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]';109 doh.assertEqual(success,result);110 }111 },112 {113 name: "$..book[?(@.isbn)]",114 runTest: function(t) {115 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));116 var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]';117 doh.assertEqual(success,result);118 }119 },120 {121 name: "$..book[?(@.price<10)]",122 runTest: function(t) {123 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));124 var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]';125 doh.assertEqual(success,result);126 }127 },128 {129 name: "$.symbols[*]",130 runTest: function(t) {131 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));132 var success = '[5]';133 doh.assertEqual(success,result);134 }135 },136 {137 name: "$.symbols['@.$;']",138 runTest: function(t) {139 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));140 var success = '[5]';141 doh.assertEqual(success,result);142 }143 },144 {145 name: "$.symbols[(@[('@.$;')]?'@.$;':'@.$;')]",146 runTest: function(t) {147 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, this.name));148 var success = '[5]';149 doh.assertEqual(success,result);150 }151 },152 {153 name: "resultType: 'BOTH' test",154 runTest: function(t) {155 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$..book[*]",{resultType:"BOTH"}));156 var success = dojo.toJson([{"path": "$['store']['book'][0]", "value": {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95}}, {"path": "$['store']['book'][1]", "value": {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99}}, {"path": "$['store']['book'][2]", "value": {"category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99}}, {"path": "$['store']['book'][3]", "value": {"category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99}}]);157 doh.assertEqual(success,result);158 }159 },160 {161 name: "evalType: 'RESULT' test $.store.book[?(@.price<15)][1:3]",162 runTest: function(t) {163 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[?(@.price<15)][1:3]",{evalType:"RESULT"}));164 var success = '[{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]';165 doh.assertEqual(success,result);166 }167 },168 {169 name: "evalType: 'RESULT' test $.store.book[1].category",170 runTest: function(t) {171 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[1].category",{evalType:"RESULT"}));172 var success = '"fiction"';173 doh.assertEqual(success,result);174 }175 },176 {177 name: "evalType: 'RESULT' test $.store.bicycle",178 runTest: function(t) {179 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.bicycle",{evalType:"RESULT"}));180 var success = '{"color":"red","price":19.95}';181 doh.assertEqual(success,result);182 }183 },184 {185 name: "evalType: 'RESULT' test $.store.book[*]",186 runTest: function(t) {187 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book[*]",{evalType:"RESULT"}));188 var success = '["reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99]';189 doh.assertEqual(success,result);190 }191 },192 {193 name: "evalType: 'RESULT' test $.store.book.category",194 runTest: function(t) {195 var result = dojo.toJson(dojox.jsonPath.query(dojox.jsonPath.tests.testData, "$.store.book.category",{evalType:"RESULT"}));196 var success = '["reference","fiction","fiction","fiction"]';197 doh.assertEqual(success,result);198 }199 }200 ]...
parser.py
Source:parser.py
1from __future__ import print_function, absolute_import, division, generators, nested_scopes2import sys3import os.path4import logging5import ply.yacc6from jsonpath_rw.jsonpath import *7from jsonpath_rw.lexer import JsonPathLexer8logger = logging.getLogger(__name__)9def parse(string):10 return JsonPathParser().parse(string)11class JsonPathParser(object):12 '''13 An LALR-parser for JsonPath14 '''15 16 tokens = JsonPathLexer.tokens17 def __init__(self, debug=False, lexer_class=None):18 if self.__doc__ == None:19 raise Exception('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.')20 self.debug = debug21 self.lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY22 def parse(self, string, lexer = None):23 lexer = lexer or self.lexer_class()24 return self.parse_token_stream(lexer.tokenize(string))25 def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):26 # Since PLY has some crufty aspects and dumps files, we try to keep them local27 # However, we need to derive the name of the output Python file :-/28 output_directory = os.path.dirname(__file__)29 try:30 module_name = os.path.splitext(os.path.split(__file__)[1])[0]31 except:32 module_name = __name__33 34 parsing_table_module = '_'.join([module_name, start_symbol, 'parsetab'])35 # And we regenerate the parse table every time; it doesn't actually take that long!36 new_parser = ply.yacc.yacc(module=self,37 debug=self.debug,38 tabmodule = parsing_table_module,39 outputdir = output_directory,40 write_tables=0,41 start = start_symbol,42 errorlog = logger)43 return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))44 # ===================== PLY Parser specification =====================45 46 precedence = [47 ('left', ','),48 ('left', 'DOUBLEDOT'),49 ('left', '.'),50 ('left', '|'),51 ('left', '&'),52 ('left', 'WHERE'),53 ]54 def p_error(self, t):55 raise Exception('Parse error at %s:%s near token %s (%s)' % (t.lineno, t.col, t.value, t.type)) 56 def p_jsonpath_binop(self, p):57 """jsonpath : jsonpath '.' jsonpath 58 | jsonpath DOUBLEDOT jsonpath59 | jsonpath WHERE jsonpath60 | jsonpath '|' jsonpath61 | jsonpath '&' jsonpath"""62 op = p[2]63 if op == '.':64 p[0] = Child(p[1], p[3])65 elif op == '..':66 p[0] = Descendants(p[1], p[3])67 elif op == 'where':68 p[0] = Where(p[1], p[3])69 elif op == '|':70 p[0] = Union(p[1], p[3])71 elif op == '&':72 p[0] = Intersect(p[1], p[3])73 def p_jsonpath_fields(self, p):74 "jsonpath : fields_or_any"75 p[0] = Fields(*p[1])76 def p_jsonpath_named_operator(self, p):77 "jsonpath : NAMED_OPERATOR"78 if p[1] == 'this':79 p[0] = This()80 elif p[1] == 'parent':81 p[0] = Parent()82 else:83 raise Exception('Unknown named operator `%s` at %s:%s' % (p[1], p.lineno(1), p.lexpos(1)))84 def p_jsonpath_root(self, p):85 "jsonpath : '$'"86 p[0] = Root()87 def p_jsonpath_idx(self, p):88 "jsonpath : '[' idx ']'"89 p[0] = p[2]90 def p_jsonpath_slice(self, p):91 "jsonpath : '[' slice ']'"92 p[0] = p[2]93 def p_jsonpath_fieldbrackets(self, p):94 "jsonpath : '[' fields ']'"95 p[0] = Fields(*p[2])96 def p_jsonpath_child_fieldbrackets(self, p):97 "jsonpath : jsonpath '[' fields ']'"98 p[0] = Child(p[1], Fields(*p[3]))99 def p_jsonpath_child_idxbrackets(self, p):100 "jsonpath : jsonpath '[' idx ']'"101 p[0] = Child(p[1], p[3])102 def p_jsonpath_child_slicebrackets(self, p):103 "jsonpath : jsonpath '[' slice ']'"104 p[0] = Child(p[1], p[3])105 def p_jsonpath_parens(self, p):106 "jsonpath : '(' jsonpath ')'"107 p[0] = p[2]108 # Because fields in brackets cannot be '*' - that is reserved for array indices109 def p_fields_or_any(self, p):110 """fields_or_any : fields 111 | '*' """112 if p[1] == '*':113 p[0] = ['*']114 else:115 p[0] = p[1]116 def p_fields_id(self, p):117 "fields : ID"118 p[0] = [p[1]]119 def p_fields_comma(self, p):120 "fields : fields ',' fields"121 p[0] = p[1] + p[3]122 def p_idx(self, p):123 "idx : NUMBER"124 p[0] = Index(p[1])125 def p_slice_any(self, p):126 "slice : '*'"127 p[0] = Slice()128 def p_slice(self, p): # Currently does not support `step`129 "slice : maybe_int ':' maybe_int"130 p[0] = Slice(start=p[1], end=p[3])131 def p_maybe_int(self, p):132 """maybe_int : NUMBER133 | empty"""134 p[0] = p[1]135 136 def p_empty(self, p):137 'empty :'138 p[0] = None139class IteratorToTokenStream(object):140 def __init__(self, iterator):141 self.iterator = iterator142 def token(self):143 try:144 return next(self.iterator)145 except StopIteration:146 return None147if __name__ == '__main__':148 logging.basicConfig()149 parser = JsonPathParser(debug=True)...
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!!