Best way to enumerate something in python

David Stockwell winexpert at hotmail.com
Wed May 26 10:21:31 EDT 2004


Thanks for the help.

I found out range creates lists not tuples, so apparently they can get 
changed (for an enum type approach).

I'm going to use them in SQL statements that get generated on the fly.  I 
need to be able to refer to a particular column in  a meaningful way which 
is why if i had an enumerated type it might be easy to do so.

i did some reading and if i interpreted this correctly, dictionaries are 
hashed, correct?  If so i might be able to get away with referencing the 
dictionary and passing the hashkey in this case a number to retrieve the 
column name.  However, I'd like to do this mnemonically  so I can refer to 
the column name with a short cut name that makes sense instead of saying 
something like

insrtCommand = ' select ' + columns[1] + ',' + columns[2] + ' from ' 
blah.......

from a maintenance standpoint, the programmer can't tell what column 1 is 
supposed to refer to.   however if i had something like

columns[HOUSING_ID]    instead of the '1' then the programmer would later at 
a glance be able to follow the code.




David
-------
tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html




>From: "Larry Bates" <lbates at swamisoft.com>
>To: python-list at python.org
>Subject: Re: Best way to enumerate something in python
>Date: Wed, 26 May 2004 08:54:10 -0500
>
>David,
>
>You may have to give us more detail about what
>you want to do, but here goes:
>
>listofcolumns=('field1','field2','field3')
>for column in listofcolumns:
>     <do something>
>
>Most of the time I find that putting the names
>in a dictionary with the column name as key and
>offset as the value seems to work better.
>
>dictofcolumns={'field1':1, 'field2': 2, 'field3':3}
>
>value_for_field3=row[dictofcolumns['field3']]
>
>For your second question I think you should
>take a look at os.path.getatime, .gmtime, getsize
>they are easier to use.
>
>Larry Bates
>Syscon, Inc.
>
>
>"David Stockwell" <winexpert at hotmail.com> wrote in message
>news:mailman.311.1085575108.6949.python-list at python.org...
> > Hi Everyone,
> >
> > I'm wondering about the best way to enumerate something.
> >
> > I have a list of columnames for a db and I decided to put them in a 
>giant
> > tuple list for two reasons:
> >    1) its unchangeable
> >    2) I was hoping that creating an enumeration of those names would be
>easy
> >
> > In the os.stat there is aparrently a list of things you can refer to eg:
> > ST_SIZE, ST_ATIME, etc.
> >
> > How are these defined?  They appear to be related to 0,1,2,3,....   some
> > sort of enumeration.
> >
> > I would like to create an enumeration with 'friendly names' that map to
>the
> > particular offset in my column name tuple.
> >
> > Thanks,
> >
> >
> >
> > David
> > -------
> > tracfone: http://cellphone.duneram.com/index.html
> > Cam: http://www.duneram.com/cam/index.html
> > Tax: http://www.duneram.com/index.html
> >
> > _________________________________________________________________
> > FREE pop-up blocking with the new MSN Toolbar – get it now!
> > http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
> >
> >
>
>
>--
>http://mail.python.org/mailman/listinfo/python-list

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/





More information about the Python-list mailing list