From bb1511505004fd94067dcec32fd0071f36a27ad5 Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Fri, 29 Mar 2024 18:10:06 +0330 Subject: [PATCH] use dict comprehension in slob.py and readmdict.py --- pyglossary/plugin_lib/readmdict.py | 15 ++++++++------- pyglossary/slob.py | 10 ++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pyglossary/plugin_lib/readmdict.py b/pyglossary/plugin_lib/readmdict.py index 2ebe96393..50e3edbb3 100644 --- a/pyglossary/plugin_lib/readmdict.py +++ b/pyglossary/plugin_lib/readmdict.py @@ -154,11 +154,10 @@ def _read_int32(f): @staticmethod def _parse_header(header): """Extract attributes from .""" - taglist = re.findall(rb'(\w+)="(.*?)"', header, re.DOTALL) - tagdict = {} - for key, value in taglist: - tagdict[key] = _unescape_entities(value) - return tagdict + return { + key: _unescape_entities(value) + for key, value in re.findall(rb'(\w+)="(.*?)"', header, re.DOTALL) + } def _decode_block(self, block, decompressed_size): # block info: compression, encryption @@ -383,8 +382,10 @@ def _read_header(self): self._stylesheet = {} if header_tag.get("StyleSheet"): lines = header_tag["StyleSheet"].splitlines() - for i in range(0, len(lines), 3): - self._stylesheet[lines[i]] = (lines[i + 1], lines[i + 2]) + self._stylesheet = { + lines[i]: (lines[i + 1], lines[i + 2]) + for i in range(0, len(lines), 3) + } # before version 2.0, number is 4 bytes integer # version 2.0 and above uses 8 bytes diff --git a/pyglossary/slob.py b/pyglossary/slob.py index 377486f39..626f72fcf 100644 --- a/pyglossary/slob.py +++ b/pyglossary/slob.py @@ -592,13 +592,11 @@ def read_header(_file: "MultiFileReader") -> Header: raise UnknownCompression(compression) def read_tags() -> "dict[str, str]": - tags: "dict[str, str]" = {} count = reader.read_byte() - for _ in range(count): - key = reader.read_tiny_text() - value = reader.read_tiny_text() - tags[key] = value - return tags + return { + reader.read_tiny_text(): reader.read_tiny_text() + for _ in range(count) + } tags = read_tags()