<commit_before># -*- coding: utf-8 -*-
"""
This module implements some simple utility functions.

..  :copyright: (c) 2014 by <NAME>.
    :license: MIT, see License for more details.
"""

import os.path
import pylatex.base_classes
import shutil
import tempfile


_latex_special_chars = {
    '&': r'\&',
    '%': r'\%',
    '$': r'\$',
    '#': r'\#',
    '_': r'\_',
    '{': r'\{',
    '}': r'\}',
    '~': r'\textasciitilde{}',
    '^': r'\^{}',
    '\\': r'\textbackslash{}',
    '\n': r'\\',
    '-': r'{-}',
    '\xA0': '~',  # Non-breaking space
}

_tmp_path = os.path.abspath(
    os.path.join(
        tempfile.gettempdir(),
        "pylatex"
    )
)


def escape_latex(s):
    r"""Escape characters that are special in latex.

    Parameters
    ----------
    s : str
        The string to be escaped.

    Returns
    -------
    str
        The string, with special characters in latex escaped.

    Examples
    --------
    >>> escape_latex("Total cost: $30,000")
    'Total cost: \$30,000'
    >>> escape_latex("Issue #5 occurs in 30% of all cases")
    'Issue \#5 occurs in 30\% of all cases'
    >>> print(escape_latex("Total cost: $30,000"))


    References
    ----------
        * http://tex.stackexchange.com/a/34586/43228
        * http://stackoverflow.com/a/16264094/2570866

    """

    return ''.join(_latex_special_chars.get(c, c) for c in s)


def _merge_packages_into_kwargs(new_packages, kwargs):
    """Merge packages into keyword arguments that were passed to __init__.

    This is mostly useful when a class that adds packages itself can be
    inheritted afterwards.
    """

    if 'packages' in kwargs and kwargs['packages'] is not None:
        kwargs['packages'] = new_packages + kwargs['packages']
    else:
        kwargs['packages'] = new_packages


def fix_filename(path):
    """Fix filenames for use in LaTeX.

    Latex has problems if there are one or more points in the filename, thus
    'abc.def.jpg' will be changed to '{abc.def}.jpg'

    Parameters
    ----------
    filename : str
        The filen name to be changed.

    Returns
    -------
    str
        The new filename.

    Examples
    --------
    >>> fix_filename("foo.bar.pdf")
    '{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.pdf")
    '/etc/local/{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.baz/document.pdf")
    '/etc/local/foo.bar.baz/document.pdf'

    """

    path_parts = path.split('/')
    dir_parts = path_parts[:-1]

    filename = path_parts[-1]
    file_parts = filename.split('.')

    if len(file_parts) > 2:
        filename = '{' + '.'.join(file_parts[0:-1]) + '}.' + file_parts[-1]

    dir_parts.append(filename)
    return '/'.join(dir_parts)


def dumps_list(l, escape=False, token='\n', mapper=None):
    r"""Try to generate a LaTeX string of a list that can contain anything.

    Parameters
    ----------
    l : list
        A list of objects to be converted into a single string.
    escape : bool
        Whether to escape special LaTeX characters in converted text.
    token : str
        The token (default is a newline) to separate objects in the list.
    mapper: callable
        A function that should be called on all entries of the list after
        converting them to a string, for instance bold

    Returns
    -------
    str
        A single LaTeX string.

    Examples
    --------
    >>> dumps_list([r"\textbf{Test}", r"\nth{4}"])
    '\\textbf{Test}\n\\nth{4}'
    >>> print(dumps_list([r"\textbf{Test}", r"\nth{4}"]))
    \textbf{Test}
    \nth{4}
    >>> print(pylatex.utils.dumps_list(["There are", 4, "lights!"]))
    There are
    4
    lights!
    >>> print(dumps_list(["$100%", "True"], escape=True))
    \$100\%
    True
    """

    return token.join(_latex_item_to_string(i, escape, mapper) for i in l)


def _latex_item_to_string(item, escape=False, post_convert=None):
    """Use the render method when possible, otherwise uses str.

    :param item:
    :param escape:

    :type item: object
    :type escape: bool

    :return:
    :rtype: str
    """

    if isinstance(item, pylatex.base_classes.LatexObject):
        s = item.dumps()
    else:
        s = str(item)
        if escape:
            s = escape_latex(s)

    if post_convert:
        return post_convert(s)
    return s


def bold(s):
    r"""Make a string appear bold in LaTeX formatting.

    bold() wraps a given string in the LaTeX command \textbf{}.
<commit_msg>Convert one last function in utils.py to Numpy doc format<commit_after># -*- coding: utf-8 -*-
"""
This module implements some simple utility functions.

..  :copyright: (c) 2014 by <NAME>.
    :license: MIT, see License for more details.
"""

import os.path
import pylatex.base_classes
import shutil
import tempfile


_latex_special_chars = {
    '&': r'\&',
    '%': r'\%',
    '$': r'\$',
    '#': r'\#',
    '_': r'\_',
    '{': r'\{',
    '}': r'\}',
    '~': r'\textasciitilde{}',
    '^': r'\^{}',
    '\\': r'\textbackslash{}',
    '\n': r'\\',
    '-': r'{-}',
    '\xA0': '~',  # Non-breaking space
}

_tmp_path = os.path.abspath(
    os.path.join(
        tempfile.gettempdir(),
        "pylatex"
    )
)


def escape_latex(s):
    r"""Escape characters that are special in latex.

    Parameters
    ----------
    s : str
        The string to be escaped.

    Returns
    -------
    str
        The string, with special characters in latex escaped.

    Examples
    --------
    >>> escape_latex("Total cost: $30,000")
    'Total cost: \$30,000'
    >>> escape_latex("Issue #5 occurs in 30% of all cases")
    'Issue \#5 occurs in 30\% of all cases'
    >>> print(escape_latex("Total cost: $30,000"))


    References
    ----------
        * http://tex.stackexchange.com/a/34586/43228
        * http://stackoverflow.com/a/16264094/2570866

    """

    return ''.join(_latex_special_chars.get(c, c) for c in s)


def _merge_packages_into_kwargs(new_packages, kwargs):
    """Merge packages into keyword arguments that were passed to __init__.

    This is mostly useful when a class that adds packages itself can be
    inheritted afterwards.
    """

    if 'packages' in kwargs and kwargs['packages'] is not None:
        kwargs['packages'] = new_packages + kwargs['packages']
    else:
        kwargs['packages'] = new_packages


def fix_filename(path):
    """Fix filenames for use in LaTeX.

    Latex has problems if there are one or more points in the filename, thus
    'abc.def.jpg' will be changed to '{abc.def}.jpg'

    Parameters
    ----------
    filename : str
        The filen name to be changed.

    Returns
    -------
    str
        The new filename.

    Examples
    --------
    >>> fix_filename("foo.bar.pdf")
    '{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.pdf")
    '/etc/local/{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.baz/document.pdf")
    '/etc/local/foo.bar.baz/document.pdf'

    """

    path_parts = path.split('/')
    dir_parts = path_parts[:-1]

    filename = path_parts[-1]
    file_parts = filename.split('.')

    if len(file_parts) > 2:
        filename = '{' + '.'.join(file_parts[0:-1]) + '}.' + file_parts[-1]

    dir_parts.append(filename)
    return '/'.join(dir_parts)


def dumps_list(l, escape=False, token='\n', mapper=None):
    r"""Try to generate a LaTeX string of a list that can contain anything.

    Parameters
    ----------
    l : list
        A list of objects to be converted into a single string.
    escape : bool
        Whether to escape special LaTeX characters in converted text.
    token : str
        The token (default is a newline) to separate objects in the list.
    mapper: callable
        A function that should be called on all entries of the list after
        converting them to a string, for instance bold

    Returns
    -------
    str
        A single LaTeX string.

    Examples
    --------
    >>> dumps_list([r"\textbf{Test}", r"\nth{4}"])
    '\\textbf{Test}\n\\nth{4}'
    >>> print(dumps_list([r"\textbf{Test}", r"\nth{4}"]))
    \textbf{Test}
    \nth{4}
    >>> print(pylatex.utils.dumps_list(["There are", 4, "lights!"]))
    There are
    4
    lights!
    >>> print(dumps_list(["$100%", "True"], escape=True))
    \$100\%
    True
    """
    # TODO: Mapper should be used in this function directly

    return token.join(_latex_item_to_string(i, escape, mapper) for i in l)


def _latex_item_to_string(item, escape=False, post_convert=None):
    """Use the render method when possible, otherwise uses str.

    Args
    ----
    item: object
        An object that needs to be converted to a string
    escape: bool
        Flag that indicates if escaping is needed
    post_convert: callable
        Function that should be called when returning the stringified object

    Returns
    -------
    str
        Latex
    """

    if isinstance(item, pylatex.base_classes.LatexObject):
        s = item.dumps()
    else:
        s = str(item)
        if escape:
            s = escape_latex(s)

    if post_convert:
        return post_convert(s)
    return s


def bold(s):
    r"""Make a string appear bold in LaTeX formatting.

    bold() wraps a given string in the LaTeX command \textbf{}.

{'fraction_non_alphanumeric': 0.15604922138734617, 'fraction_numerical': 0.015245562452357617, 'mean_word_length': 2.290576854174131, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '867754', 'n_tokens_mistral': 3100, 'n_tokens_neox': 2932, 'n_words': 848}