Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get print() output
#11
what is needed is a new kind of file-like class for buffering with sensible semantics more like a pipe.  when created, it will have two file-like interfaces, one for writing, and one for reading. after data is written to the write side, some or all of it can be read.  if a flush() is done on the write side then all (as opposed to some or all) of the data written before the flush() can be read.  after a close() is done on the write side, any resources needed for writing can be released, but the written data is not released.  data can still be read if it has not been read, yet.  after data is read, then it is no longer readable and can be released.  after close() on the write interface, no more data can be written,  after close() on the read interface, no more data can be read.  data can be read both before closing the write interface and after.

additional methods could do:

peek() can see all the data that has be written but not yet read
unread() can push data back in, in reverse order
reset() can make it empty and ready to write, again

features can be added to special implementations:

operate over system piper if a fork() is done
testers can simulate data delays, mangled data, lost data, etc.

(Apr-25-2017, 06:58 AM)wavic Wrote:
import sys

stdout_bak = sys.stdout

with open('output.txt', 'w') as sys.stdout:
    print("I've never did this before so now I'm experimenting")
    print("After that output.txt should containd this prints")

sys.stdout = stdout_bak # you must return everything as it was before to print normally again

print('After the first with statement')

with open('output.txt', 'r') as in_file:
    print(in_file.read())
Output:
victor@jerry:~$ python3 /tmp/test.py After the first with statement I've never did this before so now I'm experimenting After that output.txt should containd this prints

this is the kind of thing i have already been doing.  the function takes an argument of a file-like object to write to.  so i have already written to a file and read it back.  i am seeking something faster with less overhead
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
Well, do it in memory. StringIO is the module I think

Also, you can use queue module.

An example:

In [1]: import queue

In [2]: fifo = queue.Queue()

In [3]: d = {1: 'one'}

In [4]: fifo.put(d)

In [5]: fifo.put(14)

In [6]: text = 'Post here if you are recruiting Python programmers, or are a Python programmer looking for work.'

In [7]: fifo.put(text)

In [8]: while fifo.not_empty:
    ...:     print(fifo.get())
    ...:     
{1: 'one'}
14
Post here if you are recruiting Python programmers, or are a Python programmer looking for work.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
i tried StringIO. it fails. it's goofy. read my other posts. it won't hold the data.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
How does it fail?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#15
(Apr-25-2017, 11:19 AM)wavic Wrote: How does it fail?
an exception event happens: ValueError: I/O operation on closed file

this is because the called function does a close() on the file passed to it, which is a normal thing to do to indicate the end of data.  StringIO is designed wrong.  the writer doing a close() should only mean that there is nothing more to write.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
Don't use the writer then.
I was thinking that the idea was to redirect print to a file.

import io

f = io.StringIO()

print(text, file=f)
This is not using StringIO.write() and should not close the "file"
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#17
the function is written under normal filesystem assumptions it can be given a file name and will open it or be given an already open file object.  in both cases it closes it when done writing.  i have been giving it a filesystem file and when the function returns, open the file for reading and reading it.

i think what it does is:

def function(...,file=None):
    try:
        o=open(file.fileno(),'w')
    except whatever:
        try:
            o=open(file,'w')
        except whatever:
            o=sys.stdout
    ...
    print(stuff.file=o) # many places
    ...
    o.close()
    return
or something similar like holding options in a dictionary.

it's a rather normal thing to close a file when done with it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#18
Yes, an opened file should be closed. But I am missing something.
You want to write and read a text in a file object which you would print() it in another case?
Why don't you write a function that is just 'opening' and closing a file and you can use another one to do the job?

In [1]: import io

In [2]: def io_handler(text, callback): # stupid name but my English.... :D
   ...:     f = io.StringIO()
   ...:     callback(text, f)
   ...:     f.getvalue()
   ...:     f.close()
   ...:     

In [3]: def say(text, file):
   ...:     print(text, file=file)
   ...:     

In [4]: io_handler("Does it work? Will see", say)

In [5]: def io_handler(text, callback): 
   ...:     f = io.StringIO()
   ...:     callback(text, f)
   ...:     print(f.getvalue())
   ...:     f.close()
   ...:  

In [6]: io_handler("Does it work? Will see", say)
Does it work? Will see
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#19
i am thinking of this idea which i am not just trying because it is more involved.  multi-threading with a pipe.  one thread calls the function and the other thread reads the pipe.  that or implement my own file-like object that works the way i want.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,035 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,279 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,689 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,756 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 10,965 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,882 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,046 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 3,942 Jan-16-2021, 09:42 AM
Last Post: xninhox
  Output with none, print(x) in function Vidar567 3 2,454 Nov-24-2020, 05:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020