从可用作字符串的类中实例化 python 类,仅在内存中!

发布于 2024-09-16 16:17:30 字数 1672 浏览 6 评论 0原文

我正在使用 Reportlab 创建 PDF。我正在创建两个 PDF,我想在创建它们后合并它们。 Reportlab提供了一种保存pycanvas的方法 (source) (基本上是内存中的 pdf 文件)作为 python 文件,并在该 python 文件上调用 doIt(filename) 方法,将重新创建 pdf 文件。这很棒,因为您可以在源代码的基础上合并两个 PDF 并创建一个合并 pdf。

这样做是这样的:

from reportlab.pdfgen import canvas, pycanvas
#create your canvas
p = pycanvas.Canvas(buffer,pagesize=PAGESIZE)
#...instantiate your pdf...

# after that, close the PDF object cleanly.
p.showPage()
p.save()

#now create the string equivalent of your canvas
source_code_equiv = str(p)
source_code_equiv2 = str(p)

#merge the two files on str. basis
#not shown how it is exactly done, to make it more easy to read the source
#actually one just have to take the middle part of source_code_equiv2 and add it into source_code_equiv
final_pdf = source_code_equiv_part1 + source_code_equiv2_center_part + source_code_equiv_part2

#write the source-code equivalent of the pdf
open("n2.py","w").write(final_pdf)
from myproject import n2
p = n2.doIt(buffer)

# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response  

这工作正常,但我想跳过将 n2.py 保存到磁盘的步骤。因此,我正在寻找一种方法来从 Final_pdf 字符串实例化相应的 python 类并直接在源代码中使用它。这可能吗?

它应该以某种方式工作。这样

n2 = instantiate_python_class_from_source(final_pdf)
p = n2.doIt(buffer)

做的原因主要是没有真正需要将源保存到磁盘,其次它绝对不是线程保存。我可以在运行时命名创建的文件,但随后我不知道要导入什么!?如果没有办法阻止文件保存,是否有办法根据运行时定义的文件名来定义导入!?

有人可能会问为什么我不提前创建一份 pdf,但这是不可能的,因为它们来自不同的应用程序。

I'm using Reportlab to create PDFs. I'm creating two PDFs which I want to merge after I created them. Reportlab provides a way to save a pycanvas (source) (which is basically my pdf file in memory) as a python file, and calling the method doIt(filename) on that python file, will recreate the pdf file. This is great, since you can combine two PDFs on source code basis and create one merge pdf.

This is done like this:

from reportlab.pdfgen import canvas, pycanvas
#create your canvas
p = pycanvas.Canvas(buffer,pagesize=PAGESIZE)
#...instantiate your pdf...

# after that, close the PDF object cleanly.
p.showPage()
p.save()

#now create the string equivalent of your canvas
source_code_equiv = str(p)
source_code_equiv2 = str(p)

#merge the two files on str. basis
#not shown how it is exactly done, to make it more easy to read the source
#actually one just have to take the middle part of source_code_equiv2 and add it into source_code_equiv
final_pdf = source_code_equiv_part1 + source_code_equiv2_center_part + source_code_equiv_part2

#write the source-code equivalent of the pdf
open("n2.py","w").write(final_pdf)
from myproject import n2
p = n2.doIt(buffer)

# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response  

This works fine, but I want to skip the step that I save the n2.py to the disk. Thus I'm looking for a way to instantiate from the final_pdf string the corresponding python class and use it directly in the source. Is this possible?

It should work somehow like this..

n2 = instantiate_python_class_from_source(final_pdf)
p = n2.doIt(buffer)

The reason for this is mainly that there is not really a need to save the source to the disk, and secondly that it is absolutely not thread save. I could name the created file at run time, but then I do not know what to import!? If there is no way to prevent the file saving, is there a way to define the import based on the name of the file, which is defined at runtime!?

One might ask why I do not create one pdf in advance, but this is not possible, since they are coming from different applications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2024-09-23 16:17:31

这似乎离你想要的还有很长的路要走。 Reportlab 没有可以从中提取 PDF 文档的 Canvas 类吗?我不明白为什么这里应该涉及生成的Python源代码。

但如果出于某种原因有必要,那么您可以使用 StringIO 将源“写入”字符串,然后 exec 执行它:

from cStringIO import StringIO

source_code = StringIO()
source_code.write(final_pdf)
exec(source_code)
p = doIt(buffer)

This seems like a really long way around to what you want. Doesn't Reportlab have a Canvas class from which you can pull the PDF document? I don't see why generated Python source code should be involved here.

But if for some reason it is necessary, then you can use StringIO to "write" the source to a string, then exec to execute it:

from cStringIO import StringIO

source_code = StringIO()
source_code.write(final_pdf)
exec(source_code)
p = doIt(buffer)
任性一次 2024-09-23 16:17:31

好吧,我想你可以使用提供标准解释器交互模式的代码模块。下面将执行函数 doIt。

import code
import string
coded_data = """
def doIt():
    print "XXXXX"
"""
script = coded_data + "\ndoIt()\n" 
co = code.compile_command(script, "<stdin>", "exec")
if co:
    exec co

如果这有帮助,请告诉我。

Ok, I guess you could use code module which provides standard interpreter’s interactive mode. The following would execute function doIt.

import code
import string
coded_data = """
def doIt():
    print "XXXXX"
"""
script = coded_data + "\ndoIt()\n" 
co = code.compile_command(script, "<stdin>", "exec")
if co:
    exec co

Let me know, if this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文