44163

instantiate python class from class available as string , only in memory!

Question:

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 <a href="http://www.java2s.com/Open-Source/Python/PDF/ReportLab/ReportLab_2_4/src/reportlab/pdfgen/pycanvas.py.htm" rel="nofollow">(source)</a> (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.

Answer1:

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)

Answer2:

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.

Recommend

  • React + Axios double POST request to spring RestController (also in fetch, seem not to be OPTIONS)
  • Specified element is already the logical child of another element. Disconnect it first
  • AngularJS how to use browser language detection?
  • Backbone Router on any page change event
  • Android v23 PDFreader keep crashing
  • Recursive regex not matching template blocks
  • Eclipse > Javascript > Code highlighting not working with Object Notation
  • Creating a stored procedure in MySQL5 with ColdFusion 9's -Tag
  • Python - replace unicode emojis with ASCII charachters
  • Separate float into digits
  • Python-3 Request parameter encoding error
  • TypeError: Main.login is not a function
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Get highlight text in current window and send it in a popup
  • How to click on a link that has a certain content in puppeteer?
  • Pointer to a logger class provide for all other classes? [duplicate]
  • Send the string to its hex equivalent
  • How to remove the dot in to_char if the number is an integer
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • force json_encode to create strings
  • C function strchr - How to calculate the position of the character?
  • Converter from SAT to 3-SAT
  • Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
  • Parse a date string in a specific locale (not timezone!)
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • How to handle AllServersUnavailable Exception
  • How to set my toolbar fixed while scrolling android
  • 0x202A in filename: Why?
  • SVN: Merging two branches together
  • AT Commands to Send SMS not working in Windows 8.1
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • apache spark aggregate function using min value
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Python/Django TangoWithDjango Models and Databases