def test_get_with_unknown_key(): """the default value is used if no key exists""" from sherpa.astro.io.meta import Meta store = Meta() store['key1'] = 2 store['key2'] = 23 assert store.get('key3', "unknown") == "unknown"
def test_get_with_known_key(): """get works on a known key""" from sherpa.astro.io.meta import Meta store = Meta() store['key1'] = 2 store['key2'] = 23 assert store.get('key1') == 2
def test_key_pop(): """Can pop a known value""" from sherpa.astro.io.meta import Meta store = Meta() store['key1'] = True store['key2'] = False assert store.pop('key1') assert store.keys() == ['key2']
def test_key_pop(): """Can pop an unknown""" from sherpa.astro.io.meta import Meta store = Meta() store['key1'] = True store['key2'] = False assert store.pop('key') is None assert store.keys() == ['key1', 'key2']
def test_del_keyword(): """We can delete a keyword""" from sherpa.astro.io.meta import Meta store = Meta() store['x1'] = 1 store['u1'] = 2 del store['x1'] assert store.keys() == ['u1'] assert store.values() == [2]
def test_del_missing_keyword(): """We can not delete a keyword that doesn't exist""" from sherpa.astro.io.meta import Meta store = Meta() store['x1'] = 1 store['u1'] = 2 with pytest.raises(KeyError): del store['y1'] assert store.keys() == ['u1', 'x1'] assert store.values() == [2, 1]
def test_add_keyword(value): """Can add a keyword""" from sherpa.astro.io.meta import Meta store = Meta() store['key'] = value assert store.keys() == ['key'] assert store.has_key('key') assert len(store.values()) == 1 svalue = store['key'] assert svalue == value
def test_keyword_order(): """keys are sorted, not based on input order""" from sherpa.astro.io.meta import Meta store = Meta() store['key2'] = 1 store['key1'] = 2 store['a'] = True store['A'] = False keys = store.keys() assert keys == ['A', 'a', 'key1', 'key2'] vals = store.values() assert vals == [False, True, 2, 1]
def test_str_singleton(): """stringification: single value""" from sherpa.astro.io.meta import Meta store = Meta() # Could loop over this, but a bit easier to check the string # output this way. # store['key'] = "" assert str(store) == '\n key = ' store['key'] = " " assert str(store) == '\n key = ' store['key'] = " a string " assert str(store) == '\n key = a string ' store['key'] = 23 assert str(store) == '\n key = 23' store['key'] = 23.0 assert str(store) == '\n key = 23.0' store['key'] = False assert str(store) == '\n key = False' # Now some special cases for val in [None, "None", "NONE", "none"]: store['key'] = val assert str(store) == ''
def test_overwrite_keyword(): """We can change a keyword""" from sherpa.astro.io.meta import Meta store = Meta() store['val1'] = True store['val1'] = False assert not store['val1']
def test_error_if_missing_key(): """Raise the correct error on missing key""" from sherpa.astro.io.meta import Meta store = Meta() store['key'] = 1 with pytest.raises(KeyError): store['keykey']
def _get_meta_data(hdu): meta = Meta() for key in dict(hdu.header.items()).keys(): val = hdu.header[key] # empty numpy strings are not recognized by load pickle! if type(val) is numpy.str_ and val == '': val = '' meta[key] = val return meta
def _get_meta_data(crate): """ checked for new crates """ meta = Meta() names = pycrates.get_key_names(crate) if names is not None and numpy.iterable(names): for name in names: val = pycrates.get_keyval(crate, name) # empty numpy strings are not recognized by load pickle! if type(val) is numpy.str_ and val == '': val = '' meta[name] = val return meta
def test_str_multi(): """Multiple keys are displayed as expected""" from sherpa.astro.io.meta import Meta store = Meta() store['Xkey'] = 'X X' store['xkey'] = ' y y' store['a'] = 23 store['INFILE'] = 'none' store['outfile'] = '/tmp/b.fits' lines = str(store).split('\n') assert len(lines) == 5 assert lines[0] == '' assert lines[1] == ' Xkey = X X' assert lines[2] == ' a = 23' assert lines[3] == ' outfile = /tmp/b.fits' assert lines[4] == ' xkey = y y'
def test_save_pha_no_clobber(tmp_path): """save_pha: does clobber=False work?""" # This import requires an I/O backend hence it is done here # from sherpa.astro.io.meta import Meta ui.load_arrays(1, [1, 2, 3], [5, 4, 3], ui.DataPHA) # The code requires DataPHA to have a "valid" header so # fake one. Ideally we would not require it. # hdr = Meta() ui.get_data().header = hdr out = tmp_path / "data.dat" out.write_text('some text') check_clobber(out, ui.save_data)
def test_save_data_datapha(tmp_path): """Does save_data work for DataPHA?""" # This import requires an I/O backend hence it is done here # from sherpa.astro.io.meta import Meta ui.load_arrays(1, [1, 2, 3], [5, 4, 3], ui.DataPHA) # The code requires DataPHA to have a "valid" header so # fake one. Ideally we would not require it. # hdr = Meta() ui.get_data().header = hdr out = tmp_path / "data.dat" outfile = str(out) ui.save_data(outfile) cts = out.read_text() check_output(cts, ['CHANNEL', 'COUNTS'], [[1, 5], [2, 4], [3, 3]])
def test_save_pha(tmp_path): """Does save_pha work for DataPHA?""" # This import requires an I/O backend hence it is done here # from sherpa.astro.io.meta import Meta ui.load_arrays(1, [1, 2, 3], [5, 4, 3], ui.DataPHA) # The code requires DataPHA to have a "valid" header so # fake one. Ideally we would not require it. # hdr = Meta() ui.get_data().header = hdr out = tmp_path / "data.dat" outfile = str(out) ui.save_pha(outfile) # just check the first line; the output may depend on the FITS backend cts = out.read_text()[:80].split() assert cts[0] == 'SIMPLE' assert cts[1] == '=' assert cts[2] == 'T'
def test_str_empty(): """stringification: empty""" from sherpa.astro.io.meta import Meta s = str(Meta()) assert s == ''
def test_empty(): """An empty store is empty.""" from sherpa.astro.io.meta import Meta store = Meta() assert len(store.keys()) == 0
def test_key_does_not_exist(): """We know when a key does not exist""" from sherpa.astro.io.meta import Meta store = Meta() assert not store.has_key("key")