Os Command class¶
-
class
os_command_py.os_command.Command(list_cmd, my_env=None, prefix='-', **kwargs)¶ The Command class is a way to launch bash command and mainly gromacs
Parameters: - cmd (list) – command list
- env (dict) – environment variable
- prefix (str, opional, default "-") – argument prefix
Example: >>> # Simple ls command >>> >>> cmd_list = ['ls', '-a', TEST_PATH] >>> cmd_test = Command(list_cmd=cmd_list) >>> cmd_test.display() #doctest: +ELLIPSIS ls -a ...input >>> return_code = cmd_test.run(out_data=True) >>> print(return_code['stdout']) #doctest: +ELLIPSIS . .. 1y0m.pdb volume.xvg <BLANKLINE> >>> # Not Working command: >>> >>> cmd_list = ['ls', '-a', '/NON_EXISTING_FILE'] >>> cmd_test = Command(list_cmd=cmd_list) >>> cmd_test.display() #doctest: +ELLIPSIS ls -a ...NON_EXISTING_FILE >>> try: ... cmd_test.run() ... except RuntimeError: ... print('Command failed') #doctest: +ELLIPSIS The following command could not be executed correctly : ls -a ...NON_EXISTING_FILE Command failed >>> # Word Count >>> cmd_list = ['wc', os.path.join(TEST_PATH,'1y0m.pdb')] >>> cmd_test_2 = Command(list_cmd=cmd_list) >>> cmd_test_2.display() #doctest: +ELLIPSIS wc ...1y0m.pdb >>> return_code = cmd_test_2.run(out_data=True) >>> print('Number of line = {} word = {} char = {}'.format( *return_code['stdout'].split()[:3])) #doctest: +ELLIPSIS Number of line = 1627 word = 18466 char = 13...
-
define_env(my_env)¶ Define the environment of the
Commandobject.Example: >>> print(os.getenv('USELESS')) None >>> cmd_list = ['ls', '-lsh'] >>> cmd_test = Command(list_cmd=cmd_list) >>> cmd_test.display() #doctest: +ELLIPSIS ls -lsh >>> cmd_test.define_env(os.environ.update({'USELESS': 'Changed'})) >>> return_code = cmd_test.run(out_data=True) >>> print(os.getenv('USELESS')) Changed
-
display()¶ Show
Commandobject that will be launch. Show only the name of the command (eg. gmx) instead of the full path. Show relative path for files in the command.
-
display_raw()¶ Show
Commandobject that will be launch. Show the full path of the command as well as the full path for files in the command.
-
run(com_input='', display=False, out_data=False)¶ Launch
Commandobject that will be launch. return programm output is out_data is set to TrueParameters: - com_input (str) – input for the command
- display (bool) – option to display output
- out_data (bool) – option to return output data
Returns: Return Code or output dict
Return type: bool/dict
-
run_background(func_input_dict, com_input='', display=False, out_data=False)¶ Launch
Commandobject that will be launch. Will the command is running launch the function using func_input_dict as argument. return programm output is out_data is set to TrueParameters: - function (function) – function to be launch
- func_input_dict (dict) – input for the function
- com_input (str) – input for the command
- display (bool) – option to display output
- out_data (bool) – option to return output data
Returns: Return Code or output dict
Return type: bool/dict
Example:
>>> import time >>> >>> # Create the function that will run while the command is running >>> def function_iter(proc, dict): ... while proc.poll() is None: ... time.sleep(dict['refresh_time']) ... print('X', end='') >>> >>> monitor = {'function': function_iter, ... 'refresh_time': 0.1} >>> >>> # Create the command >>> cmd_list = ['sleep', '1'] >>> background_test = Command(list_cmd=cmd_list) >>> background_test.display() #doctest: +ELLIPSIS sleep 1 >>> return_code = background_test.run_background(monitor, ... display=True) #doctest: +ELLIPSIS XXXXXX...sleep 1 None <BLANKLINE> <BLANKLINE>