Best way to enumerate something in python

Discussion in 'Python' started by David Stockwell, May 26, 2004.

  1. 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/
     
    David Stockwell, May 26, 2004
    #1
    1. Advertising

  2. David Stockwell

    Larry Bates Guest

    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" <> wrote in message
    news:...
    > 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/
    >
    >
     
    Larry Bates, May 26, 2004
    #2
    1. Advertising

  3. 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
     
    Hallvard B Furuseth, May 26, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Stockwell

    re best way to enumerate something in Python

    David Stockwell, May 26, 2004, in forum: Python
    Replies:
    2
    Views:
    337
    Michael Geary
    May 26, 2004
  2. David Stockwell

    Re: Best way to enumerate something in python

    David Stockwell, May 26, 2004, in forum: Python
    Replies:
    1
    Views:
    365
  3. David Stockwell

    Re: Best way to enumerate something in python

    David Stockwell, May 27, 2004, in forum: Python
    Replies:
    3
    Views:
    382
    Roy Smith
    May 27, 2004
  4. Дамјан ГеоргиевÑки

    Best way to enumerate classes in a module

    Дамјан ГеоргиевÑки, Jun 24, 2009, in forum: Python
    Replies:
    5
    Views:
    335
    Michele Simionato
    Jun 26, 2009
  5. Robert K.

    ruby way to enumerate users

    Robert K., Feb 1, 2004, in forum: Ruby
    Replies:
    9
    Views:
    242
    Rove Monteux
    Feb 2, 2004
Loading...

Share This Page