def test_iter_cast(os_env): env = EnvWrapper( NUMBERS=EnvVar(postprocessor=EnvVar.tokenize(sep=','), sub_cast=float)) os_env['NUMBERS'] = '0.1, 0.2, 0.3, 0.4' assert env.NUMBERS == [0.1, 0.2, 0.3, 0.4] d = env.collect() assert d['NUMBERS'] == os_env['NUMBERS']
def test_error_inclusion(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool), VAR1=EnvVar(include_if='FLAG')) os_env['VAR1'] = 'some_value' os_env['FLAG'] = 'off' with pytest.raises(ConfigurationError) as e: _ = env._get('VAR1') assert str(e.value) == 'Conditions to include variable VAR1 are not met'
def test_items(os_env): env = EnvWrapper(VAR1=EnvVar(), VAR2=EnvVar()) os_env['VAR1'] = 'foo' os_env['VAR2'] = 'bar' d = dict(env.items()) assert set(env.keys()) == set(d.keys()) assert d['VAR1'] == 'foo' assert d['VAR2'] == 'bar'
def test_json(os_env): env = EnvWrapper(JSON=EnvVar(postprocessor=json.loads)) expected = {'foo': 1, 'bar': 0.23} os_env['JSON'] = json.dumps(expected) actual = env.JSON assert actual == expected d = env.collect() assert d['JSON'] == os_env['JSON']
def test_alt_bundle(os_env): env = EnvWrapper(SETTINGS=EnvVar(convert=dict)) d = {'foo': 0, 'bar': False, 'spam': "yada"} val = str(d) os_env['SETTINGS'] = val v = env.SETTINGS assert v == d assert env.collect()['SETTINGS'] == os_env['SETTINGS']
def test_error_exclusion(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool), VAR1=EnvVar(exclude_if='FLAG')) os_env['VAR1'] = 'some_value' os_env['FLAG'] = 'on' with pytest.raises(ConfigurationError) as e: _ = env._get('VAR1') assert str(e.value) == 'Variable VAR1 is explicitly excluded ' \ 'from this configuration'
def test_to_source_file(os_env): env = EnvWrapper(VAR=EnvVar(default='foo'), VAR1=EnvVar(bundle='SETTINGS', default='bar'), VAR2=EnvVar(bundle='SETTINGS', convert=bool, default='1'), FLAG=EnvVar(convert=bool, default='no'), FAKE=EnvVar(proxy='YADA', default='yada')) os_env.update(env.collect()) f = io.StringIO() env.to_source_file(f) assert f.getvalue() == """VAR=foo
def test_collect(): env = EnvWrapper(VAR=EnvVar(), PREFIXED=EnvVar(prefix='FOO_'), BUNDLED=EnvVar(bundle='GROUP'), PROXIED=EnvVar(proxy='YADA')) d = env.collect() expected_os_env = {'VAR', 'FOO_PREFIXED', 'BUNDLED', 'YADA'} assert set(d.keys()) == expected_os_env expected_app_schema = {'VAR', 'PREFIXED', 'GROUP', 'BUNDLED', 'PROXIED'} assert set(env.keys()) == expected_app_schema
def test_default(os_env): os_env.setdefault('ENV', 'production') env = EnvWrapper(ENV=EnvVar(default='testing')) assert env.ENV == 'production' os_env.clear() assert env.ENV == 'testing' os_env['ENV'] = 'integration' assert env.ENV == 'integration' assert env.get('YADA') is None assert env.get('YADA', 'some_value') == 'some_value'
def test_to_config(os_env): env = EnvWrapper(VAR=EnvVar(default='foo'), VAR1=EnvVar(bundle='SETTINGS', default='bar'), VAR2=EnvVar(bundle='SETTINGS', default='baz'), FLAG=EnvVar(convert=bool, default='no')) os_env.update(env.collect()) f = io.StringIO() env.to_config(f, bool_values=('off', 'on'), preserve_case=False, default_section='general') assert f.getvalue() == """[general]
def test_bundle(os_env): env = EnvWrapper(VAR1=EnvVar(bundle='VARS', preprocessor=str.upper), VAR2=EnvVar(bundle='VARS', preprocessor=str.upper)) assert 'VARS' in dir(env) assert 'VARS' in dict(env.items()).keys() assert 'VAR1' in dir(env) assert 'VAR2' in dir(env) os_env['VAR1'] = 'foo' os_env['VAR2'] = 'bar' d = env.VARS assert d == {'var1': 'FOO', 'var2': 'BAR'}
def test_inc_exc_bundle(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool, bundle='SETTINGS'), VAR_1=EnvVar(include_if='FLAG', bundle='SETTINGS', default='foo'), VAR_2=EnvVar(exclude_if='FLAG', bundle='SETTINGS', default='bar')) os_env['FLAG'] = 'on' assert 'SETTINGS' in dir(env) assert 'FLAG' in dir(env) assert 'VAR_1' in dir(env) assert 'VAR_2' not in dir(env) assert 'flag' in env.get('SETTINGS') assert 'var_1' in env.get('SETTINGS') assert 'var_2' not in env.get('SETTINGS') os_env['FLAG'] = 'off' assert 'SETTINGS' in dir(env) assert 'FLAG' in dir(env) assert 'VAR_1' not in dir(env) assert 'VAR_2' in dir(env) assert 'flag' in env.get('SETTINGS') assert 'var_1' not in env.get('SETTINGS') assert 'var_2' in env.get('SETTINGS')
def test_read_from_source_file(): f = io.StringIO("""export foo='yada ' ; export flag= ' on '; export switch='on'; export var1 =' spam'; export var2='eg gs' ; export workers=16; export boost=0.2; garbled: 'sh'pam' """) env = EnvWrapper.from_source_file(f, inline_prefix='export', inline_suffix=';', value_delimiter='\'', bool_values=('off', 'on')) assert all(('FOO' in env, 'FLAG' in env, 'VAR1' in env, 'VAR2' in env)) assert env.FOO == 'yada ' assert env.FLAG == ' on ' assert not isinstance(env.FLAG, bool) assert env.VAR1 == ' spam' assert env.VAR2 == 'eg gs' assert env.SWITCH is True assert env.WORKERS == int('16') assert env.BOOST == float('0.2') assert 'GARBLED' not in env
def test_read_from_json(): f = io.StringIO('{"flag": "false", ' '"settings": {"var1": "bar", "var2": "true"}, ' '"var": "foo"}') env = EnvWrapper.from_json(f) assert all(('FLAG' in env, 'VAR' in env, 'VAR1' in env, 'VAR2' in env)) assert 'SETTINGS' in env
def test_proxy_def(os_env): env = EnvWrapper(VAR1=EnvVar(default='foo', proxy='OS_VAR1')) os_env.setdefault('VAR1', 'yada') os_env.setdefault('OS_VAR1', 'spam') assert env.VAR1 == 'spam' del os_env['OS_VAR1'] assert env.VAR1 == 'foo'
def test_var_bool(os_env): os_env['FLAG'] = 'on' env = EnvWrapper(FLAG=EnvVar(convert=bool)) assert env.FLAG is True os_env['FLAG'] = '0' assert env.FLAG is False
def test_process_float(): env = EnvWrapper( PI=EnvVar(default='31,4', convert=float, preprocessor=lambda s: s.translate(s.maketrans(',', '.')), postprocessor=lambda f: round(f / 10.0, 2))) assert env.PI == float(3.14)
def test_dir_inc_exc(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool, default='on'), FOO=EnvVar(exclude_if='FLAG'), SPAM=EnvVar(include_if='FLAG')) os.environ['FLAG'] = 'on' assert dir(env) == ['FLAG', 'SPAM'] os.environ['FLAG'] = 'off' assert dir(env) == ['FLAG', 'FOO']
def test_exclusion(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool), VAR1=EnvVar(exclude_if='FLAG')) os_env['VAR1'] = 'some_value' os_env['FLAG'] = 'off' assert 'VAR1' in dir(env) os_env['FLAG'] = 'on' assert 'VAR1' not in dir(env)
def test_bundle_inc_exc(os_env): env = EnvWrapper(FLAG=EnvVar(convert=bool, default='on'), FOO=EnvVar(exclude_if='FLAG', bundle='CIRCUS'), SPAM=EnvVar(include_if='FLAG', bundle='CIRCUS')) os.environ['FLAG'] = 'on' assert env.CIRCUS == {'spam': ''} os.environ['FLAG'] = 'off' assert env.CIRCUS == {'foo': ''}
def test_read_from_config(): f = io.StringIO("""[general] var = foo FLAG = off [settings] var1 = bar var2 = baz """) env = EnvWrapper.from_config(f, default_section='general', bool_values=('off', 'on')) assert all(('FLAG' in env, 'VAR' in env, 'VAR1' in env, 'VAR2' in env)) assert 'SETTINGS' in env
def test_to_json(os_env): env = EnvWrapper(VAR=EnvVar(default='foo'), VAR1=EnvVar(bundle='SETTINGS', default='bar'), VAR2=EnvVar(bundle='SETTINGS', convert=bool, default='1'), FLAG=EnvVar(convert=bool, default='no')) os_env.update(env.collect()) f = io.StringIO() env.to_json(f) assert f.getvalue() == '{"var": "foo", "flag": "false", ' \ '"settings": {"var1": "bar", "var2": "true"}}' f = io.StringIO() env.to_json(f, preserve_case=True) assert f.getvalue() == '{"VAR": "foo", "FLAG": "false", ' \ '"SETTINGS": {"VAR1": "bar", "VAR2": "true"}}' f = io.StringIO() env.to_json(f, sort_keys=True) assert f.getvalue() == '{"flag": "false", ' \ '"settings": {"var1": "bar", "var2": "true"}, ' \ '"var": "foo"}'
def test_inc_exc(os_env): env = EnvWrapper(INC=EnvVar(convert=bool), EXC=EnvVar(convert=bool), VAR=EnvVar(include_if='INC', exclude_if='EXC')) os_env['INC'] = 'yes' os_env['EXC'] = 'no' os_env['VAR'] = 'foo' assert 'VAR' in env os_env['EXC'] = 'yes' assert 'VAR1' not in env os_env['EXC'] = 'no' os_env['INC'] = 'no' assert 'VAR1' not in env
def test_read_from_source_file_undelimited_values(): f = io.StringIO(""" foo: yada flag: off var1 :spam garbled: sh'pam var2: eg gs var3: 42 var4: 7.09 """) env = EnvWrapper.from_source_file(f, delimiter=':', bool_values=('off', 'on')) assert all(('FOO' in env, 'FLAG' in env, 'VAR1' in env, 'VAR2' in env)) assert env.VAR2 == 'eg gs' assert env.FLAG is False assert env.GARBLED == 'sh\'pam' assert env.VAR3 == int('42') assert env.VAR4 == float('7.09')
name = "simple_tag_coop" # Environment name, select from: "HalfCheetah-v2", "simple_tag_coop", "simple_spread" n_runs = 5 # Amount of runs to do logpath = "./logs/" # Logging directory logname = "mambpo_test" # Name of log file config_name = "default_mambpo" n_steps = 25*5001 # Setup logging (to .log file) logfile = logpath + logname logging.basicConfig(filename=logpath + logname + ".log", filemode='w', level=logging.DEBUG) logger = logging.getLogger('root') handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.DEBUG) logger.addHandler(handler) # Setup logging (to .p file) data_log = DataLog(logpath, logname) logdata = dict() # Further environment Setup suite = "gym" if name == "HalfCheetah-v2" else "particle" for run in range(n_runs): logger.info("run:" + str(run)) agent_fn = None for n_agent in [4]: env = EnvWrapper(suite, name, n_agents=n_agent, randomized=False) algname = "SAC" par = get_hyperpar(config_name) record_env = EnvWrapper(suite, name, n_agents=n_agent, randomized=True) single_run(env, MAMBPO, data_log, run, agent_kwargs={"hyperpar": par}, n_steps=n_steps, record_env=record_env, name=name+config_name+logname)
def test_prefix(os_env): env = EnvWrapper(VAR=EnvVar(prefix='APP_')) os_env['APP_VAR'] = 'foo' assert 'VAR' in env.keys() assert 'APP_VAR' not in env
def test_var_nocast(os_env): os_env['VAR1'] = 'some_value' env = EnvWrapper(VAR1=EnvVar()) assert env.VAR1 == 'some_value' assert env['VAR1'] == 'some_value' assert env.get('VAR1') == 'some_value'
def test_len_keys_and_dir(): env = EnvWrapper(VAR1=EnvVar(), VAR2=EnvVar()) assert set(env.keys()) == set(dir(env)) == {'VAR1', 'VAR2'} assert len(env) == len(dir(env)) == len(list(env.keys())) == 2
def test_iter_sub_cast_literal_dict(os_env): env = EnvWrapper(VALUES=EnvVar(convert=dict, sub_cast=int)) os.environ['VALUES'] = "{'foo': '1', 'bar': '2', 'spam': '3'}" assert env.VALUES == {'bar': 2, 'foo': 1, 'spam': 3}
def test_iter_sub_cast_literal_tuple(os_env): env = EnvWrapper(VALUES=EnvVar(convert=tuple, sub_cast=int)) os.environ['VALUES'] = "('1', '2', '3')" assert env.VALUES == (1, 2, 3)