TimeSeriesCore no longer subclasses UserDict

This commit is contained in:
Gourav Kumar 2022-04-10 14:09:24 +05:30
parent 83a8b05802
commit d4bbaf7903

View File

@ -2,7 +2,7 @@ from __future__ import annotations
import datetime import datetime
import inspect import inspect
from collections import UserDict, UserList from collections import UserList
from dataclasses import dataclass from dataclasses import dataclass
from numbers import Number from numbers import Number
from typing import Callable, Iterable, List, Literal, Mapping, Sequence, Type from typing import Callable, Iterable, List, Literal, Mapping, Sequence, Type
@ -208,7 +208,7 @@ class Series(UserList):
return eq return eq
class TimeSeriesCore(UserDict): class TimeSeriesCore:
"""Defines the core building blocks of a TimeSeries object""" """Defines the core building blocks of a TimeSeries object"""
def __init__( def __init__(
@ -237,7 +237,7 @@ class TimeSeriesCore(UserDict):
ts_data = _preprocess_timeseries(ts_data, date_format=date_format) ts_data = _preprocess_timeseries(ts_data, date_format=date_format)
super().__init__(dict(ts_data)) self.data = dict(ts_data)
if len(self.data) != len(ts_data): if len(self.data) != len(ts_data):
print("Warning: The input data contains duplicate dates which have been ignored.") print("Warning: The input data contains duplicate dates which have been ignored.")
self.frequency: Frequency = getattr(AllFrequencies, frequency) self.frequency: Frequency = getattr(AllFrequencies, frequency)
@ -364,9 +364,9 @@ class TimeSeriesCore(UserDict):
raise TypeError(f"Invalid type {repr(type(key).__name__)} for slicing.") raise TypeError(f"Invalid type {repr(type(key).__name__)} for slicing.")
def __setitem__(self, key: str | datetime.datetime, item: Number) -> None: def __setitem__(self, key: str | datetime.datetime, value: Number) -> None:
key = _parse_date(key) key = _parse_date(key)
super().__setitem__(key, item) self.data.update({key: value})
self.data = dict(sorted(self.data.items())) self.data = dict(sorted(self.data.items()))
def __iter__(self): def __iter__(self):
@ -381,9 +381,12 @@ class TimeSeriesCore(UserDict):
self.n += 1 self.n += 1
return key, self.data[key] return key, self.data[key]
def __len__(self):
return len(self.data)
@date_parser(1) @date_parser(1)
def __contains__(self, key: object) -> bool: def __contains__(self, key: object) -> bool:
return super().__contains__(key) return key in self.data
@date_parser(1) @date_parser(1)
def get(self, date: str | datetime.datetime, default=None, closest=None): def get(self, date: str | datetime.datetime, default=None, closest=None):