Best way to enumerate something in python

D

David Stockwell

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/onm00200415ave/direct/01/
 
L

Larry Bates

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.
 
H

Hallvard B Furuseth

David said:
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?
.... def __init__(self, dummy = None):
.... self.__dict__ = dict(zip(self, range(0, len(self))))
.... 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top