473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what is the the best way to express following:

if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()
In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.
Mar 28 '06 #1
6 1072
"AndyL" <ask@me> wrote in message news:_u********************@comcast.com...
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()
In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.


Your literal Pythonic approach is to use the isinstance() builtin:

if isinstance(key,(int,long)):
abc()
elif isinstance(key,str):
efg():
elif isinstance(key,(tuple,list)):
ijk()
But since you are dispatching to a method based on type (or some other type
of enumerated condition), even more Pythonic is to use a dict to create a
dispatch table:

dispatch = {
int : abc,
long : abc,
str : efg,
tuple : ijk,
list : ijk,
}
try:
fn = dispatch[ type(key) ]
except KeyError:
print "No handler for key of type", type(key)
else:
fn()
-- Paul
Mar 28 '06 #2
AndyL wrote:
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()
In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.


Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()
James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 28 '06 #3
James Stroud wrote:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()

Is not that a shortcut? I have got "KeyError" exception ...
Mar 28 '06 #4
Paul McGuire wrote:
"AndyL" <ask@me> wrote in message news:_u********************@comcast.com...
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()
In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.

Your literal Pythonic approach is to use the isinstance() builtin:

if isinstance(key,(int,long)):
abc()
elif isinstance(key,str):
efg():
elif isinstance(key,(tuple,list)):
ijk()


thx a lot . In fact I do not do dispatch, just wanted ilustrate if:
elif: sequence. Andy
Mar 28 '06 #5
AndyL wrote:
James Stroud wrote:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()


Is not that a shortcut? I have got "KeyError" exception ...


No, I hit send too soon, mixing ideas in my head. Paul McGuire's answer
is the way to go.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 28 '06 #6
Em Seg, 2006-03-27 Ã*s 18:43 -0800, James Stroud escreveu:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()


Maybe...

todo = {(int, long): abc, basestr: afg, (tuple, list): ijk}
(y for x,y in todo.iteritems() if isinstance(key, x)).next()()

Forget it! Damn, that's too unpythonic! (or perlish =)

--
Felipe.

Mar 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Val3 | last post by:
Hi all. I need to build dll(s) and windows services using VB .NET 2005 Express. When I make File/New project the windows contain only Windows application, Windows control library, Console...
1
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005...
4
by: Andrew Robinson | last post by:
My main dev machine has WinXp and VS2005 (pro). 1. I need to install VWD Express Edition so that I can do some instruction on this. Any issues with both on the same machine. Installation order?...
3
by: Greg | last post by:
>From my dark past I seem to remember that when distributing an app that talked to an access database, the end user did not need to have access installed on their machine in order for an...
12
by: johannblake | last post by:
First off, I am NOT a beginner. I have lots of experience developing professional web sites and am a professional software developer. Unfortunately I've been out of web site development for the...
2
by: JJ | last post by:
I wanted to run mySQL server as a back end to my web site(s), running on win2k3 with asp2.0 . Unfortunately this doesn't appear to be possible as there is no 'connector' available to connect the...
17
by: Matko | last post by:
Hello, I need to store data in some database for my application. I need database for easy deployment (small size), that is reliable and maybe fast. What database is best to use in this case? ...
10
by: Shraddha | last post by:
int *s=(int *)2000; int *p=(int *)1000; printf("%d",p-s);
3
by: =?ISO-8859-1?Q?S=E9bastien_de_Mapias?= | last post by:
Good day, To people well acquainted with XML schemas manipulation here, how do you express relationships/references between elements ? Take the following excerpt from one of the analysts around...
2
by: ChaosMageX | last post by:
I copied some code from a Mono 2.4 library to use in my own application. I'm getting a problem because one of the variable declarations involves the keyword "pinned" between the identifier and the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.