@@ -312,82 +312,102 @@ def load_json(data):
312
312
return json_dict
313
313
314
314
315
- def pyquery (data , query ):
316
- """Sets options and runs the user's query"""
317
- output = None
315
+ def pyquery (_θ_data , _θ_query ):
316
+ """Sets options and runs the user's query. This function uses odd variable names so they don't
317
+ collide with user defined names."""
318
+ _θ_output = None
318
319
319
320
# read data into '_' variable
320
321
# if data is a list of dictionaries, then need to iterate through and convert all dictionaries to DotMap
321
- if isinstance (data , list ):
322
+ if isinstance (_θ_data , list ):
322
323
_ = [DotMap (i , _dynamic = False , _prevent_method_masking = True ) if isinstance (i , dict )
323
- else i for i in data ]
324
+ else i for i in _θ_data ]
324
325
325
- elif isinstance (data , dict ):
326
- _ = DotMap (data , _dynamic = False , _prevent_method_masking = True )
326
+ elif isinstance (_θ_data , dict ):
327
+ _ = DotMap (_θ_data , _dynamic = False , _prevent_method_masking = True )
327
328
328
329
else :
329
- _ = data
330
+ _ = _θ_data
331
+
332
+ del _θ_data
330
333
331
334
# read initialization file to set colors, options, and user-defined functions
332
- jelloconf = ''
333
- conf_file = ''
335
+ _θ_jelloconf = ''
336
+ _θ_conf_file = ''
334
337
335
338
if opts .initialize :
336
339
if sys .platform .startswith ('win32' ):
337
- conf_file = os .path .join (os .environ ['APPDATA' ], '.jelloconf.py' )
340
+ _θ_conf_file = os .path .join (os .environ ['APPDATA' ], '.jelloconf.py' )
338
341
else :
339
- conf_file = os .path .join (os .environ ["HOME" ], '.jelloconf.py' )
342
+ _θ_conf_file = os .path .join (os .environ ["HOME" ], '.jelloconf.py' )
340
343
341
344
try :
342
- with open (conf_file , 'r' ) as f :
343
- jelloconf = f .read ()
345
+ with open (_θ_conf_file , 'r' ) as _θ_f :
346
+ _θ_jelloconf = _θ_f .read ()
347
+ del _θ_f
344
348
except FileNotFoundError :
345
- raise FileNotFoundError (f'-i used and initialization file not found: { conf_file } ' )
349
+ raise FileNotFoundError (f'-i used and initialization file not found: { _θ_conf_file } ' )
346
350
347
- warn_options = False
348
- warn_colors = False
351
+ _θ_warn_options = False
352
+ _θ_warn_colors = False
349
353
350
- i_block = ast .parse (jelloconf , mode = 'exec' )
351
- exec (compile (i_block , '<string>' , mode = 'exec' ))
354
+ _θ_i_block = ast .parse (_θ_jelloconf , mode = 'exec' )
355
+ exec (compile (_θ_i_block , '<string>' , mode = 'exec' ))
352
356
353
- for option in [opts .compact , opts .raw , opts .lines , opts .nulls , opts .mono , opts .schema , opts .types ]:
354
- if not isinstance (option , bool ) and option is not None :
357
+ for _θ_option in [opts .compact , opts .raw , opts .lines , opts .nulls , opts .mono , opts .schema , opts .types ]:
358
+ if not isinstance (_θ_option , bool ) and _θ_option is not None :
355
359
opts .compact = opts .raw = opts .lines = opts .nulls = opts .mono = opts .schema = opts .types = False
356
- warn_options = True
360
+ _θ_warn_options = True
357
361
358
- for color_config in [opts .keyname_color , opts .keyword_color , opts .number_color , opts .string_color ]:
359
- valid_colors = ['black' , 'red' , 'green' , 'yellow' , 'blue' , 'magenta' , 'cyan' , 'gray' , 'brightblack' , 'brightred' ,
360
- 'brightgreen' , 'brightyellow' , 'brightblue' , 'brightmagenta' , 'brightcyan' , 'white' ]
362
+ for _θ_color_config in [opts .keyname_color , opts .keyword_color , opts .number_color , opts .string_color ]:
363
+ _θ_valid_colors = ['black' , 'red' , 'green' , 'yellow' , 'blue' , 'magenta' , 'cyan' , 'gray' , 'brightblack' ,
364
+ 'brightred' , 'brightgreen' , 'brightyellow' , 'brightblue' , 'brightmagenta' , 'brightcyan' ,
365
+ 'white' ]
361
366
362
- if color_config not in valid_colors and color_config is not None :
367
+ if _θ_color_config not in _θ_valid_colors and _θ_color_config is not None :
363
368
opts .keyname_color = opts .keyword_color = opts .number_color = opts .string_color = None
364
- warn_colors = True
369
+ _θ_warn_colors = True
370
+
371
+ if _θ_warn_options :
372
+ print (f'Jello: Warning: Options must be set to True or False in { _θ_conf_file } \n Unsetting all options.\n ' , file = sys .stderr )
365
373
366
- if warn_options :
367
- print (f'Jello: Warning: Options must be set to True or False in { conf_file } \n Unsetting all options.\n ' , file = sys .stderr )
374
+ if _θ_warn_colors :
375
+ _θ_valid_colors_string = ', ' .join (_θ_valid_colors )
376
+ print (f'Jello: Warning: Colors must be set to one of: { _θ_valid_colors_string } in { _θ_conf_file } \n Unsetting all colors.\n ' , file = sys .stderr )
368
377
369
- if warn_colors :
370
- valid_colors_string = ', ' .join (valid_colors )
371
- print (f'Jello: Warning: Colors must be set to one of: { valid_colors_string } in { conf_file } \n Unsetting all colors.\n ' , file = sys .stderr )
378
+ # clean up variables
379
+ del _θ_color_config
380
+ del _θ_conf_file
381
+ del _θ_i_block
382
+ del _θ_jelloconf
383
+ del _θ_option
384
+ del _θ_valid_colors
385
+ del _θ_warn_colors
386
+ del _θ_warn_options
372
387
373
388
# run the query
374
- block = ast .parse (query , mode = 'exec' )
375
- last = ast .Expression (block .body .pop ().value ) # assumes last node is an expression
376
- exec (compile (block , '<string>' , mode = 'exec' ))
377
- output = eval (compile (last , '<string>' , mode = 'eval' ))
389
+ _θ_block = ast .parse (_θ_query , mode = 'exec' )
390
+ del _θ_query
391
+ _θ_last = ast .Expression (_θ_block .body .pop ().value ) # assumes last node is an expression
392
+
393
+ exec (compile (_θ_block , '<string>' , mode = 'exec' ))
394
+ del _θ_block
395
+
396
+ _θ_output = eval (compile (_θ_last , '<string>' , mode = 'eval' ))
397
+ del _θ_last
378
398
379
399
# convert output back to normal dict
380
- if isinstance (output , list ):
381
- output = [i .toDict () if isinstance (i , DotMap ) else i for i in output ]
400
+ if isinstance (_θ_output , list ):
401
+ _θ_output = [i .toDict () if isinstance (i , DotMap ) else i for i in _θ_output ]
382
402
383
- elif isinstance (output , DotMap ):
384
- output = output .toDict ()
403
+ elif isinstance (_θ_output , DotMap ):
404
+ _θ_output = _θ_output .toDict ()
385
405
386
406
# if DotMap returns a bound function then we know it was a reserved attribute name
387
- if hasattr (output , '__self__' ):
407
+ if hasattr (_θ_output , '__self__' ):
388
408
raise ValueError ('Reserved key name. Use bracket notation to access this key.' )
389
409
390
- return output
410
+ return _θ_output
391
411
392
412
393
413
if __name__ == '__main__' :
0 commit comments