Hide keyboard shortcuts

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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

""" 

@file 

@brief Buffer as a logging function. 

""" 

from io import StringIO 

 

 

class BufferedPrint: 

""" 

Buffered display. Relies on :epkg:`*py:io:StringIO`. 

Use it as follows:: 

 

.. runpython:: 

:showcode: 

 

def do_something(fLOG=None): 

if fLOG: 

fLOG("Did something.") 

return 3 

 

from pyquickhelper.loghelper import BufferedPrint 

buf = BufferedPrint() 

do_something(fLOG=buf.fprint) 

print(buf) 

""" 

 

def __init__(self): 

"constructor" 

self.buffer = StringIO() 

 

def fprint(self, *args, **kwargs): 

"print function" 

mes = " ".join(str(_) for _ in args) 

self.buffer.write(mes) 

self.buffer.write("\n") 

 

def __str__(self): 

"Returns the content." 

return self.buffer.getvalue()