def test_user_override(config): apply_user_overrides( config, [('biz', '12'), ('e.f', 'range(4)'), ('g.h.i', 'zip("abc", range(3))')] ) assert config['foo.bar.biz'] == 12.0 assert config['d.e.f'] == [0, 1, 2, 3] assert config['g.h.i'] == {'a': 0, 'b': 1, 'c': 2}
def test_user_override(config): apply_user_overrides(config, [ ('biz', '12'), ('e.f', 'range(4)'), ('g.h.i', 'zip("abc", range(3))'), ]) assert config['foo.bar.biz'] == 12.0 assert config['d.e.f'] == [0, 1, 2, 3] assert config['g.h.i'] == {'a': 0, 'b': 1, 'c': 2}
def test_user_override_bool(config): apply_user_overrides(config, [('def.baz', '1')]) assert config['abc.def.baz'] is True apply_user_overrides(config, [('def.baz', 'True')]) assert config['abc.def.baz'] is True apply_user_overrides(config, [('def.baz', 'False')]) assert config['abc.def.baz'] is False
def test_user_override_bool(config): apply_user_overrides(config, [('def.baz', '1')]) assert config['abc.def.baz'] is True apply_user_overrides(config, [('def.baz', 'True')]) assert config['abc.def.baz'] is True apply_user_overrides(config, [('def.baz', 'False')]) assert config['abc.def.baz'] is False
def test_user_override_str(config): apply_user_overrides(config, [('a.b.c', 'just a string')]) assert config['a.b.c'] == 'just a string'
def test_user_override_int_invalid(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('bar.baz', 'eighteen')])
def test_user_override_int(config): apply_user_overrides(config, [('bar.baz', '18')]) assert config['foo.bar.baz'] == 18
def test_user_override_int(config): apply_user_overrides(config, [('bar.baz', '18')]) assert config['foo.bar.baz'] == 18
def main(argv=None): """Command line interface for running grocery simulations. This function handles command line parsing, configuration setup, and launching simulations. """ parser = argparse.ArgumentParser() version = get_version() parser.add_argument( '--version', '-V', action='version', version=version, help='Show version and exit.') parser.add_argument( '--named', '-n', metavar='GROUP', dest='named_configs', action='append', default=[], help='Use named configuration %(metavar)s.') parser.add_argument( '--set', '-s', nargs=2, metavar=('KEY', 'VALUE'), action='append', default=[], dest='config_overrides', help='Override configuration KEY with VALUE expression.') parser.add_argument( '--factor', '-f', nargs=2, metavar=('KEYS', 'VALUES'), action='append', default=[], dest='factors', help='Add factorial KEYS with VALUES list of value expressions.') parser.add_argument( '--config', '-c', metavar='YAML', type=argparse.FileType('r'), action='append', default=[], dest='config_files', help='Read configuration from YAML file.') parser.add_argument( '--print-config', action='store_true', help='Print configuration and exit.') parser.add_argument( '--print-named', action='store_true', help='Print named configuration groups and exit.') extra_argv = shlex.split(os.environ.get('SB_EXTRA', '')) if argv is None: argv = sys.argv[1:] args = parser.parse_args(extra_argv + argv) if len(args.config_files) > 1 and args.factors: parser.error('argument --factor/-f: not allowed with multiple ' '--config/-c arguments') if args.print_named: print_named(named, sys.stdout) parser.exit() configs = [] try: if args.config_files: named_overrides = named.resolve(*args.named_configs) for config_file in args.config_files: config = named.resolve('default') apply_user_config(config, parse_config_file(config_file)) config.update(named_overrides) apply_user_overrides(config, args.config_overrides) configs.append(config) else: config = named.resolve('default', *args.named_configs) apply_user_overrides(config, args.config_overrides) configs.append(config) factors = parse_user_factors(configs[0], args.factors) except ConfigError as e: parser.error(str(e)) if args.print_config: yaml.safe_dump_all(configs, stream=sys.stdout) parser.exit() try: if len(configs) == 1: config = configs[0] if factors: results = simulate_factors(config, factors, Top, Environment) return check_errors(results) else: simulate(config, Top, Environment) else: results = simulate_many(configs, Top, Environment) return check_errors(results) except KeyboardInterrupt: print("\nInterrupted by user", file=sys.stderr) return 1
def test_user_override_invalid_value(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('baz', '1')])
def test_user_override_type_mismatch(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('d.e.f', 'os.system("clear")')])
def test_user_override_str_int(config): apply_user_overrides(config, [('a.b.c', '123')]) assert config['a.b.c'] == '123'
def test_user_override_str(config): apply_user_overrides(config, [('a.b.c', 'just a string')]) assert config['a.b.c'] == 'just a string'
def test_user_override_int_invalid(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('bar.baz', 'eighteen')])
def test_user_override_str_int(config): apply_user_overrides(config, [('a.b.c', '123')]) assert config['a.b.c'] == '123'
def test_user_override_type_mismatch(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('d.e.f', 'os.system("clear")')])
def test_user_override_invalid_value(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('baz', '1')])
def test_user_override_invalid_key(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('not.a.key', '1')])
def test_user_override_invalid_key(config): with pytest.raises(ConfigError): apply_user_overrides(config, [('not.a.key', '1')])
} parser = ArgumentParser() parser.add_argument( '--set', '-s', nargs=2, metavar=('KEY', 'VALUE'), action='append', default=[], dest='config_overrides', help='Override config KEY with VALUE expression', ) parser.add_argument( '--factor', '-f', nargs=2, metavar=('KEYS', 'VALUES'), action='append', default=[], dest='factors', help='Add multi-factor VALUES for KEY(S)', ) args = parser.parse_args() apply_user_overrides(config, args.config_overrides) factors = parse_user_factors(config, args.factors) if factors: simulate_factors(config, factors, Top) else: simulate(config, Top)
'sim.duration': '7200 s', 'sim.gtkw.file': 'sim.gtkw', 'sim.gtkw.live': False, 'sim.log.enable': True, 'sim.progress.enable': False, 'sim.result.file': 'result.json', 'sim.seed': 1234, 'sim.timescale': 's', 'sim.vcd.dump_file': 'sim.vcd', 'sim.vcd.enable': True, 'sim.vcd.persist': False, 'sim.workspace': 'workspace', } parser = ArgumentParser() parser.add_argument( '--set', '-s', nargs=2, metavar=('KEY', 'VALUE'), action='append', default=[], dest='config_overrides', help='Override config KEY with VALUE expression') parser.add_argument( '--factor', '-f', nargs=2, metavar=('KEYS', 'VALUES'), action='append', default=[], dest='factors', help='Add multi-factor VALUES for KEY(S)') args = parser.parse_args() apply_user_overrides(config, args.config_overrides) factors = parse_user_factors(config, args.factors) if factors: simulate_factors(config, factors, Top) else: simulate(config, Top)