Skip to content

Commit 9b2d6c3

Browse files
author
Benjamin Moody
committed
Avoid ambiguous backslashes in regular expression strings.
Sequences such as "\s" should be avoided in ordinary Python strings due to the potential for confusion, and will result in warnings in some Python versions. For regular expressions, it's better to use raw strings (r"\s") instead, which make parsing unambiguous without needing to double backslashes. (Since re.compile understands all the ordinary Python backslash sequences, it should be safe to change non-raw regexp strings to raw strings as long as they don't contain any escaped backslashes or quotes.)
1 parent 3c30345 commit 9b2d6c3

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

tests/test_annotation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_1(self):
4444
target_aux_note = [None] * nannot
4545

4646
RXannot = re.compile(
47-
"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
47+
r"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
4848
)
4949

5050
for i in range(0, nannot):
@@ -117,7 +117,7 @@ def test_2(self):
117117
target_aux_note = [None] * nannot
118118

119119
RXannot = re.compile(
120-
"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
120+
r"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
121121
)
122122

123123
for i in range(0, nannot):
@@ -188,7 +188,7 @@ def test_3(self):
188188
target_aux_note = [None] * nannot
189189

190190
RXannot = re.compile(
191-
"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
191+
r"[ \t]*(?P<time>[\[\]\w\.:]+) +(?P<sample>\d+) +(?P<symbol>.) +(?P<subtype>\d+) +(?P<chan>\d+) +(?P<num>\d+)\t?(?P<aux_note>.*)"
192192
)
193193

194194
for i in range(0, nannot):

wfdb/io/annotation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def check_field(self, field):
385385

386386
# Field specific checks
387387
if field == "record_name":
388-
if bool(re.search("[^-\w]", self.record_name)):
388+
if bool(re.search(r"[^-\w]", self.record_name)):
389389
raise ValueError(
390390
"record_name must only comprise of letters, digits, hyphens, and underscores."
391391
)
@@ -2385,9 +2385,9 @@ def update_extra_fields(subtype, chan, num, aux_note, update):
23852385
return subtype, chan, num, aux_note
23862386

23872387

2388-
rx_fs = re.compile("## time resolution: (?P<fs>\d+\.?\d*)")
2388+
rx_fs = re.compile(r"## time resolution: (?P<fs>\d+\.?\d*)")
23892389
rx_custom_label = re.compile(
2390-
"(?P<label_store>\d+) (?P<symbol>\S+) (?P<description>.+)"
2390+
r"(?P<label_store>\d+) (?P<symbol>\S+) (?P<description>.+)"
23912391
)
23922392

23932393

wfdb/io/record.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def check_field(self, field, required_channels="all"):
421421
# Record specification fields
422422
elif field == "record_name":
423423
# Allow letters, digits, hyphens, and underscores.
424-
accepted_string = re.match("[-\w]+", self.record_name)
424+
accepted_string = re.match(r"[-\w]+", self.record_name)
425425
if (
426426
not accepted_string
427427
or accepted_string.string != self.record_name
@@ -461,7 +461,7 @@ def check_field(self, field, required_channels="all"):
461461

462462
if field == "file_name":
463463
# Check for file_name characters
464-
accepted_string = re.match("[-\w]+\.?[\w]+", item[ch])
464+
accepted_string = re.match(r"[-\w]+\.?[\w]+", item[ch])
465465
if (
466466
not accepted_string
467467
or accepted_string.string != item[ch]
@@ -505,7 +505,7 @@ def check_field(self, field, required_channels="all"):
505505
"baseline values must be between -2147483648 (-2^31) and 2147483647 (2^31 -1)"
506506
)
507507
elif field == "units":
508-
if re.search("\s", item[ch]):
508+
if re.search(r"\s", item[ch]):
509509
raise ValueError(
510510
"units strings may not contain whitespaces."
511511
)
@@ -520,7 +520,7 @@ def check_field(self, field, required_channels="all"):
520520
"block_size values must be non-negative integers"
521521
)
522522
elif field == "sig_name":
523-
if re.search("\s", item[ch]):
523+
if re.search(r"\s", item[ch]):
524524
raise ValueError(
525525
"sig_name strings may not contain whitespaces."
526526
)
@@ -534,7 +534,7 @@ def check_field(self, field, required_channels="all"):
534534
# Segment names must be alphanumerics or just a single '~'
535535
if item[ch] == "~":
536536
continue
537-
accepted_string = re.match("[-\w]+", item[ch])
537+
accepted_string = re.match(r"[-\w]+", item[ch])
538538
if (
539539
not accepted_string
540540
or accepted_string.string != item[ch]

0 commit comments

Comments
 (0)