Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > correct way to detect container type

Reply
Thread Tools

correct way to detect container type

 
 
Andrew Dalke
Guest
Posts: n/a
 
      10-07-2004
Tim Williams wrote:
> Can anyone suggest a way of capturing the output from smtplib's
> set_debuglevel into a list or dictionary (or anything else I can work
> with),


Looking at smtplib.py, all of the debug messages go to
the module variable 'stderr', which is a reference to sys.stderr.

That means you can replace smtplib.stderr with something
of your own. Try this untested bit of code

import cStringIO
old_error = smtplib.error
new_error = cStringIO.StringIO()
try:
smtplib.error = new_error
... do your smtplib call here ...
finally:
smtplib.error = old_error
print "Debug messages"
print new_error.getvalue()

What this does is replace smtplib.error with a StringIO,
which is a file-like object that stays in memory. I used
the try/finally to ensure that the original error output
file is restored after the smtplib call ends.

There are other things you could do by making your own
sufficiently file-like object instead of using StringIO.
All it needs to do is implement the 'write' method.
But it may have to accumulate several writes before it
gets a full line.

class ShowWrites:
def write(self, text):
print "Got", repr(text)

To test it out, change my first example so it uses
ShowWrite instead of StringIO and get rid of the
last two lines

new_error = ShowWrite()

and get rid of these two lines

old_error = smtplib.error
new_error = ShowWrites()
try:
smtplib.error = new_error
... do your smtplib call here ...
finally:
smtplib.error = old_error



Andrew
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      10-07-2004

----- Original Message -----
From: "Steve Holden" <(E-Mail Removed)>


> Tim Williams wrote:
>
> > Can anyone suggest a way of capturing the output from smtplib's
> > set_debuglevel


> All smtplib debug output is produced using print statements to standard
> ouput, so you could try creating a "file-like" object (such as a
> cStringIO) and temporarily replacing sys.stdout with it (saving
> sys.sdout and replacing it if other parts of your program need to use it).
>
> If you need help to work out the details, post again.


Many thanks Steve

I have implemented your suggestion , but what is the best way to get the
data out of the file-like object, the only option that seems to work for
me is

f.getvalue()

which returns a string, but getvalue() doesn't "feel" right somehow. Am I
missing something ?

TIA












 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      10-08-2004
Tim Williams wrote:

> ----- Original Message -----
> From: "Steve Holden" <(E-Mail Removed)>
>
>
>>Tim Williams wrote:
>>
>>
>>>Can anyone suggest a way of capturing the output from smtplib's
>>>set_debuglevel

>
>
>>All smtplib debug output is produced using print statements to standard
>>ouput, so you could try creating a "file-like" object (such as a
>>cStringIO) and temporarily replacing sys.stdout with it (saving
>>sys.sdout and replacing it if other parts of your program need to use it).
>>
>>If you need help to work out the details, post again.

>
>
> Many thanks Steve
>
> I have implemented your suggestion , but what is the best way to get the
> data out of the file-like object, the only option that seems to work for
> me is
>
> f.getvalue()
>
> which returns a string, but getvalue() doesn't "feel" right somehow. Am I
> missing something ?
>

If you want to use file primitives to read the value back, just create
another one:

inpt = StringIO.StringIO(firstStringIO.getvalue())

then you can read the printed output from inpt just like a file.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      10-08-2004

> If you want to use file primitives to read the value back, just create
> another one:
>
> inpt = StringIO.StringIO(firstStringIO.getvalue())
>
> then you can read the printed output from inpt just like a file.


Or even:

firstStringIO.seek(0)

- Josiah

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      10-08-2004

----- Original Message -----
From: "Josiah Carlson" <(E-Mail Removed)>


>
> > If you want to use file primitives to read the value back, just create
> > another one:
> >
> > inpt = StringIO.StringIO(firstStringIO.getvalue())
> >
> > then you can read the printed output from inpt just like a file.

>
> Or even:
>
> firstStringIO.seek(0)
>
> - Josiah


Steve, Andrew, Josiah,

Thanks for your answers.

The outgoing SMTP is multi-threaded, so the threads disrupt each other's
output to the file-like object if there is any concurrent outgoing mail
activity.

Is there anyway I can seperate the output from each thread and capture it ?

The outgoing threads are started with






 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-08-2004
Josiah Carlson wrote:

>>If you want to use file primitives to read the value back, just create
>>another one:
>>
>>inpt = StringIO.StringIO(firstStringIO.getvalue())
>>
>>then you can read the printed output from inpt just like a file.

>
>
> Or even:
>
> firstStringIO.seek(0)
>

Yup, actually rather better than my hurried attempt.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119

 
Reply With Quote
 
Peter L Hansen
Guest
Posts: n/a
 
      10-08-2004
Tim Williams wrote:
> I have implemented your suggestion , but what is the best way to get the
> data out of the file-like object, the only option that seems to work for
> me is
>
> f.getvalue()
>
> which returns a string, but getvalue() doesn't "feel" right somehow. Am I
> missing something ?


I've never felt the name "spoke to me" very well either , but
it is the correct way to do it.

Note also, regarding Steve's suggestion, that for simplicity
in a small program you don't need to save sys.stdout if you
are doing that just to restore it later, because sys.__stdout__
is always the original value and a quick sys.stdout == sys.__stdout__
will do the trick.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining iterator type through container object type Urs Thuermann C++ 6 11-04-2011 02:10 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments