我已经制作了一些python函数,用于使用latex 编译传递的字符串作为pdf文件.该函数按预期工作并且非常有用,因此我寻找改进它的方法.
我有的代码:
def generate_pdf(pdfname,table):
"""
Generates the pdf from string
"""
import subprocess
import os
f = open('cover.tex','w')
tex = standalone_latex(table)
f.write(tex)
f.close()
proc=subprocess.Popen(['pdflatex','cover.tex'])
subprocess.Popen(['pdflatex',tex])
proc.communicate()
os.unlink('cover.tex')
os.unlink('cover.log')
os.unlink('cover.aux')
os.rename('cover.pdf',pdfname)
Run Code Online (Sandbox Code Playgroud)
代码的问题在于它在工作目录中创建了一堆名为cover的文件,之后被删除.
如何避免在工作目录中创建不需要的文件?
def generate_pdf(pdfname,tex):
"""
Genertates the pdf from string
"""
import subprocess
import os
import tempfile
import shutil
current = os.getcwd()
temp = tempfile.mkdtemp()
os.chdir(temp)
f = open('cover.tex','w')
f.write(tex)
f.close()
proc=subprocess.Popen(['pdflatex','cover.tex'])
subprocess.Popen(['pdflatex',tex])
proc.communicate()
os.rename('cover.pdf',pdfname)
shutil.copy(pdfname,current)
shutil.rmtree(temp)
Run Code Online (Sandbox Code Playgroud) 我想在 python 中接口 C++ 库,它将函数指针作为参数。我看起来可以在 C 中使用 进行调用PyEval_CallObject,但要进一步进行,我需要正确的签名(输入和输出参数的类型)。是否可以从具有指定签名的python返回回调?
另外我有点担心性能,所以我也查看了numba编译python函数的python项目。如果可以在 C/C++ 中访问它以提高性能,我很感兴趣。