Skip to content

Commit

Permalink
use dict comprehension in slob.py and readmdict.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Mar 29, 2024
1 parent 103a119 commit bb15115
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 8 additions & 7 deletions pyglossary/plugin_lib/readmdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ def _read_int32(f):
@staticmethod
def _parse_header(header):
"""Extract attributes from <Dict attr="value" ... >."""
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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions pyglossary/slob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit bb15115

Please sign in to comment.