From 8c20e6e45ffb50c4cf142f28c0e1a77b5aa384ef Mon Sep 17 00:00:00 2001 From: Xun Jiang Date: Wed, 15 Jan 2025 13:33:49 -0800 Subject: [PATCH] modify the usage of Argparser as suggested by Carl. --- aviary/docs/user_guide/aviary_commands.ipynb | 205 +++++++++---------- 1 file changed, 102 insertions(+), 103 deletions(-) diff --git a/aviary/docs/user_guide/aviary_commands.ipynb b/aviary/docs/user_guide/aviary_commands.ipynb index 58a565a78..d7cbc6be0 100644 --- a/aviary/docs/user_guide/aviary_commands.ipynb +++ b/aviary/docs/user_guide/aviary_commands.ipynb @@ -144,10 +144,7 @@ "command = 'aviary run_mission ' + str_folder + '/' + str_model\n", "glue_variable(command, md_code=True)\n", "command += ' --max_iter 0 --optimizer IPOPT' # max_iter to limit build time, IPOPT to run on CI\n", - "subprocess.run(command.split()).check_returncode();\n", - "\n", - "\n", - "\n" + "subprocess.run(command.split()).check_returncode();\n" ] }, { @@ -184,29 +181,27 @@ "outputs": [], "source": [ "# Testing Cell\n", + "import argparse\n", "import aviary.api as av\n", - "from aviary.interface.methods_for_level1 import _setup_level1_parser, run_level_1\n", + "from aviary.interface.methods_for_level1 import run_level_1\n", "from aviary.utils.doctape import glue_variable\n", "import inspect\n", "\n", + "current_opt_vars = []\n", + "\n", "# glue all the options of 'aviary run_mission'\n", - "source_code = inspect.getsource(_setup_level1_parser)\n", - "new_line = ''\n", - "for ch in source_code:\n", - " if ch == '\\n':\n", - " new_line = new_line.strip()\n", - " if new_line.startswith('\\\"-') or new_line.startswith(\"'-\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if skey == '-o':\n", - " glue_variable('--outdir', md_code=True)\n", - " glue_variable(skey, md_code=True)\n", - " elif new_line.startswith('\\\"') or new_line.startswith(\"'\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if len(skey) > 1:\n", - " glue_variable(skey, md_code=True)\n", - " new_line = ''\n", - " else:\n", - " new_line += ch\n", + "command = 'run_mission'\n", + "parser = argparse.ArgumentParser()\n", + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n", "\n", "glue_variable(av.EquationsOfMotion.HEIGHT_ENERGY.value, md_code=False, display=True)\n", "glue_variable(av.EquationsOfMotion.TWO_DEGREES_OF_FREEDOM.value, md_code=False, display=True)\n", @@ -214,7 +209,9 @@ "\n", "# obtain the default value of maximum number of iterations from function run_level_1().\n", "max_iter = inspect.signature(run_level_1).parameters['max_iter'].default\n", - "glue_variable('max_iter', str(max_iter), md_code=False)\n", + "if 'max_iter' not in current_opt_vars:\n", + " glue_variable('max_iter', str(max_iter), md_code=False)\n", + " current_opt_vars.append('max_iter')\n", "\n" ] }, @@ -243,31 +240,24 @@ "outputs": [], "source": [ "# Testing Cell\n", - "import aviary.api as av\n", - "from aviary.utils.fortran_to_aviary import _setup_F2A_parser\n", "from aviary.utils.doctape import glue_variable\n", - "import inspect\n", + "\n", + "print(current_opt_vars)\n", "\n", "# glue all the options of 'aviary fortran_to_aviary'\n", - "source_code = inspect.getsource(_setup_F2A_parser)\n", - "new_line = ''\n", - "for ch in source_code:\n", - " if ch == '\\n':\n", - " new_line = new_line.strip()\n", - " if new_line.startswith('\\\"-') or new_line.startswith(\"'-\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if skey != '-o' and skey != '--verbosity':\n", - " glue_variable(skey, md_code=True)\n", - " new_line = ''\n", - " else:\n", - " new_line += ch\n", - "\n", - "# experimenting\n", - "# import argparse\n", - "# parser = argparse.ArgumentParser()\n", - "# _setup_F2A_parser(parser)\n", - "# options = [action.dest for action in parser._actions]\n", - "# print(\"Options:\", options)\n" + "command = 'fortran_to_aviary'\n", + "parser = argparse.ArgumentParser()\n", + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " if opt not in current_opt_vars:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n" ] }, { @@ -347,25 +337,22 @@ "outputs": [], "source": [ "# Testing Cell\n", - "import aviary.api as av\n", - "from aviary.utils.engine_deck_conversion import _setup_EDC_parser\n", "from aviary.utils.doctape import glue_variable\n", - "import inspect\n", "\n", "# glue all the options of 'aviary convert_engine'\n", - "source_code = inspect.getsource(_setup_EDC_parser)\n", - "new_line = ''\n", - "for ch in source_code:\n", - " if ch == '\\n':\n", - " new_line = new_line.strip()\n", - " if new_line.startswith('\\\"-') or new_line.startswith(\"'-\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if skey != '-o':\n", - " glue_variable(skey, md_code=True)\n", - " new_line = ''\n", - " else:\n", - " new_line += ch\n", - "\n" + "command = 'convert_engine'\n", + "parser = argparse.ArgumentParser()\n", + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " if opt not in current_opt_vars:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n" ] }, { @@ -455,31 +442,23 @@ "outputs": [], "source": [ "# Testing Cell\n", - "import aviary.api as av\n", - "from aviary.utils.aero_table_conversion import _setup_ATC_parser\n", - "from aviary.utils.doctape import check_contains\n", - "import inspect\n", "import argparse\n", "\n", - "# glue all the options of 'aviary fortran_to_aviary'\n", - "source_code = inspect.getsource(_setup_ATC_parser)\n", - "new_line = ''\n", - "options = []\n", - "for ch in source_code:\n", - " if ch == '\\n':\n", - " new_line = new_line.strip()\n", - " if new_line.startswith('\\\"-') or new_line.startswith(\"'-\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " options.append(skey)\n", - " new_line = ''\n", - " else:\n", - " new_line += ch\n", - "\n", + "# glue all the options of 'aviary convert_aero_table'\n", + "command = 'convert_aero_table'\n", "parser = argparse.ArgumentParser()\n", - "_setup_ATC_parser(parser)\n", - "options2 = [action.dest for action in parser._actions]\n", - "options.append(options2)\n", - "check_contains(['-f', '--data_format'], options)\n" + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " if opt not in current_opt_vars:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n", + "\n" ] }, { @@ -623,6 +602,32 @@ "!aviary convert_prop_table -h" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "# glue all the options of 'aviary convert_prop_table'\n", + "command = 'convert_prop_table'\n", + "parser = argparse.ArgumentParser()\n", + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " if opt not in current_opt_vars:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -663,28 +668,22 @@ "outputs": [], "source": [ "# Testing Cell\n", - "import aviary.api as av\n", - "from aviary.visualization.dashboard import _dashboard_setup_parser\n", "from aviary.utils.doctape import glue_variable\n", - "import inspect\n", "\n", - "# glue all the options of 'aviary run_mission'\n", - "source_code = inspect.getsource(_dashboard_setup_parser)\n", - "new_line = ''\n", - "for ch in source_code:\n", - " if ch == '\\n':\n", - " new_line = new_line.strip()\n", - " if new_line.startswith('\\\"-') or new_line.startswith(\"'-\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if skey != '-d' and skey != '--force':\n", - " glue_variable(skey, md_code=True)\n", - " elif new_line.startswith('\\\"') or new_line.startswith(\"'\"):\n", - " skey = new_line.split(',')[0][1:-1]\n", - " if len(skey) > 1:\n", - " glue_variable(skey, md_code=True)\n", - " new_line = ''\n", - " else:\n", - " new_line += ch\n" + "# glue all the options of 'aviary dashboard'\n", + "command = 'dashboard'\n", + "parser = argparse.ArgumentParser()\n", + "_command_map[command][0](parser)\n", + "actions = [*parser._get_optional_actions(),*parser._get_positional_actions()]\n", + "for action in actions:\n", + " opt_list = action.option_strings\n", + " for opt in opt_list:\n", + " if opt not in current_opt_vars:\n", + " glue_variable(opt, md_code=True)\n", + " current_opt_vars.append(opt)\n", + " if action.dest not in current_opt_vars:\n", + " glue_variable(action.dest, md_code=True)\n", + " current_opt_vars.append(action.dest)\n" ] }, {