[docs]classHistoryRetriever:""" Abstract base class that defines the retrieve method signature. Implements _filter_arkouda_command. """def_filter_arkouda_command(self,command:str,filter_string:str="ak")->Optional[str]:""" Return command string. Return 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]:""" Generate list of commands executed. Generate 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 # noqa: DAR202 ------- List[str] A list of commands from the Python shell, Jupyter notebook, or IPython notebook """raiseNotImplementedError("Derived classes must implement retrieve")
[docs]classShellHistoryRetriever(HistoryRetriever):"""Implement 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]:""" Generate list of commands executed. Generate 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 Examples -------- >>> import arkouda as ak >>> from arkouda.history import ShellHistoryRetriever >>> import readline >>> h = ShellHistoryRetriever() >>> readline.clear_history() >>> 1 + 2 3 >>> h.retrieve() [' 1 + 2', 'h.retrieve()'] """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):"""Implement 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]:""" Generate list of commands executed. Generate 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 Examples -------- >>> import arkouda as ak >>> from arkouda.history import NotebookHistoryRetriever >>> h = NotebookHistoryRetriever() >>> 1+2 >>> 4*6 >>> 2**3 >>> h.retrieve(num_commands=3) ['1+2', '4*6', '2**3'] """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)ret=[cmd[2]forcmdinreversed(list(cur))ifisinstance(cmd[2],str)]ifcommand_filter:ret=[cmdforcmdinretifself._filter_arkouda_command(cmd,command_filter)]returnret[-num_to_return:]