def test_parse_compound_arglist1(): argv = "run with a=17 b=1 -m localhost:22222".split() args = parse_args(['test_prog.py'] + argv) expected = parse_args(['test_prog.py'], print_help=False) expected['COMMAND'] = 'run' expected['with'] = True expected['UPDATE'] = ['a=17', 'b=1'] expected['--mongo_db'] = 'localhost:22222' assert args == expected
def test_parse_compound_arglist2(): argv = "evaluate with a=18 b=2 -l30".split() args = parse_args(['test_prog.py'] + argv) expected = parse_args(['test_prog.py'], print_help=False) expected['COMMAND'] = 'evaluate' expected['with'] = True expected['UPDATE'] = ['a=18', 'b=2'] expected['--loglevel'] = '30' assert args == expected
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :returns: the Run object corresponding to the finished run :rtype: sacred.run.Run """ if argv is None: argv = sys.argv all_commands = self.gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) cmd_name = args.get('COMMAND') or self.default_command try: return self.run_command(cmd_name, config_updates, named_configs, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
def generate_config(zealot, env): # check for user-scope config file user_config_path = os.path.join(os.path.expanduser('~'), '.zealot.yaml') if (os.path.exists(user_config_path)): zealot.add_config(user_config_path) # check for command-line config args = arg_parser.parse_args(sys.argv) conf_updates = arg_parser.get_config_updates(args['UPDATE']) # capture env variables sys_vars = {} for k, v in os.environ.items(): try: assert_is_valid_key(k) sys_vars[k] = v except KeyError: pass # need to be a valid identifier env.add_config({'sys_variables': sys_vars}) # create dict config = {} for c in zealot.configurations: if isinstance(c, ConfigScope): config.update(c.__call__()) else: config.update(c._conf) config.update(conf_updates[0]) return config
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ if argv is None: argv = sys.argv elif isinstance(argv, basestring): argv = shlex.split(argv) else: if not isinstance(argv, (list, tuple)): raise ValueError("argv must be str or list, but was {}".format( type(argv))) if not all([isinstance(a, basestring) for a in argv]): problems = [a for a in argv if not isinstance(a, basestring)] raise ValueError("argv must be list of str but contained the " "following elements: {}".format(problems)) all_commands = self.gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) cmd_name = args.get('COMMAND') or self.default_command try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace() exit(1)
def test_parse_compound_arglist1(): argv = "run with a=17 b=1 -m localhost:22222".split() args = parse_args(['test_prog.py'] + argv) expected = { '--help': False, '--mongo_db': 'localhost:22222', '--logging': None, '--debug': False, 'COMMAND': 'run', 'UPDATE': ['a=17', 'b=1'], 'help': False, 'with': True } assert args == expected
def test_parse_compound_arglist2(): argv = "evaluate with a=18 b=2 -l30".split() args = parse_args(['test_prog.py'] + argv) expected = { '--help': False, '--mongo_db': None, '--logging': '30', '--debug': False, 'COMMAND': 'evaluate', 'UPDATE': ['a=18', 'b=2'], 'help': False, 'with': True } assert args == expected
def get_default_options(self): """Get a dictionary of default options as used with run. Returns ------- dict A dictionary containing option keys of the form '--beat_interval'. Their values are boolean if the option is a flag, otherwise None or its default value. """ all_commands = self.gather_commands() args = parse_args(["dummy"], description=self.doc, commands=OrderedDict(all_commands)) return {k: v for k, v in args.items() if k.startswith('--')}
def test_parse_individual_arguments(argv, expected): args = parse_args(['test_prog.py'] + argv.split(), print_help=False) plain = { '--help': False, '--mongo_db': None, '--logging': None, 'COMMAND': None, 'UPDATE': [], 'help': False, 'with': False, '--debug': False } plain.update(expected) assert args == plain
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :returns: the Run object corresponding to the finished run :rtype: sacred.run.Run """ if argv is None: argv = sys.argv all_commands = self._gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) loglevel = args.get('--logging') for obs in get_observers(args): if obs not in self.observers: self.observers.append(obs) if args['COMMAND']: cmd_name = args['COMMAND'] else: cmd_name = self.default_command try: return self.run_command(cmd_name, config_updates=config_updates, named_configs_to_use=named_configs, log_level=loglevel) except Exception: if args['--debug']: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :return: The result of the command that was run. """ if argv is None: argv = sys.argv all_commands = self._gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) loglevel = args.get('--logging') for obs in get_observers(args): if obs not in self.observers: self.observers.append(obs) if args['COMMAND']: cmd_name = args['COMMAND'] else: cmd_name = self.default_command try: return self.run_command(cmd_name, config_updates=config_updates, named_configs_to_use=named_configs, loglevel=loglevel) except: if args['--debug']: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
logger = logging.getLogger('treeqn_log') # Sacred setup to support config hierarchies @contextmanager def suppress_stdout(): with open(os.devnull, "w") as devnull: old_stdout = sys.stdout sys.stdout = devnull try: yield finally: sys.stdout = old_stdout parsed_args = dict([x.split("=") for x in parse_args(sys.argv)["UPDATE"]]) if "config" in parsed_args: path = parsed_args["config"] else: path = "./conf/default.yaml" def fetch_parents(current_path, parents=[]): tmp_ex = Experiment('treeqn') tmp_ex.add_config(current_path) with suppress_stdout(): tmp_ex.run("print_config") if tmp_ex.current_run is not None and "parent_config" in tmp_ex.current_run.config: return fetch_parents(tmp_ex.current_run.config["parent_config"], [current_path] + parents) else: return [current_path] + parents
def test_parse_individual_arguments(argv, expected): args = parse_args(['test_prog.py'] + argv.split(), print_help=False) plain = parse_args(['test_prog.py'], print_help=False) plain.update(expected) assert args == plain
from time import time from sacred import Experiment from sacred.arg_parser import parse_args from sacred.observers import SqlObserver from jack import readers from jack import train as jtrain from jack.core.shared_resources import SharedResources from jack.io.embeddings.embeddings import load_embeddings, Embeddings from jack.io.load import loaders from jack.util.vocab import Vocab logger = logging.getLogger(os.path.basename(sys.argv[0])) parsed_args = dict([x.split("=") for x in parse_args(sys.argv)["UPDATE"]]) if "config" in parsed_args: path = parsed_args["config"] else: path = "./conf/jack.yaml" def fetch_parents(current_path, parents=[]): tmp_ex = Experiment('jack') tmp_ex.add_config(current_path) if "parent_config" in tmp_ex.configurations[0]._conf: return fetch_parents(tmp_ex.configurations[0]._conf["parent_config"], [current_path] + parents) else: return [current_path] + parents configs = fetch_parents(path)