Skip to content

Commit

Permalink
move log inside functions
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Aug 31, 2023
1 parent 4dff62b commit f9d3d69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions py/desiutil/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
from .log import get_logger, DEBUG


log = None


def find_column_name(columns, prefix=('unit', )):
"""Find the column that contains unit descriptions, or comments.
Expand Down Expand Up @@ -100,6 +97,7 @@ def load_csv_units(filename):
ValueError
If `filename` does not at least contain a "unit" column.
"""
log = get_logger()
units = dict()
comments = dict()
header = None
Expand Down Expand Up @@ -150,6 +148,7 @@ def load_yml_units(filename):
A tuple containing two :class:`dict` objects for units and comments.
If no comments are detected, the comments :class:`dict` will be empty.
"""
log=get_logger()
comments = dict()
# log.debug("y = yaml.safe_load('%s')", filename)
with open(filename, newline='') as f:
Expand Down Expand Up @@ -196,6 +195,7 @@ def annotate_table(table, units, inplace=False):
could be written to a file. If this ever changes, this function could
be extended to add comments.
"""
log = get_logger()
if inplace:
t = table
else:
Expand Down Expand Up @@ -249,6 +249,7 @@ def annotate(filename, extension, units=None, comments=None):
:class:`astropy.io.fits.HDUList`
An updated version of the file.
"""
log = get_logger()
new_hdus = list()
with fits.open(filename, mode='readonly', memmap=False, lazy_load_hdus=False, uint=False, disable_image_compression=True, do_not_scale_image_data=True, character_as_bytes=True, scale_back=True) as hdulist:
log.debug(hdulist._open_kwargs)
Expand Down Expand Up @@ -336,7 +337,6 @@ def main():
:class:`int`
An integer suitable for passing to :func:`sys.exit`.
"""
global log
options = _options()
if options.test or options.verbose:
log = get_logger(DEBUG)
Expand Down
46 changes: 23 additions & 23 deletions py/desiutil/test/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_find_key_name(self):
k = find_key_name({'RA': 'deg', 'DEC': 'deg', 'COLUMN': None}, prefix=('comment', 'description'))
self.assertEqual(e.exception.args[0], "No key matching 'comment' found!")

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_csv_units(self, mock_log):
"""Test parsing of units in a CSV file.
"""
Expand All @@ -77,7 +77,7 @@ def test_load_csv_units(self, mock_log):
self.assertEqual(units['COLUMN1'], '')
self.assertEqual(comments['COLUMN1'], 'This is a comment.')

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_csv_units_no_comment(self, mock_log):
"""Test parsing of units in a CSV file without comments.
"""
Expand All @@ -93,7 +93,7 @@ def test_load_csv_units_no_comment(self, mock_log):
self.assertEqual(units['COLUMN1'], '')
self.assertFalse(bool(comments))

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_csv_units_no_units(self, mock_log):
"""Test parsing of units in a CSV file with bad units.
"""
Expand All @@ -108,7 +108,7 @@ def test_load_csv_units_no_units(self, mock_log):
units, comments = load_csv_units(unitsFile)
self.assertEqual(str(e.exception), f"{unitsFile} does not have a unit column!")

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_yml_units(self, mock_log):
"""Test parsing of YAML input.
"""
Expand All @@ -124,9 +124,9 @@ def test_load_yml_units(self, mock_log):
self.assertEqual(units['RA'], 'deg')
self.assertIsNone(units['COLUMN'])
self.assertFalse(bool(comments))
mock_log.warning.assert_not_called()
mock_log().warning.assert_not_called()

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_yml_units_backward(self, mock_log):
"""Test parsing of older-style YAML input.
"""
Expand All @@ -141,9 +141,9 @@ def test_load_yml_units_backward(self, mock_log):
self.assertEqual(units['RA'], 'deg')
self.assertIsNone(units['COLUMN'])
self.assertFalse(bool(comments))
mock_log.warning.assert_called_once_with(f'{unitsFile} does not have a unit column, assuming keys are columns!')
mock_log().warning.assert_called_once_with(f'{unitsFile} does not have a unit column, assuming keys are columns!')

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_load_yml_units_comments(self, mock_log):
"""Test parsing of YAML input with units and comments.
"""
Expand All @@ -162,9 +162,9 @@ def test_load_yml_units_comments(self, mock_log):
units, comments = load_yml_units(unitsFile)
self.assertEqual(comments['RA'], 'deg')
self.assertEqual(comments['COLUMN'], 'dimensionless')
mock_log.warning.assert_not_called()
mock_log().warning.assert_not_called()

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_annotate_table(self, mock_log):
"""Test adding units to table columns.
"""
Expand All @@ -178,14 +178,14 @@ def test_annotate_table(self, mock_log):
tt = annotate_table(t, u)
self.assertEqual(tt['a'].unit, 'm')
self.assertEqual(tt['b'].unit, 'deg')
mock_log.debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'm'),
mock_log().debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'm'),
call("t['%s'].unit = '%s'", 'b', 'deg'),
call("Not setting blank unit for column '%s'.", 'c'),
call("Not setting blank unit for column '%s'.", 'd'),
call("Column '%s' not present in table.", 'e')])
mock_log.info.assert_called_once_with("Column '%s' not found in units argument.", 'f')
mock_log().info.assert_called_once_with("Column '%s' not found in units argument.", 'f')

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_annotate_table_inplace(self, mock_log):
"""Test adding units to table columns.
"""
Expand All @@ -200,7 +200,7 @@ def test_annotate_table_inplace(self, mock_log):
self.assertEqual(t['b'].unit, 'deg')
self.assertIs(t, tt)

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_annotate_qtable(self, mock_log):
"""Test adding units to qtable columns.
"""
Expand All @@ -214,14 +214,14 @@ def test_annotate_qtable(self, mock_log):
tt = annotate_table(t, u)
self.assertEqual(tt['a'].unit, 'm')
self.assertEqual(tt['b'].unit, 'deg')
mock_log.debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'm'),
mock_log().debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'm'),
call("t['%s'].unit = '%s'", 'b', 'deg'),
call("Not setting blank unit for column '%s'.", 'c'),
call("Not setting blank unit for column '%s'.", 'd'),
call("Column '%s' not present in table.", 'e')])
mock_log.info.assert_called_once_with("Column '%s' not found in units argument.", 'f')
mock_log().info.assert_called_once_with("Column '%s' not found in units argument.", 'f')

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_annotate_qtable_with_units_present(self, mock_log):
"""Test adding units to table columns with existing units.
"""
Expand All @@ -235,15 +235,15 @@ def test_annotate_qtable_with_units_present(self, mock_log):
tt = annotate_table(t, u)
self.assertEqual(tt['a'].unit, 'cm')
self.assertEqual(tt['b'].unit, 'deg')
mock_log.debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'cm'),
mock_log().debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'cm'),
call("t.replace_column('%s', t['%s'].to('%s'))", 'a', 'a', 'cm'),
call("t['%s'].unit = '%s'", 'b', 'deg'),
call("Not setting blank unit for column '%s'.", 'c'),
call("Not setting blank unit for column '%s'.", 'd'),
call("Column '%s' not present in table.", 'e')])
mock_log.info.assert_not_called()
mock_log().info.assert_not_called()

@patch('desiutil.annotate.log')
@patch('desiutil.annotate.get_logger')
def test_annotate_qtable_with_units_present_bad_conversion(self, mock_log):
"""Test adding units to table columns with existing units.
"""
Expand All @@ -257,11 +257,11 @@ def test_annotate_qtable_with_units_present_bad_conversion(self, mock_log):
tt = annotate_table(t, u)
self.assertEqual(tt['a'].unit, 'm')
self.assertEqual(tt['b'].unit, 'deg')
mock_log.debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'A'),
mock_log().debug.assert_has_calls([call("t['%s'].unit = '%s'", 'a', 'A'),
call("t.replace_column('%s', t['%s'].to('%s'))", 'a', 'a', 'A'),
call("t['%s'].unit = '%s'", 'b', 'deg'),
call("Not setting blank unit for column '%s'.", 'c'),
call("Not setting blank unit for column '%s'.", 'd'),
call("Column '%s' not present in table.", 'e')])
mock_log.info.assert_not_called()
mock_log.error.assert_has_calls([call("Cannot add or replace unit '%s' to column '%s'!", 'A', 'a')])
mock_log().info.assert_not_called()
mock_log().error.assert_has_calls([call("Cannot add or replace unit '%s' to column '%s'!", 'A', 'a')])

0 comments on commit f9d3d69

Please sign in to comment.