Module gdess.formatters.strings
Expand source code
import os
import re
def tex_escape(text: str) -> str:
"""Return text with problematic escape sequences parsed for Latex use.
Parameters
----------
text : str
a plain text message
Note
----
This function was copied from the following StackOverflow answer,
<https://stackoverflow.com/a/25875504/10134974>
Returns
-------
str
the message escaped to appear correctly in LaTeX
"""
conv = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
'<': r'\textless{}',
'>': r'\textgreater{}',
}
regex = re.compile('|'.join(re.escape(str(key))
for key
in sorted(conv.keys(), key=lambda item: - len(item))))
return regex.sub(lambda match: conv[match.group()], text)
def append_before_extension(filename: str, text_to_append: str) -> str:
"""Take a filename and add a string to the end before the extension,
with an underscore inserted in between.
Parameters
----------
filename : str
text_to_append : str
Returns
-------
str
filename with the text appended before the extension
"""
return "{0}_{2}{1}".format(*os.path.splitext(filename) + (text_to_append,))
Functions
def append_before_extension(filename: str, text_to_append: str) -> str-
Take a filename and add a string to the end before the extension, with an underscore inserted in between.
Parameters
filename:strtext_to_append:str
Returns
str- filename with the text appended before the extension
Expand source code
def append_before_extension(filename: str, text_to_append: str) -> str: """Take a filename and add a string to the end before the extension, with an underscore inserted in between. Parameters ---------- filename : str text_to_append : str Returns ------- str filename with the text appended before the extension """ return "{0}_{2}{1}".format(*os.path.splitext(filename) + (text_to_append,)) def tex_escape(text: str) -> str-
Return text with problematic escape sequences parsed for Latex use.
Parameters
text:str- a plain text message
Note
This function was copied from the following StackOverflow answer, https://stackoverflow.com/a/25875504/10134974
Returns
str- the message escaped to appear correctly in LaTeX
Expand source code
def tex_escape(text: str) -> str: """Return text with problematic escape sequences parsed for Latex use. Parameters ---------- text : str a plain text message Note ---- This function was copied from the following StackOverflow answer, <https://stackoverflow.com/a/25875504/10134974> Returns ------- str the message escaped to appear correctly in LaTeX """ conv = { '&': r'\&', '%': r'\%', '$': r'\$', '#': r'\#', '_': r'\_', '{': r'\{', '}': r'\}', '~': r'\textasciitilde{}', '^': r'\^{}', '\\': r'\textbackslash{}', '<': r'\textless{}', '>': r'\textgreater{}', } regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key=lambda item: - len(item)))) return regex.sub(lambda match: conv[match.group()], text)