def init_data(self): from gna.configurator import configurator efficiencies = self.cfg.efficiencies if isinstance(efficiencies, str): try: data = configurator(self.cfg.efficiencies) except: raise Exception('Unable to open or parse file: '%self.cfg.efficiencies ) try: efficiencies = data.efficiencies self.mode = data.mode except: raise Exception('Efficiencies file (%s) should contain "efficiencies" and "mode" field'%self.cfg.efficiencies) else: self.mode = self.cfg.mode try: self.efficiencies = N.array( [tuple(e) for e in efficiencies], dtype=[('name', 'S', 20), ('eff', 'd'), ('corr', 'd'), ('uncorr', 'd')] ) except: print('Unable to parse efficiencies:') print(efficiencies) raise if not self.mode in ['absolute', 'relative']: raise Exception( 'Unsupported uncertainty type: %s (should be "absolute" or "relative")'%self.mode )
def load_dictionaries(path): from os import listdir from gna.configurator import configurator for filename in listdir(path): if not filename.endswith('.py'): continue filepath = path + '/' + filename cfg = configurator(filepath) reg_dictionary(filename[:-2], cfg.dictionary)
def get_data(source): if isinstance(source, str): try: data = configurator(source) return data['data'] except: print('Unable to open or parse file {}'.format(source)) raise elif isinstance(source, (NestedDict, OrderedDict, dict)): return source else: raise Exception("Wrong type of data source {}".format( type(source)))
def init_data(self): try: self.file_idx_to_reactor = self.cfg.name_mapping except KeyError: self.file_idx_to_reactor = mapping_idx_to_reactors self.core_info_daily = defaultdict(lambda: defaultdict(list)) with np.load(self.cfg.reactor_info) as f: self.data = f['power'] seconds_per_day = 60*60*24 for it in self.data: current_core = self.core_info_daily[ self.file_idx_to_reactor[it['core']] ] current_core['power'].append(it['power']) current_core['days'].append((it['end'] - it['start'])/seconds_per_day) current_core['fission_fractions'].append(it['iso']) self.fission_info = configurator(self.cfg.fission_uncertainty_info)
import string from collections import OrderedDict import itertools as I from gna.configurator import configurator cfg = configurator('{location}/gna/labels.py', subst='default', debug=False) class LFormatter(string.Formatter): def get_value(self, key, args, kwargs): if isinstance(key, (int)): return args[key] res = dictionaries.get(key, kwargs) if not res is None: return res if key.startswith('$'): return self.get_value(dictionaries.get(key[1:], kwargs), args, kwargs) if key.startswith('^'): return (self.get_value(key[1:], args, kwargs)).capitalize() return '?' + key + '?' def __call__(self, s, *args, **kwargs): return self.format(s, *args, **kwargs) def s(self, key, **kwargs): return self.base(key, **kwargs)
#!/usr/bin/env python from gna.configurator import configurator from pprint import pprint print('Cfg 1') c = configurator('tests/elementary/test_cfgloader_cfg1.py', debug=True) pprint(c) print() print('Cfg 2') c.__load__('tests/elementary/test_cfgloader_cfg2.py') pprint(c) print() print('Cfg 3 (nested)') c.__load__('tests/elementary/test_cfgloader_cfg3.py') pprint(c) print() print('Test subst (list)') c = configurator('tests/elementary/test_cfgloader_{0}.py', debug=True, subst=['cfg1', 'cfg2']) pprint(c) print() print('Test subst (dict)') c = configurator('tests/elementary/test_cfgloader_{location}.py', debug=True, subst=dict(key='location', values=['cfg1', 'cfg2', 'cfg3']))
#!/usr/bin/env python """GNA configuration library the global configuration is read from './config' folder optional configuration overriding the global one may be stored in the './config_local' folder""" from gna.configurator import configurator cfg = configurator('{location}/gna/gnacfg.py', subst='default', debug=False, prefetch=False)
#!/usr/bin/env python from gna.configurator import configurator cfg = configurator() cfg.a = 1 cfg['b'] = 2 cfg.set('c', 3) print(cfg) print(cfg.a) print(cfg['a']) print(cfg.get('a')) print() cfg = configurator() cfg('b') cfg['b.d'] = 1 cfg.b.e = 2 cfg.set('f.g', 3) cfg.setdefault('h.g', 3) cfg.setdefault('h.g', 4) print(cfg) print(cfg.b.d) print(cfg['b.d']) print(cfg.get('b.d')) print(cfg.get('b.d', 'none')) try: print(cfg.get('d.d', 'none')) print('\033[32mOK!\033[0m') except KeyError: