Skip to content
Snippets Groups Projects
Verified Commit 671b1e12 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT: Cleaned up the code a bit.

parent 5ab0bb6f
No related branches found
No related tags found
1 merge request!130Release v0.14.0
Pipeline #47108 canceled
......@@ -23,48 +23,52 @@ import warnings
def escape_squoted_text(text: str) -> str:
"""The characters ``\\``, ``*`` and ``'`` need to be escaped if used in single quoted
r"""Return an escaped version of the argument.
The characters ``\``, ``*`` and ``'`` need to be escaped if used in single quoted
expressions in the query language.
This function returns the given string where the characters ``\\``, ``'`` and ``*`` are
escaped by a ``\\``.
This function returns the given string where the characters ``\``, ``'`` and ``*`` are
escaped by a ``\`` (backslash character).
Parameters
----------
text : str
The text to be escaped
The text to be escaped.
Returns
-------
str
The escaped text
out : str
The escaped text.
"""
return text.replace('\\', r'\\').replace("'", "\\'").replace('*', r'\*')
return text.replace("\\", r"\\").replace("'", r"\'").replace("*", r"\*")
def escape_dquoted_text(text: str) -> str:
"""The characters ``\\``, ``*`` and ``"`` need to be escaped if used in double quoted
r"""Return an escaped version of the argument.
The characters ``\``, ``*`` and ``"`` need to be escaped if used in double quoted
expressions in the query language.
This function returns the given string where the characters ``\\``, ``"`` and ``*`` are
escaped by a ``\\``.
This function returns the given string where the characters ``\``, ``"`` and ``*`` are
escaped by a ``\`` (backslash character).
Parameters
----------
text : str
The text to be escaped
The text to be escaped.
Returns
-------
str
The escaped text
out : str
The escaped text.
"""
return text.replace('\\', r'\\').replace('"', '\\"').replace('*', r'\*')
return text.replace("\\", r"\\").replace('"', r"\"").replace("*", r"\*")
def escape_quoted_text(text: str) -> str:
"""
Please use escape_squoted_text or escape_dquoted_text
Please use escape_squoted_text or escape_dquoted_text instead of this function.
"""
warnings.warn("Please use escape_squoted_text or escape_dquoted_text", DeprecationWarning)
return escape_squoted_text(text)
......@@ -38,9 +38,9 @@ def test_xml2str():
def test_escape_quoted_text():
assert escape_quoted_text("bla") == "bla"
assert escape_quoted_text("bl\\a") == "bl\\\\a"
assert escape_quoted_text("bl*a") == "bl\\*a"
assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla"
assert escape_squoted_text("bl'a") == "bl\\'a"
assert escape_dquoted_text('bl"a') == 'bl\\"a'
assert escape_squoted_text("bla") == "bla"
assert escape_squoted_text(r"bl\a") == r"bl\\a"
assert escape_squoted_text("bl*a") == r"bl\*a"
assert escape_squoted_text(r"bl*ab\\lab\*labla") == r"bl\*ab\\\\lab\\\*labla"
assert escape_squoted_text("bl'a") == r"bl\'a"
assert escape_dquoted_text('bl"a') == r'bl\"a'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment