diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-02 21:42:01 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-02 21:53:00 +0200 |
commit | e1012e314d6c21ad1b75082a292506cd751a9117 (patch) | |
tree | 4530dfff8cf7545d8395c6400fe5efc29e490c42 /src/ee/ds/__init__.py | |
parent | b6c8c2c103b98097adef2fd18547feb6fd7e1952 (diff) | |
download | ee-python-e1012e314d6c21ad1b75082a292506cd751a9117.tar.gz ee-python-e1012e314d6c21ad1b75082a292506cd751a9117.tar.bz2 ee-python-e1012e314d6c21ad1b75082a292506cd751a9117.tar.xz ee-python-e1012e314d6c21ad1b75082a292506cd751a9117.zip |
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.
Diffstat (limited to 'src/ee/ds/__init__.py')
-rw-r--r-- | src/ee/ds/__init__.py | 20 |
1 files changed, 16 insertions, 4 deletions
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) |