Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Richer run command output #735

Closed
3 of 5 tasks
germa89 opened this issue Nov 26, 2021 · 2 comments · Fixed by #791
Closed
3 of 5 tasks

Richer run command output #735

germa89 opened this issue Nov 26, 2021 · 2 comments · Fixed by #791
Assignees

Comments

@germa89
Copy link
Collaborator

germa89 commented Nov 26, 2021

Current situation

Commands like ELIST, NLIST, etc. returns a string, which is fine. Other commands such as PRNSOL 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

Example

class CommandOutput():
    
    def __repr__(self): 
        # Used when ``>>> CommandOutput``
        return self.cmd_output 

    def __str__(self):
        # Used when ``>>> print(CommandOutput())``
        
        return self.cmd_output
    
    def __init__(self, cmd, string_):
        self.cmd_output = string_
        self.cmd = cmd

        if cmd in ['ELIST', 'NLIST']:
            #parse_XLIST_command()
            pass

    def parse_XLIST_command(self):
        self.table = np.([[1,2,3],[1,2,3]])


out = 'SELECT       FOR ITEM = ENAM COMPONENT = n  IN RANGE       170 TO        170 STEP          1\n\n        531  ELEMENTS(OF       7891  DEFINED) SELECTED BY  ESEL  COMMAND.'

out = CommandOutput('ESEL', out)
out

This could be used in the output of run:

    def run(self, command, write_to_log=True, **kwargs):
        """Run single APDL command.

        For multiple commands, use ``run_multiline``.

        Parameters
        ----------
        ... 

        """
        # Skipped code
        # ...

        # special returns for certain geometry commands
        short_cmd = parse_to_short_cmd(command)

        response = CommandOutput(command, self._response) #New bit

        # return self._response #Old one
        return response
@germa89 germa89 self-assigned this Nov 26, 2021
@akaszynski
Copy link
Collaborator

Prototype this. I agree that a simple string isn't sufficient sometimes.

@akaszynski
Copy link
Collaborator

akaszynski commented Dec 10, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants