From 671b1e125762d256814a486d09fd229733ea0d8b Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Fri, 9 Feb 2024 09:44:08 +0100 Subject: [PATCH] MAINT: Cleaned up the code a bit. --- src/linkahead/utils/escape.py | 34 +++++++++++++++++++--------------- unittests/test_utils.py | 12 ++++++------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/linkahead/utils/escape.py b/src/linkahead/utils/escape.py index cae9eb9f..d20a07ac 100644 --- a/src/linkahead/utils/escape.py +++ b/src/linkahead/utils/escape.py @@ -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) diff --git a/unittests/test_utils.py b/unittests/test_utils.py index fe4c9d03..9cfa50f7 100644 --- a/unittests/test_utils.py +++ b/unittests/test_utils.py @@ -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' -- GitLab