Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ambiguous backslashes in regular expression strings #416

Merged
merged 1 commit into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/test_annotation.py
Expand Up @@ -44,7 +44,7 @@ def test_1(self):
target_aux_note = [None] * nannot

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

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

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

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

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

for i in range(0, nannot):
Expand Down
6 changes: 3 additions & 3 deletions wfdb/io/annotation.py
Expand Up @@ -385,7 +385,7 @@ def check_field(self, field):

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


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


Expand Down
10 changes: 5 additions & 5 deletions wfdb/io/record.py
Expand Up @@ -421,7 +421,7 @@ def check_field(self, field, required_channels="all"):
# Record specification fields
elif field == "record_name":
# Allow letters, digits, hyphens, and underscores.
accepted_string = re.match("[-\w]+", self.record_name)
accepted_string = re.match(r"[-\w]+", self.record_name)
if (
not accepted_string
or accepted_string.string != self.record_name
Expand Down Expand Up @@ -461,7 +461,7 @@ def check_field(self, field, required_channels="all"):

if field == "file_name":
# Check for file_name characters
accepted_string = re.match("[-\w]+\.?[\w]+", item[ch])
accepted_string = re.match(r"[-\w]+\.?[\w]+", item[ch])
if (
not accepted_string
or accepted_string.string != item[ch]
Expand Down Expand Up @@ -505,7 +505,7 @@ def check_field(self, field, required_channels="all"):
"baseline values must be between -2147483648 (-2^31) and 2147483647 (2^31 -1)"
)
elif field == "units":
if re.search("\s", item[ch]):
if re.search(r"\s", item[ch]):
raise ValueError(
"units strings may not contain whitespaces."
)
Expand All @@ -520,7 +520,7 @@ def check_field(self, field, required_channels="all"):
"block_size values must be non-negative integers"
)
elif field == "sig_name":
if re.search("\s", item[ch]):
if re.search(r"\s", item[ch]):
raise ValueError(
"sig_name strings may not contain whitespaces."
)
Expand All @@ -534,7 +534,7 @@ def check_field(self, field, required_channels="all"):
# Segment names must be alphanumerics or just a single '~'
if item[ch] == "~":
continue
accepted_string = re.match("[-\w]+", item[ch])
accepted_string = re.match(r"[-\w]+", item[ch])
if (
not accepted_string
or accepted_string.string != item[ch]
Expand Down