Skip to content

Commit 17f06a5

Browse files
bz2jhermann
authored andcommitted
Use tuple for startswith when parsing comments
Saves a generator comprehension and any() call.
1 parent 7b07952 commit 17f06a5

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/configobj/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,7 @@ def _parse(self, infile):
15281528
maxline = len(infile) - 1
15291529
cur_index = -1
15301530
reset_comment = False
1531+
comment_markers = tuple(self.COMMENT_MARKERS)
15311532

15321533
while cur_index < maxline:
15331534
if reset_comment:
@@ -1536,7 +1537,7 @@ def _parse(self, infile):
15361537
line = infile[cur_index]
15371538
sline = line.strip()
15381539
# do we have anything on the line ?
1539-
if not sline or any(sline.startswith(x) for x in self.COMMENT_MARKERS):
1540+
if not sline or sline.startswith(comment_markers):
15401541
reset_comment = False
15411542
comment_list.append(line)
15421543
continue
@@ -2003,16 +2004,16 @@ def write(self, outfile=None, section=None):
20032004
self.indent_type = DEFAULT_INDENT_TYPE
20042005

20052006
out = []
2006-
comment_markers = [x for x in self.COMMENT_MARKERS]
2007-
comment_marker_default = self.COMMENT_MARKERS[0] + ' '
2007+
comment_markers = tuple(self.COMMENT_MARKERS)
2008+
comment_marker_default = comment_markers[0] + ' '
20082009
if section is None:
20092010
int_val = self.interpolation
20102011
self.interpolation = False
20112012
section = self
20122013
for line in self.initial_comment:
20132014
line = self._decode_element(line)
20142015
stripped_line = line.strip()
2015-
if stripped_line and not any(stripped_line.startswith(x) for x in comment_markers):
2016+
if stripped_line and not stripped_line.startswith(comment_markers):
20162017
line = comment_marker_default + line
20172018
out.append(line)
20182019

@@ -2023,7 +2024,7 @@ def write(self, outfile=None, section=None):
20232024
continue
20242025
for comment_line in section.comments[entry]:
20252026
comment_line = self._decode_element(comment_line.lstrip())
2026-
if comment_line and not any(comment_line.startswith(x) for x in comment_markers):
2027+
if comment_line and not comment_line.startswith(comment_markers):
20272028
comment_line = comment_marker_default + comment_line
20282029
out.append(indent_string + comment_line)
20292030
this_entry = section[entry]
@@ -2048,7 +2049,7 @@ def write(self, outfile=None, section=None):
20482049
for line in self.final_comment:
20492050
line = self._decode_element(line)
20502051
stripped_line = line.strip()
2051-
if stripped_line and not any(stripped_line.startswith(x) for x in comment_markers):
2052+
if stripped_line and not stripped_line.startswith(comment_markers):
20522053
line = comment_marker_default + line
20532054
out.append(line)
20542055
self.interpolation = int_val

0 commit comments

Comments
 (0)