vagueish questions on use of file/string objects

Keith Keller kkeller at speakeasy.net
Tue Jan 29 02:22:11 EST 2002


In article <3C564BE5.7ED051AA at engcorp.com>,
	Peter Hansen <peter at engcorp.com> writes:
> 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:

> Were you thinking of the Spanish Inquisition sketch in reverse? :-)

No--"Three, sir!"  :)

> 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
> it.  
> 
> 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().

I saw this--hence option two to override nntplib's post with my
own post method which worked with a string object instead.  It was
quite easy to code, so this:

> Therefore wrapping sys.stdin with a simple object which sports
> a spiffy readline() method and which does the necessary processing
> should be sufficient.

just sounds yucky.  I know internet protocols better than I do file-
like protocols, so it was natural for me to hack nntplib instead.
I'd considered trying to add a readline method to string objects,
but then Steve posted about c/StringIO, and StringIO appears to
do something quite similar.

> Sometimes Python can be so freaky cool.  And I note that I said
> "perhaps elegant" above... this particular approach may be a point 
> of contention.

Ayup.  :)  But it was still freaky cool: implementing the new subclass
was amazingly easy.  (Quite a bit easier than the other p-language
I use.)

--keith

-- 
kkeller at speakeasy.net
public key:  http://wombat.san-francisco.ca.us/kkeller/public_key
alt.os.linux.slackware FAQ:  http://wombat.san-francisco.ca.us/perl/fom




More information about the Python-list mailing list