def test_apcustom_narg_tuple_one_base(): parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(1, )) assert arg.nargs == argparse.ONE_OR_MORE assert arg.nargs_range is None assert "arg [...]" in parser.format_help() parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(1, 5)) assert arg.nargs == argparse.ONE_OR_MORE assert arg.nargs_range == (1, 5) assert "arg{1..5}" in parser.format_help()
def test_apcustom_narg_tuple_other_ranges(): # Test range with no upper bound on max parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(2, )) assert arg.nargs == argparse.ONE_OR_MORE assert arg.nargs_range == (2, INFINITY) # Test finite range parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(2, 5)) assert arg.nargs == argparse.ONE_OR_MORE assert arg.nargs_range == (2, 5)
class FilesCommands(CommandSet): """ The STDApi command set. """ def __init__(self, post_job): super().__init__() self.post_job = post_job """ DelFile Command """ del_file_parser = Cmd2ArgumentParser() del_file_parser.add_argument('file', help='the file to delete') @with_argparser(del_file_parser) def do_delfile(self, args: argparse.Namespace): """ Delete a file. """ file = string_to_base64(args.file) self.post_job(f'"name":"delfile","arguments":"{file}","type":1') """ Download Command """ download_parser = Cmd2ArgumentParser() download_parser.add_argument('file', help='the file to download') @with_argparser(download_parser) def do_download(self, args: argparse.Namespace): """ Download a remote file. """ file = string_to_base64(args.file) self.post_job(f'"name":"download","arguments":"{file}","type":2') """ Upload Command """ upload_parser = Cmd2ArgumentParser() upload_parser.add_argument('file', completer=Cmd.path_complete, help='file to upload') upload_parser.add_argument('path', help='path to upload to') @with_argparser(upload_parser) def do_upload(self, args: argparse.Namespace): """ Uploads a file to the remote machine. """ with open(args.file, 'rb') as fd: file = bytes_to_base64(fd.read()) path = string_to_base64(args.path) self.post_job(f'"name":"upload","arguments":"{file},{path}","type":1')
def test_apcustom_print_message(capsys): import sys test_message = 'The test message' # Specify the file parser = Cmd2ArgumentParser() parser._print_message(test_message, file=sys.stdout) out, err = capsys.readouterr() assert test_message in out # Make sure file defaults to sys.stderr parser = Cmd2ArgumentParser() parser._print_message(test_message) out, err = capsys.readouterr() assert test_message in err
class NodeoptionCommand(NNToolShellBase): # nodeoption COMMAND parser_nodeoption = Cmd2ArgumentParser() parser_nodeoption.add_argument('step', nargs=(0, 1), choices_method=nodename_choices_method, help='Set this step number or name') parser_nodeoption.add_argument('parameter', nargs=(0, 1), choices_method=nodeoption_choices_method, help='Set this parameter') parser_nodeoption.add_argument( 'value', nargs=(0, 1), choices_function=nodeoption_value_choices_method, help='Set the parameter to this value') @with_argparser(parser_nodeoption) def do_nodeoption(self, args): """ Allows setting of autotiler generator control parameters and other code generation options such as the location of inputs and outputs. For a complete set of the parameters that can be set refer to the autotiler documentation.""" self._check_graph() if args.step is None or (args.step == '*' and args.parameter is None): for nodeid, elem in self.G.node_options.items(): print("{}: {}".format(nodeid, elem)) return if args.step == '*': nodes = [step['node'] for step in self.G.graph_state.steps] else: try: try: step = int(args.step) nodes = [self.G.graph_state.steps[step]['node']] except ValueError: nodes = [self.G[args.step]] except IndexError: self.perror("%s is not a valid step or node to set %s" % (args.step, args.parameter)) return if args.parameter is None: node_options = self.G.node_options.get(NodeId(nodes[0])) if node_options: print(node_options) else: print("nothing set") return if args.value is None: val = None else: if args.parameter in ("OUT_HOME_MEM_LOC", "OUT_EXEC_MEM_LOC"): val = str(args.value) else: val = int(args.value) for node in nodes: node_options = node.at_options setattr(node_options, args.parameter, val) self.G.node_options[NodeId(node)] = node_options
def get_show_parser(): parser = Cmd2ArgumentParser() parser.add_argument( 'model', nargs='?', choices=['mission', 'user', 'client', 'vuln', 'positive_point', 'negative_point', 'step', 'host', 'impact'], default=None, help=_('The object type to query information about.') ) parser.add_argument( 'ids', nargs='*', type=int, help=_('A list of identifiers (separated by spaces) of specific objects to print information about. If this ' 'list is empty, the program will print every object.') ) parser.add_argument( '-r', '--raw', action='store_true', help=_('Whether to print the data in raw (without formatting) or in a table. Default is to print in a table.') ) return parser
class SSDSetterCommand(NNToolShellBase): # SSD Setter command to set the ssd post process layer parameters parser_ssdsetter = Cmd2ArgumentParser() parser_ssdsetter.add_argument('parameter', choices=('max_bb_before_nms', 'nms_score_threshold'), help='Set this parameter') parser_ssdsetter.add_argument('value', type=int, default=None, help='Set to this value') @with_argparser(parser_ssdsetter) def do_ssd_setter(self, args): assert self.G.has_ssd_postprocess, "No SSD postprocess layer present in the graph" node = [ node for node in self.G.nodes() if isinstance(node, SSDDetectorParameters) ][0] if args.value is None: print('{} is set to {}'.format(args.parameter, getattr(node, args.parameter))) try: setattr(node, args.parameter, args.value) except: raise ValueError('Invalid parameter {}'.format(args.parameter))
class GenProjectCommand(NNToolShellBase): # GEN PROJECT COMMAND parser_gen_proj = Cmd2ArgumentParser() parser_gen_proj.add_argument('project_folder', completer_method=Cmd.path_complete, help='project folder to create or update') parser_gen_proj.add_argument('-o', '--overwrite', action='store_true', help='overwrite existing files') @with_argparser(parser_gen_proj) @no_history def do_gen_project(self, args): """ Generate a project for the current graph including Makefiles, template main file and nntool script. The script will be generated from the command history with open commands excluded and a final save_state command added.""" self._check_graph() self._check_quantized() self._check_adjusted() gen_project(self.G, self.settings, args.project_folder, self._cmd_history[self._graph_idx].copy(), overwrite=args.overwrite, performance=True) self.pfeedback(f'project generated in {args.project_folder}')
class WithCommandSets(Cmd): def __init__(self, command_sets: Optional[Iterable[CommandSet]] = None): super().__init__(command_sets=command_sets) self.sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] def choices_method(self) -> List[str]: """Choices methods are useful when the choice list is based on instance data of your application""" return self.sport_item_strs def choices_completion_error(self) -> List[str]: """ CompletionErrors can be raised if an error occurs while tab completing. Example use cases - Reading a database to retrieve a tab completion data set failed - A previous command line argument that determines the data set being completed is invalid """ if self.debug: return self.sport_item_strs raise CompletionError("debug must be true") # Parser for example command example_parser = Cmd2ArgumentParser(description="Command demonstrating tab completion with argparse\n" "Notice even the flags of this command tab complete") # Tab complete from a list using argparse choices. Set metavar if you don't # want the entire choices list showing in the usage text for this command. example_parser.add_argument('--choices', choices=food_item_strs, metavar="CHOICE", help="tab complete using choices") # Tab complete from choices provided by a choices function and choices method example_parser.add_argument('--choices_function', choices_function=choices_function, help="tab complete using a choices_function") example_parser.add_argument('--choices_method', choices_method=choices_method, help="tab complete using a choices_method") # Tab complete using a completer function and completer method example_parser.add_argument('--completer_function', completer_function=completer_function, help="tab complete using a completer_function") example_parser.add_argument('--completer_method', completer_method=Cmd.path_complete, help="tab complete using a completer_method") # Demonstrate raising a CompletionError while tab completing example_parser.add_argument('--completion_error', choices_method=choices_completion_error, help="raise a CompletionError while tab completing if debug is False") # Demonstrate returning CompletionItems instead of strings example_parser.add_argument('--completion_item', choices_function=choices_completion_item, metavar="ITEM_ID", descriptive_header="Description", help="demonstrate use of CompletionItems") # Demonstrate use of arg_tokens dictionary example_parser.add_argument('--arg_tokens', choices_function=choices_arg_tokens, help="demonstrate use of arg_tokens dictionary") @with_argparser(example_parser) def do_example(self, _: argparse.Namespace) -> None: """The example command""" self.poutput("I do nothing")
def test_apcustom_metavar_tuple(): # Test the case when a tuple metavar is used with nargs an integer > 1 parser = Cmd2ArgumentParser() parser.add_argument('--aflag', nargs=2, metavar=('foo', 'bar'), help='This is a test') assert '[--aflag foo bar]' in parser.format_help()
def test_complete_command_help_no_tokens(ac_app): from cmd2.argparse_completer import AutoCompleter parser = Cmd2ArgumentParser() ac = AutoCompleter(parser, ac_app) completions = ac.complete_subcommand_help(tokens=[], text='', line='', begidx=0, endidx=0) assert not completions
def test_apcustom_no_choices_callables_alongside_choices(kwargs): with pytest.raises(TypeError) as excinfo: parser = Cmd2ArgumentParser() parser.add_argument('name', choices=['my', 'choices', 'list'], **kwargs) assert 'None of the following parameters can be used alongside a choices parameter' in str( excinfo.value)
def test_apcustom_choices_callable_count(kwargs, is_valid): parser = Cmd2ArgumentParser() try: parser.add_argument('name', **kwargs) assert is_valid except ValueError as ex: assert not is_valid assert 'Only one of the following parameters' in str(ex)
class ExecutionCommands(CommandSet): """ The STDApi command set. """ def __init__(self, post_job): super().__init__() self.post_job = post_job """ Shell Command """ shell_parser = Cmd2ArgumentParser() shell_parser.add_argument('command', help='Command to execute') @with_argparser(shell_parser) def do_cmd(self, args: argparse.Namespace): """ Execute a command. """ command = string_to_base64(args.command) self.post_job(f'"name":"cmd","arguments":"{command}","type":1') """ Execute Assembly """ execute_assembly_parser = Cmd2ArgumentParser() execute_assembly_parser.add_argument('assembly', completer=Cmd.path_complete, help='.NET assembly to execute') execute_assembly_parser.add_argument('args', nargs='*', help='arguments to the assembly', default='') @with_argparser(execute_assembly_parser) def do_execute_assembly(self, args: argparse.Namespace): """ Executes a .NET assembly with reflection """ with open(args.assembly, 'rb') as fd: key, file = xor_base64(fd.read()) arg_str = '' for arg in args.args: arg_str = arg_str + ',' + string_to_base64(arg) self.post_job( f'"name":"execute_assembly","arguments":"{key},{file}{arg_str}","type":1' )
class FquantCommand(NNToolShellBase): # FQUANT COMMAND parser_fquant = Cmd2ArgumentParser() parser_fquant.add_argument('-f', '--force_width', choices=STATS_BITS, type=int, default=0, help='force all layers to this bit-width in case of POW2 scheme, ' + 'SQ8 will automatically force 8-bits') parser_fquant.add_argument('-s', '--scheme', type=str, choices=QUANTIZATION_SCHEMES, default='SQ8', help='quantize with scaling factors (TFlite quantization-like) [default] or POW2') parser_fquant.add_argument('--uniform', type=float, default=0.0, help='Use uniform distribution for input with the specified max value') parser_fquant.add_argument('--num_inference', type=int, default=1, help='How many inferences') add_options_to_parser(parser_fquant) @with_argparser(parser_fquant) def do_fquant(self, args: argparse.Namespace): """ Attempt to calculate a fake quantization for graph using random tensors and parameters. This is intended to allow code generation for performance testing even if no real weights and input data are avalaible.""" self._check_graph() opts = get_options_from_args(args) if self.replaying_history and self.history_stats: astats = self.history_stats else: self.G.constant_store.fake = True stats_collector = ActivationRangesCollector() for _ in range(args.num_inference): if args.uniform: input_tensors = [np.random.uniform(-args.uniform, args.uniform, inp.dims.shape) for inp in self.G.input_nodes()] else: input_tensors = [np.random.normal(0, 0.2, inp.dims.shape) for inp in self.G.input_nodes()] stats_collector.collect_stats(self.G, input_tensors) astats = stats_collector.stats self._record_stats(astats) self.G.constant_store.fake = False if args.force_width: opts['bits'] = args.force_width quantizer = UnifiedQuantizer(args.scheme, astats, **opts) # clear the existing quantization self.G.quantization = None qrecs = quantizer.quantize(self.G) self.G.quantization = qrecs RemoveUnnecessaryQuantizeOperators().match(self.G) self.G.add_dimensions() LOG.info("Quantization set. Use qshow command to see it.")
def test_complete_command_no_tokens(ac_app): from cmd2.argparse_completer import ( ArgparseCompleter, ) parser = Cmd2ArgumentParser() ac = ArgparseCompleter(parser, ac_app) completions = ac.complete(text='', line='', begidx=0, endidx=0, tokens=[]) assert not completions
def test_apcustom_narg_tuple_zero_base(): parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(0, )) assert arg.nargs == argparse.ZERO_OR_MORE assert arg.nargs_range is None assert "[arg [...]]" in parser.format_help() parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(0, 1)) assert arg.nargs == argparse.OPTIONAL assert arg.nargs_range is None assert "[arg]" in parser.format_help() parser = Cmd2ArgumentParser() arg = parser.add_argument('arg', nargs=(0, 3)) assert arg.nargs == argparse.ZERO_OR_MORE assert arg.nargs_range == (0, 3) assert "arg{0..3}" in parser.format_help()
class QerrorCommand(NNToolShellBase): # QERROR COMMAND parser_qerror = Cmd2ArgumentParser() parser_qerror.add_argument('-s', '--step', action='store_true', help='evaluate quantization per step. i.e.\ individually quantize each layer') parser_qerror.add_argument('--compare_quantized', action='store_true', help='quantize and dequantize the float output \ to give it the same error as the quantized output of the layer' ) parser_qerror.add_argument( '-r', '--report_lowest', type=int, help='QSNR threshold below which to report filename') table_options(parser_qerror, default_width=140) input_options(parser_qerror) @with_argparser(parser_qerror) @no_history def do_qerror(self, args): """ Show quantization error introduced by processing one or more input files.""" self._check_graph() self._check_quantized() fmt = ('tab' if args.output is None else args.output['fmt']) input_args = self._get_input_args(args) if args.step: stats_collector = StepErrorStatsCollector( quant_compare=args.compare_quantized) else: stats_collector = ErrorStatsCollector( quant_compare=args.compare_quantized) cnt = 0 for file_per_input in glob_input_files(args.input_files, self.G.num_inputs): cnt += 1 data = [ import_data(input_file, **input_args) for input_file in file_per_input ] stat = stats_collector.collect_stats(self.G, data) if args.report_lowest is not None: lowest = min((elem['qsnr'] for elem in stat.values())) if lowest < args.report_lowest: self.pfeedback( "{} had QSNR below threshold".format(file_per_input)) if not cnt: self.perror("no files to process") return tab = ErrorReporter(do_totals=(fmt != "csv"), one_input=cnt <= 1, with_chan=args.step)\ .report(self.G, stats_collector.reduce_stats()) output_table(tab, args)
class AstatsCommand(NNToolShellBase): # ASTATS COMMAND parser_astats = Cmd2ArgumentParser() parser_astats.add_argument('-q', '--qsnr', type=float, default=30.0, help='QSNR threshold') parser_astats.add_argument('-d', '--detail', action="store_true", help='Show fusions detail') parser_astats.add_argument( '-s', '--step', type=int, nargs=(1, 2), help= 'display information by channel for step. You can indicate a fusion step with two values. The step_idx and the idx of the node in the fusion.' ) table_options(parser_astats, default_width=180) input_options(parser_astats) @with_argparser(parser_astats) @no_history def do_astats(self, args: argparse.Namespace): """ Calculate activation statistics on one or more input files.""" self._check_graph() input_args = self._get_input_args(args) stats_collector = ActivationStatsCollector() step_idx = args.step if step_idx is not None: if len(step_idx) == 1: step_idx = step_idx[0] else: step_idx = tuple(step_idx) if len(args.input_files) == 0: self.perror("You must enter some files to process") return for file_per_input in glob_input_files(args.input_files, self.G.num_inputs): LOG.info("input file %s", file_per_input) data = [ import_data(input_file, **input_args) for input_file in file_per_input ] stats_collector.collect_stats(self.G, data) fmt = ('tab' if args.output is None else args.output['fmt']) tab = ActivationReporter(do_totals=(fmt != "csv"), threshold=args.qsnr, yield_fusions=args.detail or isinstance(step_idx, tuple)).report( self.G, stats_collector.reduce_stats()) output_table(tab, args)
def get_upload_parser(): parser = Cmd2ArgumentParser() parser.add_argument( 'file_path', type=str, help=_('The path to the file to upload.') ) return parser
class FusionsCommand(NNToolShellBase): # FUSIONS COMMAND def fusions_list(self): return [elem[0] for elem in get_fusions()] parser_fusions = Cmd2ArgumentParser("apply fusions to graph") parser_fustions_exclusive = parser_fusions.add_mutually_exclusive_group() parser_fustions_exclusive.add_argument('-l', '--list', action='store_true', help='list available fusions') parser_fustions_exclusive.add_argument('-a', '--apply', type=str, nargs='+', choices_method=fusions_list, help='apply a fusion') parser_fustions_exclusive.add_argument( '--pow2', action='store_true', help='apply standard fusions for AutoTiler POW2 kernels') parser_fustions_exclusive.add_argument( '--scale8', action='store_true', help='apply standard fusions for AutoTiler SQ8 kernels') @with_argparser(parser_fusions) def do_fusions(self, args): """ Carry out the default set of fusions on the graph""" self._check_graph() if args.list: self.ppaged("\n".join( ["%s - %s" % (name, desc) for name, desc in get_fusions()])) return if args.apply: fusions = [get_fusion(name) for name in args.apply] if not fusions: self.perror('fusion %s not found' % args.apply) return elif args.pow2: fusions = [get_pow2_match_group()] elif args.scale8: fusions = [get_scale8_match_group()] else: self.perror( "No fusion set selected. Nothing to do. Select --pow2 or --scale8." ) return for fusion in fusions: fusion.match(self.G) self.G.add_dimensions() if self.G.quantization and not self.G.quantization.verify_quantization( self.G): self.G.quantization = None
class TokenCommands(CommandSet): """ The token command set. """ def __init__(self, post_job): super().__init__() self.post_job = post_job def do_rev2self(self, _: Statement): """ Drops all impersonated tokens. """ self.post_job(f'"name":"rev2self","arguments":"","type":1') def do_get_token(self, _: Statement): """ Retrieve the identity of the impersonated token. """ self.post_job(f'"name":"get_token","arguments":"","type":1') make_token_parser = Cmd2ArgumentParser() make_token_parser.add_argument('domain', help='The domain to impersonate for') make_token_parser.add_argument('username', help='User to impersonate') make_token_parser.add_argument('password', help='The password of the user') @with_argparser(make_token_parser) def do_make_token(self, args: argparse.Namespace): """ Impersonate a user's token. """ domain_b64 = string_to_base64(args.domain) user_b64 = string_to_base64(args.username) password_b64 = string_to_base64(args.password) self.post_job( f'"name":"make_token","arguments":"{domain_b64},{user_b64},{password_b64}","type":1' ) steal_token_parser = Cmd2ArgumentParser() steal_token_parser.add_argument( 'pid', help='The pid of the process to impersonate.') @with_argparser(steal_token_parser) def do_steal_token(self, args: argparse.Namespace): """ Impersonate a processes token. """ pid_b64 = string_to_base64(args.pid) self.post_job(f'"name":"steal_token","arguments":"{pid_b64}","type":1')
class EnumerationCommands(CommandSet): """ The STDApi command set. """ def __init__(self, post_job): super().__init__() self.post_job = post_job """ PS Command """ def do_ps(self, _: Statement): """ Retrieves current process information. """ self.post_job(f'"name":"ps","arguments":"","type":1') """ Dir Command """ dir_parser = Cmd2ArgumentParser() dir_parser.add_argument('dir', help='the directory to inspect') @with_argparser(dir_parser) def do_dir(self, args: argparse.Namespace): """ List the contents and properties of a directory. """ directory = string_to_base64(args.dir) self.post_job(f'"name":"dir","arguments":"{directory}","type":1') """ Whoami Command """ def do_whoami(self, _: Statement): """ Retrieve the username of the user. """ self.post_job('"name":"whoami","arguments":"","type":1') """ Hostname Command """ def do_hostname(self, _: Statement): """ Retrieve the hostname of the machine. """ self.post_job('"name":"hostname","arguments":"","type":1') """ OS Command """ def do_os(self, _: Statement): """ Retrieve the operating system's product and build information. """ self.post_job(f'"name":"os","arguments":"","type":1')
def make_table_parser() -> Cmd2ArgumentParser: """Create a unique instance of an argparse Argument parser for processing table arguments. NOTE: The two cmd2 argparse decorators require that each parser be unique, even if they are essentially a deep copy of each other. For cases like that, you can create a function to return a unique instance of a parser, which is what is being done here. """ table_parser = Cmd2ArgumentParser() table_item_group = table_parser.add_mutually_exclusive_group() table_item_group.add_argument('-c', '--color', action='store_true', help='Enable color') table_item_group.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid') table_item_group.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid') return table_parser
class ImageFormatCommand(NNToolShellBase): def inputs_choices(self): if self.G is None: return [] return [node.name for node in self.G.inputs()] def format_choices(self): return [fmt.lower() for fmt in ImageFormatParameters.FORMAT_CHANGES] + ['none'] def norm_choices(self): return [fmt.lower() for fmt in ImageFormatParameters.NORMALIZATIONS] + ['none'] # IMAGEFORMAT COMMAND parser_imageformat = Cmd2ArgumentParser( "inserts image format node into graphs") parser_imageformat.add_argument('input_node', choices_method=inputs_choices, help='input node name to format') parser_imageformat.add_argument('image_formatter', choices_method=format_choices, help='input node name to format') parser_imageformat.add_argument('image_normalizer', choices_method=norm_choices, help='input node name to format') @with_argparser(parser_imageformat) def do_imageformat(self, args: argparse.Namespace): """ Add or modify image format options.""" self._check_graph() if args.input_node not in self.G: self.perror("input node not found") return input_node = self.G[args.input_node] out_edges = self.G.out_edges(input_node.name) if len(out_edges) == 1 and isinstance(out_edges[0].to_node, ImageFormatParameters): self.G.changes.image_format(input_node.name, None, None) remove_formatter(self.G, out_edges[0].to_node) self.G.add_dimensions() return if args.image_formatter == "none" and args.image_normalizer == "none": self.pfeedback("no formatting set") self.G.add_dimensions() return self.G.changes.image_format(input_node.name, args.image_formatter, args.image_normalizer) insert_formatter(self.G, input_node, args.image_formatter, args.image_normalizer) self.G.add_dimensions()
def test_single_prefix_char(): from cmd2.argparse_completer import _single_prefix_char parser = Cmd2ArgumentParser(prefix_chars='-+') # Invalid assert not _single_prefix_char('', parser) assert not _single_prefix_char('--', parser) assert not _single_prefix_char('-+', parser) assert not _single_prefix_char('++has space', parser) assert not _single_prefix_char('foo', parser) # Valid assert _single_prefix_char('-', parser) assert _single_prefix_char('+', parser)
class InputResizerCommand(NNToolShellBase): def inputs_choices(self): if self.G is None: return [] return [node.name for node in self.G.inputs()] def op_choices(self): return [fmt.lower() for fmt in ResizerParameters.SUPPORTED_OP_TYPES] + ['none'] # resizer COMMAND parser_resizer = Cmd2ArgumentParser( "inserts image format node into graphs") parser_resizer.add_argument('input_node', choices_method=inputs_choices, help='input node name to format') parser_resizer.add_argument('resize_op', choices_method=op_choices, help='input node name to format') parser_resizer.add_argument('--from_w', type=int, default=None, help='from_width') parser_resizer.add_argument('--from_h', type=int, default=None, help='from_height') @with_argparser(parser_resizer) def do_input_resizer(self, args: argparse.Namespace): """ Add or modify image format options.""" self._check_graph() if args.input_node not in self.G: self.perror("input node not found") return input_node = self.G[args.input_node] out_edge = self.G.out_edges(input_node.name)[0] if args.resize_op == "none": self.pfeedback("no resize operation set") self.G.add_dimensions() return if args.from_w is None or args.from_h is None: self.pfeedback("no from_shape arguments set") return from_shape = (args.from_h, args.from_w) self.G.changes.input_resizer(input_node.name, args.resize_op, from_shape) insert_resizer(self.G, out_edge, args.resize_op, from_shape) self.G.add_dimensions()
def test_looks_like_flag(): from cmd2.argparse_completer import _looks_like_flag parser = Cmd2ArgumentParser() # Does not start like a flag assert not _looks_like_flag('', parser) assert not _looks_like_flag('non-flag', parser) assert not _looks_like_flag('-', parser) assert not _looks_like_flag('--has space', parser) assert not _looks_like_flag('-2', parser) # Does start like a flag assert _looks_like_flag('--', parser) assert _looks_like_flag('-flag', parser) assert _looks_like_flag('--flag', parser)
class TempsCommand(NNToolShellBase): # TEMPS COMMAND parser_temps = Cmd2ArgumentParser() table_options(parser_temps, default_width=140) @with_argparser(parser_temps) def do_temps(self, args): """ Show statistics on activations.""" self._check_graph() fmt = ('tab' if args.output is None else args.output['fmt']) stats_collector = TempsStatsCollector() stats = stats_collector.collect_stats(self.G) tab = TempsReporter(do_totals=(fmt != "csv")).report(self.G, stats) output_table(tab, args)
class RoundingCommand(NNToolShellBase): # ROUNDING COMMAND parser_round = Cmd2ArgumentParser() parser_round.add_argument('round', choices=['on', 'off'], nargs=(0, 1), help='switch rounding on or off') @with_argparser(parser_round) def do_rounding(self, args: argparse.Namespace): """ Switch rounding on and off in quantized calculations.""" if args.round is not None: set_do_rounding(args.round == 'on') LOG.info("rounding is %s", 'on' if get_do_rounding() else 'off')