vagueish questions on use of file/string objects
peter at engcorp.com
Tue Jan 29 02:16:04 EST 2002
Keith Keller wrote:
> However, the nntplib module demands a file object for its post method.
> So, I have five options:
I have *five* chief options:
> ==Open a temp file, put the post there, then use it with post.
> ==Subclass NNTP, and create a new method that accepts a string instead
> ==Create a fake file object that's not on disk, and pass it to the
(and a fanatical devotion to the Pope?)
My _four_ options are ...
Were you thinking of the Spanish Inquisition sketch in reverse? :-)
> I suppose the vague question is: What would be the ''standard'' Python
> way to handle my dilemma? Is there another way I'm overlooking?
One perhaps elegant approach you can use in Python is (a) check the
source briefly, and (b) understand that the definition of an object
like 'file' is generally in terms of the interface used to work with
In this case, searching for "def post" in nntplib.py I see that the
only method use on the file object passed in to post() is readline().
Therefore wrapping sys.stdin with a simple object which sports
a spiffy readline() method and which does the necessary processing
should be sufficient.
Sometimes Python can be so freaky cool. And I note that I said
"perhaps elegant" above... this particular approach may be a point
def __init__(self, file):
self.file = file
line = self.stdin.readline()
if not line:
return '[whoa: no steenkin' blank lines allowed here!]'
f = Kfile(sys.stdin)
nntplib.post(f) # or words to that effect...
(Caution, I repeat: untested. The idea may be sound. :-)
More information about the Python-list