Basic functions and configuration stuff
Implements:
- Cache
- Config
- MJD
- UTC, UTCnow
def cache_test(path):
c = Cache(path, clear=True)
# simmple interface
c.add('one', 'one');
c.add('two', 'two')
c.add('two', 'two') # getnerates warning
if path is not None:
assert c.get('two') == 'two'
# test function interface
func = lambda x:f'value: {x}'
r1 = c('four', func, 4, description='Test')
r2 = c('four', func, 5, description='Test') #should not get called
assert c.path is None or r1==r2, f'{r1}, {r2}'
# remaving an entry
print(f'Before remove:\n{c}')
assert 'four' in c
c.remove('four')
assert 'four' not in c
print(f'After remove:\n{c}')
c.clear()
test_path = Path('/tmp/cache_test')
test_path.mkdir(exist_ok=True)
cache_test(test_path)
@ipynb_doc
def config_summary():
"""
#### config.Config -- parameters are from three sources:
- defaults
- the file `~/.config/wtlike/config.yaml` if it exists
- keyword args in Config constructor. For example, to suppress all printout:
```
config = Config(verbose=0)
```
##### Config defaults
This is yaml-format, corresponding to `config.yaml`.
{config_defaults}
#### Config contents as set up here
{config_text}
##### config.cache -- a file cache
The class `Cache`, available from `config.cache` implements a file cache in the folder
`{config.cachepath}`
{cache_text}
"""
config = Config()
config_defaults = Config.defaults
config_text = monospace(config, summary='config parameter list')
try:
cache_text = monospace(config.cache, 'current cache contents' )
except Exception as msg:
cache_text = f'(Failed: {msg})'
return locals()
config_summary()
UTC(MJD(0)), UTC(first_data), UTCnow(), MJD('now'), UTC(MJD('now')), mjd_range(0,0)
np.arange(*[mission_week(x) for x in mjd_range(0,100)])
assert UTC(MJD(0))=='2001-01-01 00:01'
assert MJD('2008')==54466
print([bin_size_name(x) for x in (1/96, 1,2,7, 14, 365.25)])
with Timer() as t:
time.sleep(2)
print('intemediate',t)
time.sleep(3)
print('Final',t)
assert(abs(t.elapsed -5)<0.1), f'wrong elapsed time: {t.elapsed}'