package opytex!

This commit is contained in:
lafrite
2015-05-16 10:42:39 +02:00
parent 03ed2b3f12
commit 44a2934c2d
9 changed files with 61 additions and 6 deletions

14
opytex/__init__.py Normal file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env python
# encoding: utf-8
__version__ = "0.1"
from .texenv import texenv
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

15
opytex/__main__.py Normal file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env python
# encoding: utf-8
from .opytex import main
main()
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

38
opytex/lib/pythagore.py Normal file
View File

@@ -0,0 +1,38 @@
#!/usr/bin/env python
# encoding: utf-8
from random import randint
def pythagore_triplet(v_min = 1, v_max = 10):
"""Random pythagore triplet generator
:param v_min: minimum in randint
:param v_max: max in randint
:returns: (a,b,c) such that a^2 + b^2 = c^2
"""
u = randint(v_min,v_max)
v = randint(v_min,v_max)
while v == u:
v = randint(v_min,v_max)
u, v = max(u,v), min(u,v)
return (u**2-v**2 , 2*u*v, u**2 + v**2)
if __name__ == '__main__':
print(pythagore_triplet())
for j in range(1,10):
for i in range(j,10):
print((i**2-j**2 , 2*i*j, i**2 + j**2))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@@ -0,0 +1,33 @@
\Block{macro solveEquation(P)}
On commence par calculer le discriminant de $P(x) = \Var{P}$.
\begin{eqnarray*}
\Delta & = & b^2-4ac \\
\Var{P.delta.explain()|calculus(name="\\Delta")}
\end{eqnarray*}
\Block{if P.delta > 0}
comme $\Delta = \Var{P.delta} > 0$ donc $P$ a deux racines
\begin{eqnarray*}
x_1 & = & \frac{-b - \sqrt{\Delta}}{2a} = \frac{\Var{-P.b} - \sqrt{\Var{P.delta}}}{2 \times \Var{P.a}} = \Var{P.roots()[0] } \\
x_2 & = & \frac{-b + \sqrt{\Delta}}{2a} = \frac{\Var{-P.b} + \sqrt{\Var{P.delta}}}{2 \times \Var{P.a}} = \Var{P.roots()[1] }
\end{eqnarray*}
Les solutions de l'équation $\Var{P} = 0$ sont donc $\mathcal{S} = \left\{ \Var{P.roots()[0]}; \Var{P.roots()[1]} \right\}$
\Block{elif P.delta == 0}
Comme $\Delta = 0$ donc $P$ a une racine
\begin{eqnarray*}
x_1 = \frac{-b}{2a} = \frac{-\Var{P.b}}{2\times \Var{P.a}} = \Var{P.roots()[0]} \\
\end{eqnarray*}
La solution de $\Var{P} = 0$ est donc $\mathcal{S} = \left\{ \Var{P.roots()[0]}\right\}$
\Block{else}
Alors $\Delta = \Var{P.delta} < 0$ donc $P$ n'a pas de racine donc l'équation $\Var{P} = 0$ n'a pas de solution.
\Block{endif}
\Block{endmacro}

112
opytex/opytex.py Executable file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import optparse
import csv
from path import path
from .texenv import texenv
import math as m
import random as rd
from pymath.expression import Expression
from pymath.polynom import Polynom
from pymath.polynomDeg2 import Polynom_deg2
from pymath.fraction import Fraction
from pymath.random_expression import random_str
export_dict = {}
export_dict.update(m.__dict__)
export_dict.update(rd.__dict__)
export_dict.update(__builtins__.__dict__)
export_dict.update({"Expression":Expression,\
"Polynom":Polynom,\
"Polynom_deg2":Polynom_deg2,\
"Fraction":Fraction,\
"random_str": random_str,\
})
def produce_and_compile(options):
#template = report_renderer.get_template(options.template)
template = texenv.get_template(options.template)
# Saving place
cwd = path("./").abspath()
template_file = path(options.template)
if options.output:
output = path(options.output)
else:
# Template should be named tpl_... tpl will replace by the number/name of the version
output = path(template_file.dirname()) / path(template_file.name[3:])
if not options.csv_file:
list_infos = [{"num": i+1} for i in range(options.num_subj)]
else:
with open(options.csv_file, 'r', encoding = 'ISO-8859-1') as f:
list_infos = list(csv.DictReader(f, delimiter=";"))
for (i,a) in enumerate(list_infos):
a['num'] = i+1
if output.dirname() != "":
output.dirname().cd()
output = output.name
tmp_pdf = []
for infos in list_infos:
#print("_______" + str(infos))
dest = path(str(infos['num']) + output)
tmp_pdf.append(dest.namebase + ".pdf")
with open( dest, 'w') as f:
f.write(template.render( infos = infos, **export_dict ))
if not options.no_compil:
os.system("pdflatex " + dest)
if not options.dirty:
os.system("rm *.aux *.log")
if not options.no_join:
print(path("./").abspath())
print("pdfjam "+ " ".join(tmp_pdf) + " -o all" + path(output).namebase + ".pdf")
os.system("pdfjam "+ " ".join(tmp_pdf) + " -o all" + path(output).namebase + ".pdf")
#os.system("pdfjam *.pdf -o all" + path(output).namebase + ".pdf")
print("rm " + " ".join(tmp_pdf))
os.system("rm " + " ".join(tmp_pdf))
cwd.cd()
def main():
parser = optparse.OptionParser()
parser.add_option("-t","--template",action="store",type="string",dest="template", help="File with the template. The name should have the following form tpl_... .")
parser.add_option("-o","--output",action="store",type="string",dest="output",help="Base name for output )")
parser.add_option("-c","--csv", action="store", type="string", dest="csv_file", help="Filename of the csv file where informations are stored")
parser.add_option("-N","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make")
parser.add_option("-d","--dirty", action="store_true", dest="dirty", help="Do not clean after compilation")
parser.add_option("-n","--no-compile", action="store_true", dest="no_compil", help="Do not compile source code")
parser.add_option("-j","--no-join", action="store_true", dest="no_join", help="Do not join pdf and clean single pdf")
(options, args) = parser.parse_args()
if not options.template:
print("I need a template!")
sys.exit(0)
produce_and_compile(options)
if __name__ == '__main__':
main()
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

53
opytex/texenv.py Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env python
# encoding: utf-8
import jinja2, os
__all__ = ["texenv"]
# Definition of jinja syntax for latex
texenv = jinja2.Environment(
block_start_string = '\Block{',
# Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template...
block_end_string = '}',
variable_start_string = '\Var{',
variable_end_string = '}',
loader = jinja2.FileSystemLoader(os.path.abspath('.')),
extensions = ['jinja2.ext.do']
)
# Filters
def do_calculus(steps, name = "A", sep = "=", end = "", joining = " \\\\ \n"):
"""Display properly the calculus
Generate this form string:
"name & sep & a_step end joining"
:param steps: list of steps
:returns: latex string ready to be endbeded
"""
ans = joining.join([name + " & " + sep + " & " + str(s) + end for s in steps])
return ans
texenv.filters['calculus'] = do_calculus
from random import shuffle
texenv.filters['shuffle'] = shuffle
if __name__ == '__main__':
from pymath.expression import Expression
exp = Expression("2/4 + 18")
print(do_calculus(exp.simplify()))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del