How to satisfy "[FLAKE8 W605] invalid escape sequence '\.'" and string format in the mean time? [duplicate]
Asked Answered
U

1

12

I have an issue in python. My original regex expression is:

f"regex(metrics_api_failure\.prod\.[\w_]+\.{method_name}\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"

(method_name is a local variable) and I got a lint warning:

"[FLAKE8 W605] invalid escape sequence '\.'Arc(W605)" 

Which looks like recommends me to use r as the regex prefix. But if I do:

r"regex(metrics_api_failure\.prod\.[\w_]+\.{method_name}\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"

The {method_name} becomes the string type rather than a passed in variable.

Does anyone have an idea how to solve this dilemma?

Unclassified answered 16/11, 2021 at 3:34 Comment(1)
"Does anyone have an idea how to solve this dilemma?" Did you try using both prefixes?Petunia
W
25

Pass in the expression:

r"regex(metrics_api_failure\.prod\.[\w_]+\." + method_name + r"\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"

Essentially, use Python string concatenation to accomplish the same thing that you were doing with the brackets. Then, r"" type string escaping should work.

or use a raw format string:

rf"regex(metrics_api_failure\.prod\.[\w_]+\.{method_name}\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"
Willie answered 16/11, 2021 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.