<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Flexible python representation of a symbolic mathematical formula.
Acceptes Presentation MathML, Content MathML (and could also do OpenMath).
Provides sympy representation.
"""
#
# File:   formula.py
# Date:   04-May-12 (creation)
# Author: <NAME> <<EMAIL>>
#


import logging
import operator
import os
import re
import string
import unicodedata
#import subprocess
from copy import deepcopy
from functools import reduce
from xml.sax.saxutils import unescape

import six
import sympy
from lxml import etree
from sympy import latex, sympify
from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.state import Ket
from sympy.printing.latex import LatexPrinter
from sympy.printing.str import StrPrinter

log = logging.getLogger(__name__)

log.warning("Dark code. Needs review before enabling in prod.")

os.environ['PYTHONIOENCODING'] = 'utf-8'

#-----------------------------------------------------------------------------


class dot(sympy.operations.LatticeOp):	 # pylint: disable=invalid-name, no-member
    """my dot product"""
    zero = sympy.Symbol('dotzero')
    identity = sympy.Symbol('dotidentity')


def _print_dot(_self, expr):
    """Print statement used for LatexPrinter"""
    return r'{((%s) \cdot (%s))}' % (expr.args[0], expr.args[1])

LatexPrinter._print_dot = _print_dot  # pylint: disable=protected-access

#-----------------------------------------------------------------------------
# unit vectors (for 8.02)


def _print_hat(_self, expr):
    """Print statement used for LatexPrinter"""
    return '\\hat{%s}' % str(expr.args[0]).lower()

LatexPrinter._print_hat = _print_hat  # pylint: disable=protected-access
StrPrinter._print_hat = _print_hat  # pylint: disable=protected-access

#-----------------------------------------------------------------------------
# helper routines


def to_latex(expr):
    """
    Convert expression to latex mathjax format
    """
    if expr is None:
        return ''
    expr_s = latex(expr)
    expr_s = expr_s.replace(r'\XI', 'XI')	 # workaround for strange greek

    # substitute back into latex form for scripts
    # literally something of the form
    # 'scriptN' becomes '\\mathcal{N}'
    # note: can't use something akin to the _print_hat method above because we
    # sometimes get 'script(N)__B' or more complicated terms
    expr_s = re.sub(
        r'script([a-zA-Z0-9]+)',
        '\\mathcal{\\1}',
        expr_s
    )

    #return '<math>%s{}{}</math>' % (xs[1:-1])
    if expr_s[0] == '$':
        return '[mathjax]%s[/mathjax]<br>' % (expr_s[1:-1])	 # for sympy v6  # xss-lint: disable=python-interpolate-html
    return '[mathjax]%s[/mathjax]<br>' % (expr_s)		# for sympy v7  # xss-lint: disable=python-interpolate-html


def my_evalf(expr, chop=False):
    """
    Enhanced sympy evalf to handle lists of expressions
    and catch eval failures without dropping out.
    """
    if isinstance(expr, list):
        try:
            return [x.evalf(chop=chop) for x in expr]
        except Exception:  # pylint: disable=broad-except
            return expr
    try:
        return expr.evalf(chop=chop)
    except Exception:  # pylint: disable=broad-except<commit_msg>Fixed bad escape character (#24316)<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Flexible python representation of a symbolic mathematical formula.
Acceptes Presentation MathML, Content MathML (and could also do OpenMath).
Provides sympy representation.
"""
#
# File:   formula.py
# Date:   04-May-12 (creation)
# Author: <NAME> <<EMAIL>>
#


import logging
import operator
import os
import re
import string
import unicodedata
#import subprocess
from copy import deepcopy
from functools import reduce
from xml.sax.saxutils import unescape

import six
import sympy
from lxml import etree
from sympy import latex, sympify
from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.state import Ket
from sympy.printing.latex import LatexPrinter
from sympy.printing.str import StrPrinter

log = logging.getLogger(__name__)

log.warning("Dark code. Needs review before enabling in prod.")

os.environ['PYTHONIOENCODING'] = 'utf-8'

#-----------------------------------------------------------------------------


class dot(sympy.operations.LatticeOp):	 # pylint: disable=invalid-name, no-member
    """my dot product"""
    zero = sympy.Symbol('dotzero')
    identity = sympy.Symbol('dotidentity')


def _print_dot(_self, expr):
    """Print statement used for LatexPrinter"""
    return r'{((%s) \cdot (%s))}' % (expr.args[0], expr.args[1])

LatexPrinter._print_dot = _print_dot  # pylint: disable=protected-access

#-----------------------------------------------------------------------------
# unit vectors (for 8.02)


def _print_hat(_self, expr):
    """Print statement used for LatexPrinter"""
    return '\\hat{%s}' % str(expr.args[0]).lower()

LatexPrinter._print_hat = _print_hat  # pylint: disable=protected-access
StrPrinter._print_hat = _print_hat  # pylint: disable=protected-access

#-----------------------------------------------------------------------------
# helper routines


def to_latex(expr):
    """
    Convert expression to latex mathjax format
    """
    if expr is None:
        return ''
    expr_s = latex(expr)
    expr_s = expr_s.replace(r'\XI', 'XI')	 # workaround for strange greek

    # substitute back into latex form for scripts
    # literally something of the form
    # 'scriptN' becomes '\\mathcal{N}'
    # note: can't use something akin to the _print_hat method above because we
    # sometimes get 'script(N)__B' or more complicated terms
    expr_s = re.sub(
        r'script([a-zA-Z0-9]+)',
        r'\\mathcal{\\1}',
        expr_s
    )

    #return '<math>%s{}{}</math>' % (xs[1:-1])
    if expr_s[0] == '$':
        return '[mathjax]%s[/mathjax]<br>' % (expr_s[1:-1])	 # for sympy v6  # xss-lint: disable=python-interpolate-html
    return '[mathjax]%s[/mathjax]<br>' % (expr_s)		# for sympy v7  # xss-lint: disable=python-interpolate-html


def my_evalf(expr, chop=False):
    """
    Enhanced sympy evalf to handle lists of expressions
    and catch eval failures without dropping out.
    """
    if isinstance(expr, list):
        try:
            return [x.evalf(chop=chop) for x in expr]
        except Exception:  # pylint: disable=broad-except
            return expr
    try:
        return expr.evalf(chop=chop)
    except Exception:  # pylint: disable=broad-except
{'fraction_non_alphanumeric': 0.18752893965118073, 'fraction_numerical': 0.007562895508566137, 'mean_word_length': 2.642495784148398, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '805710', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1967, 'n_words': 557}