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

 
 
Robin Becker
Guest
Posts: n/a
 
      10-07-2004
I seem often to allow list/tuple arguments to be a singleton. This leads to code
of the form

if type(arg) not in (type(()),type([])): arg = [arg]


or alternatively

from type import ListType, TupleType
_seqTypes = ListType, TupleType
.......
if type(arg) not in _seqTypes: ....

is there a 'best' way to do this?
--
Robin Becker
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-07-2004
Robin Becker wrote:

> I seem often to allow list/tuple arguments to be a singleton. This leads
> to code of the form
>
> if type(arg) not in (type(()),type([])): arg = [arg]
>
>
> or alternatively
>
> from type import ListType, TupleType
> _seqTypes = ListType, TupleType
> ......
> if type(arg) not in _seqTypes: ....
>
> is there a 'best' way to do this?


I prefer

isinstance(var, (list, tuple))

to the above, but I don't think there is one correct way. Often you don't
want a full-blown container, just an iterable. Then

try:
it = iter(var)
except TypeError:
it = iter([var])

may be more appropriate. However, you will end up iterating over the
characters if var is a string, which often has to be guarded against with
an additional isinstance(basestring) test

try:
iter(var)
except TypeError:
var = [var]
else:
if isinstance(var, basestring): var = [var]

Peter

 
Reply With Quote
 
 
 
 
Carlos Ribeiro
Guest
Posts: n/a
 
      10-07-2004
On Thu, 07 Oct 2004 10:30:22 +0100, Robin Becker <(E-Mail Removed)> wrote:
> I seem often to allow list/tuple arguments to be a singleton. This leads to code
> of the form
>
> if type(arg) not in (type(()),type([])): arg = [arg]
>
> or alternatively
>
> from type import ListType, TupleType
> _seqTypes = ListType, TupleType
> ......
> if type(arg) not in _seqTypes: ....
>
> is there a 'best' way to do this?


Depending upon the way you're using your sequence types, why don't you
check for __getitem__? As far as I'm aware, it's all that is needed to
support for loops. In some cases __len__ may also be needed. The
advantage is that you will accept any user defined type that supports
the sequence interface.


--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
Aaron Bingham
Guest
Posts: n/a
 
      10-07-2004
Carlos Ribeiro wrote:

>On Thu, 07 Oct 2004 10:30:22 +0100, Robin Becker <(E-Mail Removed)> wrote:
>
>
>>I seem often to allow list/tuple arguments to be a singleton. This leads to code
>>of the form
>>
>>if type(arg) not in (type(()),type([])): arg = [arg]
>>
>>or alternatively
>>
>>from type import ListType, TupleType
>>_seqTypes = ListType, TupleType
>>......
>>if type(arg) not in _seqTypes: ....
>>
>>is there a 'best' way to do this?
>>
>>

>
>Depending upon the way you're using your sequence types, why don't you
>check for __getitem__? As far as I'm aware, it's all that is needed to
>support for loops. In some cases __len__ may also be needed. The
>advantage is that you will accept any user defined type that supports
>the sequence interface.
>
>
>
>

Careful! If all you are doing is:

for x in arg:
do something with x

Then arg only needs to support __iter__ and need not be a sequence type
nor support __getitem__.

Aaron

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      10-07-2004
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), Preferably not to a file, though if this is the only way, then it
would be a start

I have googled.

TIA


 
Reply With Quote
 
Robin Becker
Guest
Posts: n/a
 
      10-07-2004
Peter Otten wrote:

> I prefer
>
> isinstance(var, (list, tuple))
>
> to the above, but I don't think there is one correct way. Often you don't
> want a full-blown container, just an iterable. Then
>
> try:
> it = iter(var)
> except TypeError:
> it = iter([var])
>
> may be more appropriate. However, you will end up iterating over the
> characters if var is a string, which often has to be guarded against with
> an additional isinstance(basestring) test
>
> try:
> iter(var)
> except TypeError:
> var = [var]
> else:
> if isinstance(var, basestring): var = [var]
>
> Peter
>

lots of good ideas, but I think this breaks in 2.1 which we're still supporting.
As do some of the iteration ideas.
--
Robin Becker
 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      10-07-2004
>Is it possible, using email.MIMEMessage, to generate an email
>with both HTML and plain text content, in such a way that:


>1) it is displayed as HTML if the email client recognizes HTML;
>2) it is displayed as plain text if the email client is HTML impaired?


It should be, though I haven't tried it.

This page might help you work out what you need to do.
http://www.wilsonweb.com/wmt5/html-email-multi.htm

HTH

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-07-2004
Robin Becker wrote:

> lots of good ideas, but I think this breaks in 2.1


Yes, I think it requires at least 2.2. Back to square one then...

Peter

 
Reply With Quote
 
Carlos Ribeiro
Guest
Posts: n/a
 
      10-07-2004
On Thu, 07 Oct 2004 11:16:47 +0100, Robin Becker <(E-Mail Removed)> wrote:
> Peter Otten wrote:
>
> > I prefer
> >
> > isinstance(var, (list, tuple))
> >
> > to the above, but I don't think there is one correct way. Often you don't
> > want a full-blown container, just an iterable. Then
> >
> > try:
> > it = iter(var)
> > except TypeError:
> > it = iter([var])
> >
> > may be more appropriate. However, you will end up iterating over the
> > characters if var is a string, which often has to be guarded against with
> > an additional isinstance(basestring) test
> >
> > try:
> > iter(var)
> > except TypeError:
> > var = [var]
> > else:
> > if isinstance(var, basestring): var = [var]
> >
> > Peter
> >

> lots of good ideas, but I think this breaks in 2.1 which we're still supporting.
> As do some of the iteration ideas.
> --
> Robin Becker


In 2.1 you can probably use the __getitem__ test.

And I also think that you can define a conditional function in your
library, as to make possible for 2.2+ users to pass iterators. It's a
general solution, and more modern one at that.

I'm now using generators for related things -- namely, when generating
long output, I'm yielding individual strings for each line (or record)
instead of concatenating them into a huge string. Depending on the
system, it will make it more responsive. The performance impact is
negligible; in some scenarios, it's in fact faster, because you can
avoid lots of small string concatenations.

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
Steve Holden
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), Preferably not to a file, though if this is the only way, then it
> would be a start
>
> I have googled.
>


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.

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

 
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