handled slicing in getitem

Also changed class representation to list[tuple]
This commit is contained in:
Gourav Kumar 2022-02-19 16:47:53 +05:30
parent 2790106790
commit 8fdaa25a1e

View File

@ -156,13 +156,13 @@ class TimeSeriesCore:
printable_data_1 = list(self.time_series)[:3] printable_data_1 = list(self.time_series)[:3]
printable_data_2 = list(self.time_series)[-3:] printable_data_2 = list(self.time_series)[-3:]
printable_str = "TimeSeries([{}\n\t...\n\t{}])".format( 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_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_2])
) )
else: else:
printable_data = self.time_series printable_data = self.time_series
printable_str = "TimeSeries([{}])".format(',\n\t'.join( 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 return printable_str
def __str__(self): def __str__(self):
@ -170,19 +170,25 @@ class TimeSeriesCore:
printable_data_1 = list(self.time_series)[:3] printable_data_1 = list(self.time_series)[:3]
printable_data_2 = list(self.time_series)[-3:] printable_data_2 = list(self.time_series)[-3:]
printable_str = "[{}\n ...\n {}]".format( 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_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_2])
) )
else: else:
printable_data = self.time_series 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 return printable_str
def __getitem__(self, n): def __getitem__(self, n):
keys = list(self.time_series.keys()) all_keys = list(self.time_series.keys())
key = keys[n] if isinstance(n, int):
item = self.time_series[key] keys = [all_keys[n]]
return key, item 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): def __len__(self):
return len(self.time_series.keys()) return len(self.time_series.keys())