From 8fdaa25a1ee0db022d49f8154600d4de93013c9a Mon Sep 17 00:00:00 2001 From: gouravkr Date: Sat, 19 Feb 2022 16:47:53 +0530 Subject: [PATCH] handled slicing in getitem Also changed class representation to list[tuple] --- fincal/fincal.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/fincal/fincal.py b/fincal/fincal.py index 03564c9..d846612 100644 --- a/fincal/fincal.py +++ b/fincal/fincal.py @@ -156,13 +156,13 @@ class TimeSeriesCore: printable_data_1 = list(self.time_series)[:3] printable_data_2 = list(self.time_series)[-3:] printable_str = "TimeSeries([{}\n\t...\n\t{}])".format( - ',\n\t'.join([str({i: self.time_series[i]}) for i in printable_data_1]), - ',\n\t'.join([str({i: self.time_series[i]}) for i in printable_data_2]) + ',\n\t'.join([str((i, self.time_series[i])) for i in printable_data_1]), + ',\n\t'.join([str((i, self.time_series[i])) for i in printable_data_2]) ) else: printable_data = self.time_series printable_str = "TimeSeries([{}])".format(',\n\t'.join( - [str({i: self.time_series[i]}) for i in printable_data])) + [str((i, self.time_series[i])) for i in printable_data])) return printable_str def __str__(self): @@ -170,19 +170,25 @@ class TimeSeriesCore: printable_data_1 = list(self.time_series)[:3] printable_data_2 = list(self.time_series)[-3:] printable_str = "[{}\n ...\n {}]".format( - ',\n '.join([str({i: self.time_series[i]}) for i in printable_data_1]), - ',\n '.join([str({i: self.time_series[i]}) for i in printable_data_2]) + ',\n '.join([str((i, self.time_series[i])) for i in printable_data_1]), + ',\n '.join([str((i, self.time_series[i])) for i in printable_data_2]) ) else: printable_data = self.time_series - printable_str = "[{}]".format(',\n '.join([str({i: self.time_series[i]}) for i in printable_data])) + printable_str = "[{}]".format(',\n '.join([str((i, self.time_series[i])) for i in printable_data])) return printable_str def __getitem__(self, n): - keys = list(self.time_series.keys()) - key = keys[n] - item = self.time_series[key] - return key, item + all_keys = list(self.time_series.keys()) + if isinstance(n, int): + keys = [all_keys[n]] + else: + keys = all_keys[n] + item = [(key, self.time_series[key]) for key in keys] + if len(item) == 1: + return item[0] + + return item def __len__(self): return len(self.time_series.keys())