Skip to content

Commit 5165ede

Browse files
kmvanbruntanselor
authored andcommitted
Changed History to use OrderDict to support Python 3.6 in non-CPython environments.
1 parent 2397280 commit 5165ede

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

cmd2/cmd2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
InteractiveConsole,
4444
)
4545
from collections import (
46+
OrderedDict,
4647
namedtuple,
4748
)
4849
from contextlib import (
@@ -4359,7 +4360,7 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
43594360
for idx, hi in history.items():
43604361
self.poutput(hi.pr(idx, script=args.script, expanded=args.expanded, verbose=args.verbose))
43614362

4362-
def _get_history(self, args: argparse.Namespace) -> Dict[int, HistoryItem]:
4363+
def _get_history(self, args: argparse.Namespace) -> 'OrderedDict[int, HistoryItem]':
43634364
"""If an argument was supplied, then retrieve partial contents of the history; otherwise retrieve entire history.
43644365
43654366
This function returns a dictionary with history items keyed by their 1-based index in ascending order.

cmd2/history.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"""
55

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

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

223225
return self._build_result_dictionary(start, end)
224226

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

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

278280
def _build_result_dictionary(
279281
self, start: int, end: int, filter_func: Optional[Callable[[HistoryItem], bool]] = None
280-
) -> Dict[int, HistoryItem]:
282+
) -> 'OrderedDict[int, HistoryItem]':
281283
"""
282284
Build history search results
283285
:param start: start index to search from
284286
:param end: end index to stop searching (exclusive)
285287
"""
286-
results: Dict[int, HistoryItem] = dict()
288+
results: OrderedDict[int, HistoryItem] = OrderedDict()
287289
for index in range(start, end):
288290
if filter_func is None or filter_func(self[index]):
289291
results[index + 1] = self[index]

0 commit comments

Comments
 (0)