[docs]classHistoryRetriever:""" HistoryRetriever is an abstract base class that defines the retrieve method signature and implements _filter_arkouda_command """def_filter_arkouda_command(self,command:str,filter_string:str="ak")->Optional[str]:""" Returns command string if the filter string is in the command and the command is not generate_history. Otherwise, returns None """returncommandif(filter_stringincommandand"generate_history"notincommand)elseNone
[docs]defretrieve(self,command_filter:Optional[str]=None,num_commands:Optional[int]=None)->List[str]:""" Generates list of commands executed within a Python REPL shell, Jupyter notebook, or IPython notebook, with an optional command filter and number of commands to return. Parameters ---------- num_commands : int The number of commands from history to retrieve command_filter : str String containing characters used to select a subset of command history. Returns ------- List[str] A list of commands from the Python shell, Jupyter notebook, or IPython notebook """raiseNotImplementedError("Derived classes must implement retrieve")
[docs]classShellHistoryRetriever(HistoryRetriever):""" ShellHistoryRetriever implements the retrieve method to get command history from the Python REPL shell. """
[docs]defretrieve(self,command_filter:Optional[str]=None,num_commands:Optional[int]=None)->List[str]:""" Generates list of commands executed within the a Python REPL shell, with an optional command filter and number of commands to return. Parameters ---------- num_commands : int The number of commands from history to retrieve command_filter : str String containing characters used to select a subset of command history. Returns ------- List[str] A list of commands from the Python shell, Jupyter notebook, or IPython notebook """length_of_history=readline.get_current_history_length()num_to_return=num_commandsifnum_commandselselength_of_historyifcommand_filter:return[readline.get_history_item(i+1)foriinrange(length_of_history)ifself._filter_arkouda_command(readline.get_history_item(i+1),command_filter)][-num_to_return:]else:return[str(readline.get_history_item(i+1))foriinrange(length_of_history)][-num_to_return:]
[docs]classNotebookHistoryRetriever(HistoryAccessor,HistoryRetriever):""" NotebookHistoryRetriever implements the retrieve method to get command history from a Jupyter notebook or IPython shell. """
[docs]defretrieve(self,command_filter:Optional[str]=None,num_commands:Optional[int]=None)->List[str]:""" Generates list of commands executed within a Jupyter notebook or IPython shell, with an optional command filter and number of commands to return. Parameters ---------- num_commands : int The number of commands from history to retrieve command_filter : str String containing characters used to select a subset of command history. Returns ------- List[str] A list of commands from the Python shell, Jupyter notebook, or IPython notebook """raw=True# HistoryAccessor _run_sql method parameteroutput=False# HistoryAccessor _run_sql method parametern=0# HistoryAccessor _run_sql method parameternum_to_return=num_commandsifnum_commandselse100ifnisNone:cur=self._run_sql("ORDER BY session DESC, line DESC LIMIT ?",(n,),raw=raw,output=output)else:cur=self._run_sql("ORDER BY session DESC, line DESC",(),raw=raw,output=output)ifcommand_filter:return[cmd[2]forcmdinreversed(list(cur))ifself._filter_arkouda_command(cmd[2],command_filter)][-num_to_return:]else:return[cmd[2]forcmdinreversed(list(cur))][-num_to_return:]