Skip to content

Commit

Permalink
modify the usage of Argparser as suggested by Carl.
Browse files Browse the repository at this point in the history
  • Loading branch information
xjjiang committed Jan 15, 2025
1 parent 4bbce12 commit 8c20e6e
Showing 1 changed file with 102 additions and 103 deletions.
205 changes: 102 additions & 103 deletions aviary/docs/user_guide/aviary_commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -184,37 +181,37 @@
"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",
"glue_variable(av.EquationsOfMotion.SOLVED_2DOF.value, md_code=False, display=True)\n",
"\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"
]
},
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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"
]
},
{
Expand Down

0 comments on commit 8c20e6e

Please sign in to comment.