Skip to content

Commit

Permalink
stardict: move some codes to memlist.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 7, 2024
1 parent 35eff94 commit 866ea9c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
35 changes: 35 additions & 0 deletions pyglossary/plugins/stardict/memlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

from typing import (
TYPE_CHECKING,
Any,
)

if TYPE_CHECKING:
from collections.abc import Iterator

__all__ = ["MemSdList"]


class MemSdList:
def __init__(self) -> None:
self._l: list[Any] = []

def append(self, x: Any) -> None:
self._l.append(x)

def __len__(self) -> int:
return len(self._l)

def __iter__(self) -> Iterator[Any]:
return iter(self._l)

def sortKey(self, item: tuple[bytes, Any]) -> tuple[bytes, bytes]: # noqa: PLR6301
return (
item[0].lower(),
item[0],
)

def sort(self) -> None:
self._l.sort(key=self.sortKey)
27 changes: 2 additions & 25 deletions pyglossary/plugins/stardict/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
from time import perf_counter as now
from typing import (
TYPE_CHECKING,
Any,
Literal,
)

if TYPE_CHECKING:
from collections.abc import Callable, Generator, Iterator
from collections.abc import Callable, Generator

from pyglossary.glossary_types import EntryType, GlossaryType
from pyglossary.langs import Lang
Expand All @@ -32,6 +31,7 @@

from pyglossary.core import log
from pyglossary.glossary_utils import Error
from pyglossary.plugins.stardict.memlist import MemSdList
from pyglossary.plugins.stardict.sqlist import IdxSqList, SynSqList
from pyglossary.text_utils import uint32ToBytes, uint64ToBytes

Expand Down Expand Up @@ -59,29 +59,6 @@ def newlinesToBr(text: str) -> str:
return re_newline.sub("<br>", text)


class MemSdList:
def __init__(self) -> None:
self._l: list[Any] = []

def append(self, x: Any) -> None:
self._l.append(x)

def __len__(self) -> int:
return len(self._l)

def __iter__(self) -> Iterator[Any]:
return iter(self._l)

def sortKey(self, item: tuple[bytes, Any]) -> tuple[bytes, bytes]: # noqa: PLR6301
return (
item[0].lower(),
item[0],
)

def sort(self) -> None:
self._l.sort(key=self.sortKey)


class Writer:
_large_file: bool = False
_dictzip: bool = True
Expand Down

0 comments on commit 866ea9c

Please sign in to comment.