Skip to content

Commit

Permalink
Changed History to use OrderDict to support Python 3.6 in non-CPython…
Browse files Browse the repository at this point in the history
… environments.
  • Loading branch information
kmvanbrunt authored and anselor committed Mar 30, 2021
1 parent 2397280 commit 5165ede
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
InteractiveConsole,
)
from collections import (
OrderedDict,
namedtuple,
)
from contextlib import (
Expand Down Expand Up @@ -4359,7 +4360,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
for idx, hi in history.items():
self.poutput(hi.pr(idx, script=args.script, expanded=args.expanded, verbose=args.verbose))

def _get_history(self, args: argparse.Namespace) -> Dict[int, HistoryItem]:
def _get_history(self, args: argparse.Namespace) -> 'OrderedDict[int, HistoryItem]':
"""If an argument was supplied, then retrieve partial contents of the history; otherwise retrieve entire history.
This function returns a dictionary with history items keyed by their 1-based index in ascending order.
Expand Down
14 changes: 8 additions & 6 deletions cmd2/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"""

import re
from collections import (
OrderedDict,
)
from typing import (
Callable,
Dict,
Optional,
Union,
)
Expand Down Expand Up @@ -173,7 +175,7 @@ def get(self, index: int) -> HistoryItem:
#
spanpattern = re.compile(r'^\s*(?P<start>-?[1-9]\d*)?(?P<separator>:|(\.{2,}))(?P<end>-?[1-9]\d*)?\s*$')

def span(self, span: str, include_persisted: bool = False) -> Dict[int, HistoryItem]:
def span(self, span: str, include_persisted: bool = False) -> 'OrderedDict[int, HistoryItem]':
"""Return a slice of the History list
:param span: string containing an index or a slice
Expand Down Expand Up @@ -222,7 +224,7 @@ def span(self, span: str, include_persisted: bool = False) -> Dict[int, HistoryI

return self._build_result_dictionary(start, end)

def str_search(self, search: str, include_persisted: bool = False) -> Dict[int, HistoryItem]:
def str_search(self, search: str, include_persisted: bool = False) -> 'OrderedDict[int, HistoryItem]':
"""Find history items which contain a given string
:param search: the string to search for
Expand All @@ -241,7 +243,7 @@ def isin(history_item: HistoryItem) -> bool:
start = 0 if include_persisted else self.session_start_index
return self._build_result_dictionary(start, len(self), isin)

def regex_search(self, regex: str, include_persisted: bool = False) -> Dict[int, HistoryItem]:
def regex_search(self, regex: str, include_persisted: bool = False) -> 'OrderedDict[int, HistoryItem]':
"""Find history items which match a given regular expression
:param regex: the regular expression to search for.
Expand Down Expand Up @@ -277,13 +279,13 @@ def truncate(self, max_length: int) -> None:

def _build_result_dictionary(
self, start: int, end: int, filter_func: Optional[Callable[[HistoryItem], bool]] = None
) -> Dict[int, HistoryItem]:
) -> 'OrderedDict[int, HistoryItem]':
"""
Build history search results
:param start: start index to search from
:param end: end index to stop searching (exclusive)
"""
results: Dict[int, HistoryItem] = dict()
results: OrderedDict[int, HistoryItem] = OrderedDict()
for index in range(start, end):
if filter_func is None or filter_func(self[index]):
results[index + 1] = self[index]
Expand Down

0 comments on commit 5165ede

Please sign in to comment.