As you may know, the DictDB
type doesn’t seem iterable with a for ... in
loop, contrary to a classic Python dict.
Indeed, let’s say I instantiate a DictDB in my IconScoreBase herited class like that :
self._dict = DictDB('DICT', db, value_type=int)
Then, I create an external method for adding items to that DictDB :
@external
def create_item(self, key: str, value: int) -> None:
self._dict[key] = value
Finally, I create a method that reads all the values of such dict, and return it :
@external(readonly=True)
def get_items(self) -> list:
items = []
for item in self._dict:
items.append(item)
return items
The function get_items
won’t work - at least not in tbears, either in Yeouido ; the method never returns any valid result but instead starts an infinite loop.
EDIT: It actually stopped Yeouido from working at 2020-03-01 01:56:51 (UTC+1)
after calling that method. Sorry for that.
My current solution to this problem is the following :
As (key, value) pairs aren’t iterable in DictDB, I keep an ArrayDB next to the DictDB that keeps all keys of that DictDB. As ArrayDB are iterable, I can iterable through my DictDB using all the keys in the ArrayDB.
Link to the actual implementation of my current “IterableDictDB” : scorelib/SCORELib/scorelib/iterable_dict.py at master · iconation/scorelib · GitHub
Full source code of buggy dictDB SCORE : DictDBTesting/IterableDictDB/main.py at master · iconation/DictDBTesting · GitHub