Skip to content

Commit

Permalink
Avoid unnecessary uses of map() (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx authored Feb 5, 2025
1 parent 27e7303 commit af0ff66
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
15 changes: 11 additions & 4 deletions babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,17 @@ def is_identical(self, other: Message) -> bool:
return self.__dict__ == other.__dict__

def clone(self) -> Message:
return Message(*map(copy, (self.id, self.string, self.locations,
self.flags, self.auto_comments,
self.user_comments, self.previous_id,
self.lineno, self.context)))
return Message(
id=copy(self.id),
string=copy(self.string),
locations=copy(self.locations),
flags=copy(self.flags),
auto_comments=copy(self.auto_comments),
user_comments=copy(self.user_comments),
previous_id=copy(self.previous_id),
lineno=self.lineno, # immutable (str/None)
context=self.context, # immutable (str/None)
)

def check(self, catalog: Catalog | None = None) -> list[TranslationError]:
"""Run various validation checks on the message. Some validations
Expand Down
6 changes: 4 additions & 2 deletions babel/messages/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ def _check_positional(results: list[tuple[str, str]]) -> bool:
'and named placeholders')
return bool(positional)

a, b = map(_parse, (format, alternative))
a = _parse(format)
b = _parse(alternative)

if not a:
return

# now check if both strings are positional or named
a_positional, b_positional = map(_check_positional, (a, b))
a_positional = _check_positional(a)
b_positional = _check_positional(b)
if a_positional and not b_positional and not b:
raise TranslationError('placeholders are incompatible')
elif a_positional != b_positional:
Expand Down
2 changes: 1 addition & 1 deletion babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _strip(line: str):
if line.startswith(tag):
return line[len(tag):].strip()
return line
comments[:] = map(_strip, comments)
comments[:] = [_strip(c) for c in comments]


def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion babel/messages/jslexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Token(NamedTuple):
(0x[a-fA-F0-9]+)
)''', re.VERBOSE)),
('jsx_tag', re.compile(r'(?:</?[^>\s]+|/>)', re.I)), # May be mangled in `get_rules`
('operator', re.compile(r'(%s)' % '|'.join(map(re.escape, operators)))),
('operator', re.compile(r'(%s)' % '|'.join(re.escape(op) for op in operators))),
('template_string', re.compile(r'''`(?:[^`\\]*(?:\\.[^`\\]*)*)`''', re.UNICODE)),
('string', re.compile(r'''(
'(?:[^'\\]*(?:\\.[^'\\]*)*)' |
Expand Down
5 changes: 2 additions & 3 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def denormalize(string: str) -> str:
escaped_lines = string.splitlines()
if string.startswith('""'):
escaped_lines = escaped_lines[1:]
lines = map(unescape, escaped_lines)
return ''.join(lines)
return ''.join(unescape(line) for line in escaped_lines)
else:
return unescape(string)

Expand Down Expand Up @@ -144,7 +143,7 @@ def append(self, s: str) -> None:
self._strs.append(s.strip())

def denormalize(self) -> str:
return ''.join(map(unescape, self._strs))
return ''.join(unescape(s) for s in self._strs)

def __bool__(self) -> bool:
return bool(self._strs)
Expand Down
3 changes: 2 additions & 1 deletion babel/plural.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ def compile_relation(self, method, expr, range_list):
if item[0] == item[1]:
rv.append(f"({expr} == {self.compile(item[0])})")
else:
min, max = map(self.compile, item)
min = self.compile(item[0])
max = self.compile(item[1])
rv.append(f"({expr} >= {min} && {expr} <= {max})")
return f"({' || '.join(rv)})"

Expand Down
6 changes: 2 additions & 4 deletions tests/test_localedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import sys
import tempfile
import unittest
from operator import methodcaller

import pytest

Expand Down Expand Up @@ -94,14 +93,13 @@ def test_unique_ids():
all_ids = localedata.locale_identifiers()
assert len(all_ids) == len(set(all_ids))
# Check locale IDs don't collide after lower-case normalization.
lower_case_ids = list(map(methodcaller('lower'), all_ids))
lower_case_ids = [id.lower() for id in all_ids]
assert len(lower_case_ids) == len(set(lower_case_ids))


def test_mixedcased_locale():
for locale in localedata.locale_identifiers():
locale_id = ''.join([
methodcaller(random.choice(['lower', 'upper']))(c) for c in locale])
locale_id = ''.join(c.lower() if random.random() < 0.5 else c.upper() for c in locale)
assert localedata.exists(locale_id)


Expand Down

0 comments on commit af0ff66

Please sign in to comment.