Source code for isaricanalytics.utils
from __future__ import annotations
__all__ = [
"clean_figure_table",
"strip_html",
"strip_nonstandard_unicode_chars",
]
# -- IMPORTS --
# -- Standard libraries --
import re
import typing
# -- 3rd party libraries --
import pandas
# -- Internal libraries --
[docs]
def strip_html(value: typing.Any) -> str | typing.Any:
""":py:class:`typing.Any` : Strip HTML elements from a value.
Parameters
----------
value : typing.Any
A value.
Returns
-------
str, typing.Any
Either a string stripped of all HTML elements, or the original non-
string value.
Examples
--------
>>> strip_html("<b><i>A value</i></b>")
'A value'
>>> strip_html("<p>This is a paragraph.</p>")
'This is a paragraph.'
""" # noqa : E501
if isinstance(value, str):
return re.sub(r"<.*?>", "", value)
return value
[docs]
def strip_nonstandard_unicode_chars(value: typing.Any) -> str | typing.Any:
""":py:class:`typing.Any` : Strip non-standard Unicode characters from a value.
The non-standard Unicode characters of interest are defined within the
function itself, and are currently limited to the "↳" (U+21B3) character,
but may be extended to include other characters.
Parameters
----------
value : typing.Any
A value.
Returns
-------
str, typing.Any
Either a string stripped of all non-standard Unicode characters, or the
original non- string value.
Examples
--------
>>> strip_nonstandard_unicode_chars("A value with a special Unicode ↳ character")
'A value with a special Unicode character'
>>> strip_nonstandard_unicode_chars("<p>This is a paragraph ending with a special Unicode character ↳.</p>")
'<p>This is a paragraph ending with a special Unicode character .</p>'
""" # noqa : E501
nonstandard_unicode_chars = "↳"
if isinstance(value, str):
return re.sub(rf"[{nonstandard_unicode_chars}]", "", value)
return value
if __name__ == "__main__": # pragma: no cover
# Doctest the module from the project root using
#
# PYTHONPATH="isaricanalytics" python3 -m doctest -v isaricanalytics/utils.py # noqa : E501
#
import doctest
doctest.testmod()