Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Best way to enumerate something in python

Reply
Thread Tools

Best way to enumerate something in python

 
 
David Stockwell
Guest
Posts: n/a
 
      05-26-2004
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
-------
Cell: 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/...ave/direct/01/


 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      05-26-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> -------
> Cell: 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/...ave/direct/01/
>
>



 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      05-26-2004
David Stockwell wrote:

> 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
>
> (...)
>
> I would like to create an enumeration with 'friendly names' that map to the
> particular offset in my column name tuple.


Like this?

>>> class tuple_names(tuple):

.... def __init__(self, dummy = None):
.... self.__dict__ = dict(zip(self, range(0, len(self))))
....
>>> x = tuple_names(('a', 'b', 'c'))
>>> x

('a', 'b', 'c')
>>> x.c

2


Another approach to enumeration which I've just been playing with:

import sys

class Named_int(int):
"""Named_int('name', value) is an int with str() = repr() = 'name'."""
def __new__(cls, name, val):
self = int.__new__(cls, val)
self.name = name
return self
def __str__(self): return self.name
__repr__ = __str__
__slots__ = 'name'

def Enum_dict(_Enum_dest = None, **src):
if _Enum_dest is None:
_Enum_dest = {}
for name, val in src.items():
_Enum_dest[name] = Named_int(name, val)
return _Enum_dest

def Enum(**mapping):
"""Enum(var = integer, ...) defines the specified named integer variables.
Each variable is set to a Named_int with name 'var' and value 'integer'.
Enum() only works in class bodies and at file level, not in functions."""

Enum_dict(sys._getframe(1).f_locals, **mapping)

# Test
if __name__ == '__main__':
x = Named_int('y', 3)
print x, str(x), int(x), "(%s = %d)" % (x, x)

Enum(
debug = 0,
info = 1,
warning = 2,
error = 3,
critical = 4
)
print"%s = %d" % (info, info)

class foo: Enum (one = 1, two = 2)
print foo.two

print Enum_dict(three = 3, five = 5)
print Enum_dict({None: ()}, seven = 7)

--
Hallvard
 
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
Best way to enumerate classes in a module Дамјан Георгиевски Python 5 06-26-2009 09:56 AM
Re: Best way to enumerate something in python David Stockwell Python 1 05-27-2004 05:50 PM
Re: Best way to enumerate something in python David Stockwell Python 3 05-27-2004 05:31 PM
re best way to enumerate something in Python David Stockwell Python 2 05-26-2004 05:18 PM
ruby way to enumerate users Robert K. Ruby 9 02-02-2004 04:17 PM



Advertisments