Closed germa89 closed 2 years ago
Prototype this. I agree that a simple string isn't sufficient sometimes.
Also, thinking about this more, provided that we can maintain backwards compatibility, being able to represent for jupyter notebooks would be really powerful as well.
I'm thinking something like this for returning the command output as different array types.
The point of this is, always return string, but allow output as native python types (array, list, pandas, etc.)
class CommandOutput():
def __init__(self, cmd, string_):
self._cmd_output = string_
self._cmd = cmd
self._str_parse = None
def __repr__(self):
# consider adding ipython representation
return self._cmd_output
def __str__(self):
return self._cmd_output
def _parse(self):
"""Prettify the command output into a more readable string
Something along the lines of: https://docs.python.org/3/library/pprint.html
"""
if _cmd in ['ELIST', 'NLIST']:
# parse_xlist_command()
pass
def _parse_xlist_command(self):
return # parse ...
def as_array(self):
"""Return the command output to a numpy array"""
if self._cmd in ['PRNSOL']:
# do something
else:
raise TypeError("This command output cannot be converted to a ``numpy.ndarray``")
return array
def as_dataframe(self):
"""Return the command output to a ``pandas.DataFrame`` if available."""
if self._cmd in ['PRNSOL']:
# do something
else:
raise TypeError("This command output cannot be converted to a ``pandas.dataframe``")
return df
Current situation
Commands like
ELIST
,NLIST
, etc. returns a string, which is fine. Other commands such asPRNSOL
and similar, returns also a string.This is the default behavior.
Proposition
I believe that output from the commands should be "richer". For example, we should parse commands like
ELIST
to output a table.To not break compatibility, we could make an object (
CommandOutput
?) which its representation (__repr__
) is the command output, hence we could keep the current code.Progress
CommandOutput
implementation. Situation: Almost ready. https://github.com/pyansys/pymapdl/pull/791CommandOutputListing
. Features to list, numpy and dataframe conversion for listing commands. https://github.com/pyansys/pymapdl/pull/816FLIST
andDLIST
. Blocker for #741.ELIST
andNLIST
. Important requested feature, difficult implementation because of the main scenarios available.Example
This could be used in the output of
run
: