Best way to enumerate something in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Stockwell

    Best way to enumerate something in python

    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!



  • Larry Bates

    #2
    Re: Best way to enumerate something in python

    David,

    You may have to give us more detail about what
    you want to do, but here goes:

    listofcolumns=( 'field1','field 2','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_field 3=row[dictofcolumns['field3']]

    For your second question I think you should
    take a look at os.path.getatim e, .gmtime, getsize
    they are easier to use.

    Larry Bates
    Syscon, Inc.


    "David Stockwell" <winexpert@hotm ail.com> wrote in message
    news:mailman.31 1.1085575108.69 49.python-list@python.org ...[color=blue]
    > 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[/color]
    easy[color=blue]
    >
    > 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[/color]
    the[color=blue]
    > 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/
    >
    >[/color]


    Comment

    • Hallvard B Furuseth

      #3
      Re: Best way to enumerate something in python

      David Stockwell wrote:
      [color=blue]
      > 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.[/color]

      Like this?
      [color=blue][color=green][color=darkred]
      >>> class tuple_names(tup le):[/color][/color][/color]
      .... def __init__(self, dummy = None):
      .... self.__dict__ = dict(zip(self, range(0, len(self))))
      ....[color=blue][color=green][color=darkred]
      >>> x = tuple_names(('a ', 'b', 'c'))
      >>> x[/color][/color][/color]
      ('a', 'b', 'c')[color=blue][color=green][color=darkred]
      >>> x.c[/color][/color][/color]
      2


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

      import sys

      class Named_int(int):
      """Named_int('n ame', 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_l ocals, **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

      Comment

      Working...