Skip to content

Commit 23dd6e7

Browse files
delete variables after use to clean up the runtime environment namespace
1 parent 6dd89e4 commit 23dd6e7

File tree

4 files changed

+68
-44
lines changed

4 files changed

+68
-44
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
jello changelog
22

3+
20210626 v1.4.4
4+
- Rename internal variables so they don't collide with user defined names
5+
- Clean up user runtime environment of unused varialbles
6+
37
20210623 v1.4.3
48
- Update html formatting tests to only run if Pygments v2.9.0 is installed
59
- Fix typo in man page

jello/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""jello - query JSON at the command line with python syntax"""
22

33

4-
__version__ = '1.4.3'
4+
__version__ = '1.4.4'
55
AUTHOR = 'Kelly Brazil'
66
WEBSITE = 'https://github.com/kellyjonbrazil/jello'
77
COPYRIGHT = '© 2020-2021 Kelly Brazil'

jello/lib.py

+62-42
Original file line numberDiff line numberDiff line change
@@ -312,82 +312,102 @@ def load_json(data):
312312
return json_dict
313313

314314

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
318319

319320
# read data into '_' variable
320321
# 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):
322323
_ = [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]
324325

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)
327328

328329
else:
329-
_ = data
330+
_ = _θ_data
331+
332+
del _θ_data
330333

331334
# read initialization file to set colors, options, and user-defined functions
332-
jelloconf = ''
333-
conf_file = ''
335+
_θ_jelloconf = ''
336+
_θ_conf_file = ''
334337

335338
if opts.initialize:
336339
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')
338341
else:
339-
conf_file = os.path.join(os.environ["HOME"], '.jelloconf.py')
342+
_θ_conf_file = os.path.join(os.environ["HOME"], '.jelloconf.py')
340343

341344
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
344348
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}')
346350

347-
warn_options = False
348-
warn_colors = False
351+
_θ_warn_options = False
352+
_θ_warn_colors = False
349353

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'))
352356

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:
355359
opts.compact = opts.raw = opts.lines = opts.nulls = opts.mono = opts.schema = opts.types = False
356-
warn_options = True
360+
_θ_warn_options = True
357361

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']
361366

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:
363368
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)
365373

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)
368377

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
372387

373388
# 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
378398

379399
# 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]
382402

383-
elif isinstance(output, DotMap):
384-
output = output.toDict()
403+
elif isinstance(_θ_output, DotMap):
404+
_θ_output = _θ_output.toDict()
385405

386406
# 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__'):
388408
raise ValueError('Reserved key name. Use bracket notation to access this key.')
389409

390-
return output
410+
return _θ_output
391411

392412

393413
if __name__ == '__main__':

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='jello',
8-
version='1.4.3',
8+
version='1.4.4',
99
author='Kelly Brazil',
1010
author_email='[email protected]',
1111
description='Filter JSON and JSON Lines data with Python syntax.',

0 commit comments

Comments
 (0)