From 6e1e6fdb59c644001d84dd4b5bee6ab0f9bff87c Mon Sep 17 00:00:00 2001 From: utensil Date: Tue, 2 Apr 2024 12:19:57 +0800 Subject: [PATCH] lint --- galgebra/gprinter.py | 126 +++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/galgebra/gprinter.py b/galgebra/gprinter.py index 1352b5b8..ce6912bb 100644 --- a/galgebra/gprinter.py +++ b/galgebra/gprinter.py @@ -1,14 +1,12 @@ -from galgebra.printer import GaLatexPrinter, isinteractive, Format_cnt, latex -import subprocess,sys,shutil +from .printer import GaLatexPrinter, isinteractive, latex +import subprocess +import sys +import shutil from sympy import init_printing try: - from IPython.display import display, Latex, Math, display_latex -except ImportError: - pass -try: - from sympy.interactive import printing + from IPython.display import display, Math except ImportError: pass @@ -17,19 +15,16 @@ 'win32': {'rm': 'del', 'evince': 'start', 'null': ' > NUL', '&': ''}, 'darwin': {'rm': 'rm', 'evince': 'open', 'null': ' > /dev/null', '&': '&'}} -class LaTeX: - -#LaTeX data - line_sep = \ -""" +class LaTeX: + # LaTeX data + line_sep = """ ************************************************************************ """ latex_flg = False latex_str = '' - latex_preamble = \ -""" + latex_preamble = """ \\pagestyle{empty} \\usepackage[latin1]{inputenc} \\usepackage{amsmath} @@ -78,32 +73,33 @@ class LaTeX: """ ip_cmds = \ -[r'$$\DeclareMathOperator{\Tr}{Tr}$$',\ -r'$$\DeclareMathOperator{\Adj}{Adj}$$',\ -r'$$\newcommand{\bfrac}[2]{\displaystyle\frac{#1}{#2}}$$',\ -r'$$\newcommand{\lp}{\left (}$$',\ -r'$$\newcommand{\rp}{\right )}$$',\ -r'$$\newcommand{\paren}[1]{\lp {#1} \rp}$$',\ -r'$$\newcommand{\half}{\frac{1}{2}}$$',\ -r'$$\newcommand{\llt}{\left <}$$',\ -r'$$\newcommand{\rgt}{\right >}$$',\ -r'$$\newcommand{\abs}[1]{\left |{#1}\right | }$$',\ -r'$$\newcommand{\pdiff}[2]{\bfrac{\partial {#1}}{\partial {#2}}}$$',\ -r'$$\newcommand{\npdiff}[3]{\bfrac{\partial^{#3} {#1}}{\partial {#2}^{#3}}}$$',\ -r'$$\newcommand{\lbrc}{\left \{}$$',\ -r'$$\newcommand{\rbrc}{\right \}}$$',\ -r'$$\newcommand{\W}{\wedge}$$',\ -r'$$\newcommand{\prm}[1]{{#1}^{\prime}}$$',\ -r'$$\newcommand{\ddt}[1]{\bfrac{d{#1}}{dt}}$$',\ -r'$$\newcommand{\R}{\dagger}$$',\ -r'$$\newcommand{\deriv}[3]{\bfrac{d^{#3}#1}{d{#2}^{#3}}}$$',\ -r'$$\newcommand{\grade}[2]{\left < {#1} \right >_{#2}}$$',\ -r'$$\newcommand{\f}[2]{{#1}\lp {#2} \rp}$$',\ -r'$$\newcommand{\eval}[2]{\left . {#1} \right |_{#2}}$$',\ -r'$$\newcommand{\bs}[1]{\boldsymbol{#1}}$$',\ -r'$$\newcommand{\grad}{\bs{\nabla}}$$'] - -#*********************************************************************** + [r'$$\DeclareMathOperator{\Tr}{Tr}$$', + r'$$\DeclareMathOperator{\Adj}{Adj}$$', + r'$$\newcommand{\bfrac}[2]{\displaystyle\frac{#1}{#2}}$$', + r'$$\newcommand{\lp}{\left (}$$', + r'$$\newcommand{\rp}{\right )}$$', + r'$$\newcommand{\paren}[1]{\lp {#1} \rp}$$', + r'$$\newcommand{\half}{\frac{1}{2}}$$', + r'$$\newcommand{\llt}{\left <}$$', + r'$$\newcommand{\rgt}{\right >}$$', + r'$$\newcommand{\abs}[1]{\left |{#1}\right | }$$', + r'$$\newcommand{\pdiff}[2]{\bfrac{\partial {#1}}{\partial {#2}}}$$', + r'$$\newcommand{\npdiff}[3]{\bfrac{\partial^{#3} {#1}}{\partial {#2}^{#3}}}$$', + r'$$\newcommand{\lbrc}{\left \{}$$', + r'$$\newcommand{\rbrc}{\right \}}$$', + r'$$\newcommand{\W}{\wedge}$$', + r'$$\newcommand{\prm}[1]{{#1}^{\prime}}$$', + r'$$\newcommand{\ddt}[1]{\bfrac{d{#1}}{dt}}$$', + r'$$\newcommand{\R}{\dagger}$$', + r'$$\newcommand{\deriv}[3]{\bfrac{d^{#3}#1}{d{#2}^{#3}}}$$', + r'$$\newcommand{\grade}[2]{\left < {#1} \right >_{#2}}$$', + r'$$\newcommand{\f}[2]{{#1}\lp {#2} \rp}$$', + r'$$\newcommand{\eval}[2]{\left . {#1} \right |_{#2}}$$', + r'$$\newcommand{\bs}[1]{\boldsymbol{#1}}$$', + r'$$\newcommand{\grad}{\bs{\nabla}}$$'] + +# *********************************************************************** + def gFormat(Fmode: bool = True, Dmode: bool = True, inverse='full'): r""" @@ -143,6 +139,7 @@ def gFormat(Fmode: bool = True, Dmode: bool = True, inverse='full'): return + def gprint(*xargs): """ Print latex or text from python script or latex from Jupyter Notebook/Lab @@ -153,17 +150,17 @@ def gprint(*xargs): new_eq_flg = False i = 0 for xi in xargs: - if isinstance(xi,str): + if isinstance(xi, str): if r'\\' in xi and i > 0: if isinteractive(): - xi_rep = xi.replace(r'\\',r'\end{equation*}@\begin{equation*} ') + xi_rep = xi.replace(r'\\', r'\end{equation*}@\begin{equation*} ') else: - xi_rep = xi.replace(r'\\',r'\end{equation*}'+'\n'+r'\begin{equation*} ') + xi_rep = xi.replace(r'\\', r'\end{equation*}'+'\n'+r'\begin{equation*} ') new_eq_flg = True fstr += xi_rep else: fstr += xi - elif isinstance(xi,type): + elif isinstance(xi, type): if LaTeX.latex_flg: fstr += r' \text{'+str(xi)+'} ' else: @@ -192,12 +189,13 @@ def gprint(*xargs): else: display(Math(lstr)) else: - LaTeX.latex_str += r'\begin{equation*} ' + (fstr % tuple(x)) + r'\end{equation*} '+'\n' + LaTeX.latex_str += r'\begin{equation*} ' + (fstr % tuple(x)) + r'\end{equation*} '+'\n' else: print(fstr % tuple(x)) return + def gxpdf(filename=None, paper=(14, 11), crop=False, png=False, prog=False, debug=False, pt='10pt', pdfprog='pdflatex'): """ @@ -213,8 +211,7 @@ def gxpdf(filename=None, paper=(14, 11), crop=False, png=False, prog=False, debu We assume that if xpdf() is called then Format() has been called at the beginning of the program. """ - - latex_str = paper_format(paper,pt)+LaTeX.latex_preamble+LaTeX.latex_str+r'\end{document}' + latex_str = paper_format(paper, pt)+LaTeX.latex_preamble+LaTeX.latex_str+r'\end{document}' if filename is None: pyfilename = sys.argv[0] @@ -223,7 +220,7 @@ def gxpdf(filename=None, paper=(14, 11), crop=False, png=False, prog=False, debu pdf_filename = rootfilename + '.pdf' else: tex_filename = filename - pdf_filename = tex_filename.replace('.tex','.pdf') + pdf_filename = tex_filename.replace('.tex', '.pdf') if debug: print('latex file =', filename) @@ -238,37 +235,36 @@ def gxpdf(filename=None, paper=(14, 11), crop=False, png=False, prog=False, debu if debug: # Display latex excution output for debugging purposes print('pdflatex path =', pdflatex) - #os.system(pdfprog + ' ' + filename[:-4]) + # os.system(pdfprog + ' ' + filename[:-4]) else: # Works for Linux don't know about Windows - subprocess.call([pdfprog,tex_filename,sys_cmd['null']]) - #os.system(pdfprog + ' ' + filename[:-4] + sys_cmd['null']) + subprocess.call([pdfprog, tex_filename, sys_cmd['null']]) + # os.system(pdfprog + ' ' + filename[:-4] + sys_cmd['null']) - subprocess.call([sys_cmd['evince'],pdf_filename]) + subprocess.call([sys_cmd['evince'], pdf_filename]) - #eval(input('!!!!Return to continue!!!!\n')) + # eval(input('!!!!Return to continue!!!!\n')) if debug: - subprocess.call([sys_cmd['rm'],rootfilename+'.aux ',rootfilename+'.log']) + subprocess.call([sys_cmd['rm'], rootfilename+'.aux ', rootfilename+'.log']) else: - subprocess.call([sys_cmd['rm'],rootfilename+'.aux ',rootfilename+'.log ',rootfilename+'.tex']) + subprocess.call([sys_cmd['rm'], rootfilename+'.aux ', rootfilename+'.log ', rootfilename+'.tex']) if crop: - subprocess.call(['pdfcrop',pdf_filename]) - subprocess.call(['rm',pdf_filename]) - subprocess.call(['mv',rootfilename+'-crop.pdf', pdf_filename]) + subprocess.call(['pdfcrop', pdf_filename]) + subprocess.call(['rm', pdf_filename]) + subprocess.call(['mv', rootfilename+'-crop.pdf', pdf_filename]) if png: - subprocess.call(['Pdf2Png',rootfilename]) + subprocess.call(['Pdf2Png', rootfilename]) return -def paper_format(paper,pt): #Set size of paper and font size + +def paper_format(paper, pt): # Set size of paper and font size if paper == 'letter': - paper_size = \ -""" + paper_size = """ \\documentclass[@10pt@,fleqn]{book} """ else: - paper_size = \ -""" + paper_size = """ \\documentclass[@10pt@,fleqn]{book} \\usepackage[vcentering]{geometry} """ @@ -280,4 +276,4 @@ def paper_format(paper,pt): #Set size of paper and font size paper_size = paper_size.replace('@10pt@', pt) - return(paper_size) + return paper_size