Pep corrections

This commit is contained in:
Benjamin Bertrand 2017-04-16 11:36:27 +03:00
parent 28daa4eedb
commit 6df2dc2b01
3 changed files with 177 additions and 90 deletions

View File

@ -2,13 +2,11 @@
# encoding: utf-8 # encoding: utf-8
from .opytex import main from .opytex import main
main() main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -1,42 +1,46 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import os """
import sys Producing then compiling templates
import optparse """
import csv
from path import Path
import os
import csv
import math as m
import optparse
import random as rd
import sys
from path import Path
from pymath.calculus.polynomDeg2 import Polynom_deg2
from pymath import Dataset
from pymath import Equation
from pymath import Expression
from pymath import Fraction
from pymath import Polynom
from pymath import random_pythagore
from pymath import random_str
from pymath import WeightedDataset
from .texenv import texenv from .texenv import texenv
import math as m EXPORT_DICT = {}
import random as rd EXPORT_DICT.update(m.__dict__)
from pymath import Expression EXPORT_DICT.update(rd.__dict__)
from pymath import Polynom EXPORT_DICT.update(__builtins__)
from pymath import Equation EXPORT_DICT.update({
from pymath.calculus.polynomDeg2 import Polynom_deg2 "Expression": Expression,
from pymath import Fraction "Polynom": Polynom,
from pymath import random_str "Polynom_deg2": Polynom_deg2,
from pymath import random_pythagore "Fraction": Fraction,
from pymath import Dataset "Equation": Equation,
from pymath import WeightedDataset "random_str": random_str,
"random_pythagore": random_pythagore,
"Dataset": Dataset,
export_dict = {} "WeightedDataset": WeightedDataset,
export_dict.update(m.__dict__)
export_dict.update(rd.__dict__)
export_dict.update(__builtins__)
export_dict.update({"Expression":Expression,\
"Polynom":Polynom,\
"Polynom_deg2":Polynom_deg2,\
"Fraction":Fraction,\
"Equation":Equation,\
"random_str": random_str,\
"random_pythagore":random_pythagore,\
"Dataset": Dataset, \
"WeightedDataset": WeightedDataset,\
}) })
def produce_and_compile(options): def produce_and_compile(options):
# template = report_renderer.get_template(options.template) # template = report_renderer.get_template(options.template)
template = texenv.get_template(options.template) template = texenv.get_template(options.template)
@ -50,12 +54,15 @@ def produce_and_compile(options):
if not options.csv_file: if not options.csv_file:
# {:02:0f} means that we want a 2 digits number # {:02:0f} means that we want a 2 digits number
list_infos = [{"num": "{:02.0f}".format(i+1)} for i in range(options.num_subj)] list_infos = [
{"num": "{:02.0f}".format(i+1)}
for i in range(options.num_subj)
]
else: else:
with open(options.csv_file, 'r', encoding = 'ISO-8859-1') as f: with open(options.csv_file, 'r', encoding='ISO-8859-1') as csv_file:
list_infos = list(csv.DictReader(f, delimiter=";")) list_infos = list(csv.DictReader(csv_file, delimiter=";"))
for (i,a) in enumerate(list_infos): for (i, info) in enumerate(list_infos):
a['num'] = "{:02.0f}".format(i+1) info['num'] = "{:02.0f}".format(i+1)
if output.dirname() != "": if output.dirname() != "":
output.dirname().cd() output.dirname().cd()
@ -70,8 +77,13 @@ def produce_and_compile(options):
# print("_______" + str(infos)) # print("_______" + str(infos))
dest = Path(str(infos['num']) + output) dest = Path(str(infos['num']) + output)
tmp_pdf.append(dest.namebase + ".pdf") tmp_pdf.append(dest.namebase + ".pdf")
with open( dest, 'w') as f: with open(dest, 'w') as output_file:
f.write(template.render( infos = infos, **export_dict )) output_file.write(
template.render(
infos=infos,
**EXPORT_DICT
)
)
if not options.no_compil: if not options.no_compil:
os.system("pdflatex " + dest) os.system("pdflatex " + dest)
@ -81,46 +93,125 @@ def produce_and_compile(options):
if not options.no_join: if not options.no_join:
print(Path("./").abspath()) print(Path("./").abspath())
print("pdfjam "+ " ".join(tmp_pdf) + " -o all" + Path(output).namebase + ".pdf") print(
os.system("pdfjam "+ " ".join(tmp_pdf) + " -o all" + Path(output).namebase + ".pdf") "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") # os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
print("rm " + " ".join(tmp_pdf)) print("rm " + " ".join(tmp_pdf))
os.system("rm " + " ".join(tmp_pdf)) os.system("rm " + " ".join(tmp_pdf))
if options.corr: if options.corr:
find_subj_tex_files = "find ./ -iname '[0-9]*_*.tex' " find_subj_tex_files = "find ./ -iname '[0-9]*_*.tex' "
os.system( find_subj_tex_files + " -exec sed -i 's/%\\\\printanswers/\\\\printanswers/g' {} \\;") os.system(
find_subj_tex_files
+ " -exec sed -i 's/%\\\\printanswers/\\\\printanswers/g' {} \\;"
)
if not options.no_compil: if not options.no_compil:
os.system(find_subj_tex_files + " -exec pdflatex {} \\;") os.system(
os.system( find_subj_tex_files + " -exec sed -i 's/\\\\printanswers/%\\\\printanswers/g' {} \\;") find_subj_tex_files
+ " -exec pdflatex {} \\;"
)
os.system(
find_subj_tex_files
+ " -exec sed -i 's/\\\\printanswers/%\\\\printanswers/g' {} \\;"
)
if not options.dirty: if not options.dirty:
os.system("rm *.aux *.log") os.system("rm *.aux *.log")
if not options.no_join: if not options.no_join:
print(Path("./").abspath()) print(Path("./").abspath())
print("pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr" + Path(output).namebase + ".pdf") print(
os.system("pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr" + Path(output).namebase + ".pdf") "pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output).namebase + ".pdf"
)
os.system(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output).namebase + ".pdf"
)
# os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf") # os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
print("find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;") print(r"find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
os.system("find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;") os.system(r"find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
cwd.cd() cwd.cd()
def main(): def main():
parser = optparse.OptionParser() 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(
parser.add_option("-C","--csv", action="store", type="string", dest="csv_file", help="Filename of the csv file where informations on subjects are stored") "-t",
parser.add_option("-N","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make") "--template",
parser.add_option("-d","--dirty", action="store_true", dest="dirty", help="Do not clean after compilation") action="store",
parser.add_option("-n","--no-compile", action="store_true", dest="no_compil", help="Do not compile source code") type="string",
parser.add_option("-j","--no-join", action="store_true", dest="no_join", help="Do not join pdf and clean single pdf") dest="template",
parser.add_option("-o","--only-corr", action="store_true",dest="only_corr", help="Create and compile only correction from existing subjects") help="File with the template. The name should have the following form tpl_... ."
parser.add_option("-c","--corr", action="store_true",dest="corr", help="Create and compile correction while making subjects") )
parser.add_option(
"-C",
"--csv",
action="store",
type="string",
dest="csv_file",
help="Filename of the csv file where informations on subjects 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"
)
parser.add_option(
"-o",
"--only-corr",
action="store_true",
dest="only_corr",
help="Create and compile only correction from existing subjects"
)
parser.add_option(
"-c",
"--corr",
action="store_true",
dest="corr",
help="Create and compile correction while making subjects"
)
(options, _) = parser.parse_args()
(options, args) = parser.parse_args()
if not options.template: if not options.template:
print("I need a template!") print("I need a template!")
@ -128,11 +219,11 @@ def main():
produce_and_compile(options) produce_and_compile(options)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -1,14 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import jinja2, os import os
import jinja2
__all__ = ["texenv"] __all__ = ["texenv"]
# Definition of jinja syntax for latex # Definition of jinja syntax for latex
texenv = jinja2.Environment( texenv = jinja2.Environment(
block_start_string='\Block{', 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='}', block_end_string='}',
variable_start_string='\Var{', variable_start_string='\Var{',
variable_end_string='}', variable_end_string='}',
@ -18,8 +18,8 @@ texenv = jinja2.Environment(
extensions=['jinja2.ext.do'] extensions=['jinja2.ext.do']
) )
# Filters
# Filters
def do_calculus(steps, name="A", sep="=", end="", joining=" \\\\ \n"): def do_calculus(steps, name="A", sep="=", end="", joining=" \\\\ \n"):
"""Display properly the calculus """Display properly the calculus
@ -32,9 +32,14 @@ def do_calculus(steps, name = "A", sep = "=", end = "", joining = " \\\\ \n"):
""" """
ans = joining.join([name + " & " + sep + " & " + str(s) + end for s in steps]) ans = joining.join([
name + " & "
+ sep + " & "
+ str(s) + end for s in steps
])
return ans return ans
texenv.filters['calculus'] = do_calculus texenv.filters['calculus'] = do_calculus
from random import shuffle from random import shuffle
@ -42,13 +47,6 @@ 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' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4: