Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Buffer as a logging function.
4"""
5from io import StringIO
8class BufferedPrint:
9 """
10 Buffered display. Relies on :epkg:`*py:io:StringIO`.
11 Use it as follows:
13 .. runpython::
14 :showcode:
16 def do_something(fLOG=None):
17 if fLOG:
18 fLOG("Did something.")
19 return 3
21 from pyquickhelper.loghelper import BufferedPrint
22 buf = BufferedPrint()
23 do_something(fLOG=buf.fprint)
24 print(buf)
25 """
27 def __init__(self):
28 "constructor"
29 self.buffer = StringIO()
31 def fprint(self, *args, **kwargs):
32 "print function"
33 mes = " ".join(str(_) for _ in args)
34 self.buffer.write(mes)
35 self.buffer.write("\n")
37 def __str__(self):
38 "Returns the content."
39 return self.buffer.getvalue()