Skip to content

Commit

Permalink
small refactoring in SqEntryList
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 6, 2024
1 parent f37c4d4 commit ea2f0d7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pyglossary/sq_entry_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,18 @@ def setSortKey(
def __len__(self) -> int:
return self._len

def _encode(self, entry: EntryType) -> bytes:
return dumps(self._entryToRaw(entry))

def _decode(self, data: bytes) -> EntryType:
return self._entryFromRaw(loads(data))

def append(self, entry: EntryType) -> None:
self._cur.execute(
f"insert into data({self._columnNames}, pickle)"
f" values (?{', ?' * len(self._sqliteSortKey)})",
[col[2](entry.l_word) for col in self._sqliteSortKey]
+ [dumps(self._entryToRaw(entry), protocol=PICKLE_PROTOCOL)],
+ [self._encode(entry)],
)
self._len += 1

Expand Down Expand Up @@ -215,6 +221,5 @@ def __iter__(self) -> Iterator[EntryType]:
raise Error("SQLite cursor is closed")
query = f"SELECT pickle FROM data ORDER BY {self._orderBy}"
self._cur.execute(query)
entryFromRaw = self._entryFromRaw
for row in self._cur:
yield entryFromRaw(loads(row[0]))
yield self._decode(row[0])

0 comments on commit ea2f0d7

Please sign in to comment.