Commit d112a90b authored by Pierre-Elliott Bécue's avatar Pierre-Elliott Bécue Committed by Jean-Benoist Leger

Cleaning almost all flake8 warnings/suggestions

parent 285fffae
......@@ -21,20 +21,22 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
LaTeX math to Unicode text converter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
flatlatex is a basic converter from LaTeX math to human readable text math using
unicode characters.
flatlatex is a basic converter from LaTeX math to human readable text math
using unicode characters.
Basic example:
>>> import flatlatex
>>> c = flatlatex.converter()
>>> c.convert(
... r'\\forall \\eta>0\\, \\exists n\\in\\mathbb{N}\\, \\forall i>n\\, |u_i-\\mathcal{l}|<\\eta')
... (
... r'\\forall \\eta>0\\, \\exists n\\in\\mathbb{N}\\, \\forall i>n\\,'
... r' |u_i-\\mathcal{l}|<\\eta'
... ))
'∀η>0 ∃n∈ℕ ∀i>n |uᵢ-𝓵|<η'
Commands can be added with LaTeX syntax:
......
......@@ -34,10 +34,10 @@ from .latexfuntypes import LatexSyntaxError
class converter:
"""flatlatex converter class
:attrib allow_zw: boolean which indicate if zero width characters are allowed
(True by default).
:attrib allow_combinings: boolean which indicate if combining characters are allowed
(True by default).
:attrib allow_zw: boolean which indicate if zero width characters are
allowed (True by default).
:attrib allow_combinings: boolean which indicate if combining characters
are allowed (True by default).
"""
def __init__(self, allow_zw=True, allow_combinings=True):
......@@ -45,7 +45,7 @@ class converter:
self.__cmds = {}
# transliteration
for k,v in data.transliterators.items():
for k, v in data.transliterators.items():
self.__cmds[k] = transliterator(v)
# symbols
......@@ -125,7 +125,8 @@ class converter:
for idx in range(len(outvec)):
if outvec[idx][0] == 'oper' and outvec[idx][1] == '^':
if idx + 2 < len(outvec):
if outvec[idx + 2][0] == 'oper' and outvec[idx + 2][1] == '_':
if (outvec[idx + 2][0] == 'oper' and
outvec[idx + 2][1] == '_'):
# we invert ^ and _
(
outvec[idx],
......
......@@ -43,9 +43,9 @@ def parse_one_element(s):
def parse(s):
l = []
ret = []
while len(s) > 0:
m, s = parse_one_element(s)
if not (m[1] == ' '):
l.append(m)
return l
ret.append(m)
return ret
......@@ -4,8 +4,13 @@ from .. import converter
def test_conv1():
c = converter()
r = c.convert(
r'\forall \eta>0\, \exists n\in\mathbb{N}\, \forall i>n\, |u_i-\mathcal{l}|<\eta')
assert r=='∀η>0 ∃n∈ℕ ∀i>n |uᵢ-𝓵|<η'
(
r'\forall \eta>0\, \exists n\in\mathbb{N}\, \forall i>n\, '
r'|u_i-\mathcal{l}|<\eta'
)
)
assert r == '∀η>0 ∃n∈ℕ ∀i>n |uᵢ-𝓵|<η'
def test_conv2():
c = converter()
......@@ -14,6 +19,7 @@ def test_conv2():
r = c.convert(r'\prob(X=k)\,=\,\binom{k}{n}\times p^k(1-p)^{n-k}')
assert r == 'ℙ(X=k) = (n!)/(k!(n-k)!)×pᵏ(1-p)ⁿ⁻ᵏ'
def test_conv3():
c = converter()
c.allow_zw = True
......@@ -23,6 +29,7 @@ def test_conv3():
r = c.convert(r'\frac{8}{9}')
assert r == '8/9'
def test_conv4():
c = converter()
c.allow_combinings = True
......
......@@ -4,14 +4,16 @@ from .. import parser
import ast
def test_known_fracts():
assert type(data.known_fracts) is dict
for k,v in data.known_fracts.items():
for k, v in data.known_fracts.items():
assert type(k) is tuple
assert len(k)==2
assert len(k) == 2
assert all([type(x) is str for x in k])
assert type(v) is str
def test_transliteration_consistancy():
for d in (
data.subscript,
......@@ -24,47 +26,51 @@ def test_transliteration_consistancy():
data.mono,
):
assert type(d) is dict
for k,v in d.items():
for k, v in d.items():
assert type(k) is str
assert type(v) is str
def test_symbols_consistancy():
assert type(data.symbols) is dict
for k,v in data.symbols.items():
for k, v in data.symbols.items():
assert type(k) is str
out=parser.parse(k)
assert len(out)==1
out = parser.parse(k)
assert len(out) == 1
assert out[0][0] == 'cmd'
assert type(v) is str
def test_combining_consistancy():
assert type(data.combinings) is dict
for k,v in data.combinings.items():
for k, v in data.combinings.items():
assert type(k) is str
out=parser.parse(k)
assert len(out)==1
out = parser.parse(k)
assert len(out) == 1
assert out[0][0] == 'cmd'
assert type(v) is tuple
assert len(v) == 2
assert all([type(x) is str for x in v])
def test_newcommands_consistancy():
assert type(data.newcommands) is tuple
for k in data.newcommands:
parsed = parser.parse(k)
assert len(parsed) in (3,6)
assert len(parsed) in (3, 6)
assert parsed[0][0] == 'cmd'
assert parsed[0][1] in (r'\newcommand', r'\renewcommand', r'\def')
if len(parsed) == 6:
assert parsed[2] == ('char','[')
assert parsed[4] == ('char',']')
assert parsed[2] == ('char', '[')
assert parsed[4] == ('char', ']')
assert type(parsed[3][0]) is str
a = int(parsed[3][1])
assert a>=0
assert a >= 0
def test_replicated_command():
datasets = [
data.symbols.keys(),
data.symbols.keys(),
data.combinings.keys(),
data.transliterators.keys(),
]
......
from .. import parser
def test_parser():
expected = {
r'\\': [('cmd', r'\\')],
r'\cd': [('cmd', r'\cd')],
r'ab': [('char','a'),('char','b')],
r'e{to{t}o}e': [('char','e'),('subexpr','to{t}o'),('char','e')],
r'g_f': [('char','g'),('oper','_'),('char','f')],
r'h^i': [('char','h'),('oper','^'),('char','i')],
r'\\': [('cmd', r'\\')],
r'\cd': [('cmd', r'\cd')],
r'ab': [('char', 'a'), ('char', 'b')],
r'e{to{t}o}e': [
('char', 'e'), ('subexpr', 'to{t}o'), ('char', 'e')
],
r'g_f': [('char', 'g'), ('oper', '_'), ('char', 'f')],
r'h^i': [('char', 'h'), ('oper', '^'), ('char', 'i')],
}
for k,v in expected.items():
assert all([pki==vi for pki,vi in zip(parser.parse(k),v)]), "parser '%s'" % k
for k1,v1 in expected.items():
for k2,v2 in expected.items():
assert all([pki==vi for pki,vi in zip(parser.parse(k1+' '+k2),v1+v2)]), "parser '%s'+'%s'" % (k1,k2)
for k, v in expected.items():
assert all([
pki == vi
for pki, vi in zip(parser.parse(k), v)
]), "parser '%s'" % k
for k1, v1 in expected.items():
for k2, v2 in expected.items():
assert all([
pki == vi
for pki, vi in zip(parser.parse(k1 + ' ' + k2), v1 + v2)
]), "parser '%s'+'%s'" % (k1, k2)
......@@ -42,4 +42,6 @@ def transliterate(alphabet):
def transliterator(alphabet):
return latexfuntypes.latexfun(lambda x: transliterate(alphabet)(x[0])[0], 1)
return latexfuntypes.latexfun(
lambda x: transliterate(alphabet)(x[0])[0], 1
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment