From e1012e314d6c21ad1b75082a292506cd751a9117 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 2 Aug 2018 21:42:01 +0200 Subject: o Fixing a bad bug and learning some Python: list.insert(index, value) will only insert at index if the list is index long. If not it will just append them. Yay. Much better digikey part data now. Creating a ValueList that automatically expands the list. o Stopping with silly key and value replacements. o Updating tests. --- src/ee/ds/__init__.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/ee/ds') diff --git a/src/ee/ds/__init__.py b/src/ee/ds/__init__.py index 5b4da4d..026ab9b 100644 --- a/src/ee/ds/__init__.py +++ b/src/ee/ds/__init__.py @@ -15,7 +15,6 @@ class ObjectType(object): def __init__(self, name: str): self._name = name self._fields = [] # type: List[str] - self._objects = {} def __eq__(self, o) -> bool: other = o # type: ObjectType @@ -54,11 +53,24 @@ class ObjectType(object): class Object(object): + class ValueList(list): + """An auto-expanding version of list.""" + + def __setitem__(self, index, value): + if index >= len(self): + self.extend([None] * (index + 1 - len(self))) + list.__setitem__(self, index, value) + + def __getitem__(self, index): + if index >= len(self): + self.extend([None] * (index + 1 - len(self))) + return list.__getitem__(self, index) + def __init__(self, ds: "DataSet", ot: ObjectType, key: str): self._ds = ds self._ot = ot self._key = key - self._data = [] + self._data = Object.ValueList() @property def object_type(self): @@ -72,7 +84,7 @@ class Object(object): if self._ds._frozen: raise Exception("This data set is frozen") idx = self._ot.index_of(key, create=True) - self._data.insert(idx, value) + self._data[idx] = value return self @@ -115,7 +127,7 @@ class Object(object): def get(self, key: str) -> Optional[str]: idx = self._ot.index_of(key) - return self._data[idx] if idx is not None and idx < len(self._data) else None + return self._data[idx] if idx is not None else None def get_req(self, key: str) -> str: idx = self._ot.index_of(key) -- cgit v1.2.3