Re: Best way to enumerate something in python

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

  1. FWIW this is what I'm going to do for enumerated types in python. Its not
    perfect but it will make it fairly easy to get at column names so I can
    build SQL statements on the fly and still have fairly easy to maintain code

    #setup stuff
    NAME_COL, ADDRESS_COL, CITY_COL, ZIPCODE_COL, \
    STATE_COL, COUNTRY_COL, SS_COL, CAT_COL, \
    DATE_COL, SALARY_COL = range(10)

    mycol = {
    NAME_COL: " NAME ", ADDRESS_COL: " ADDRESS ",
    CITY_COL: " CITY ", ZIPCODE_COL: " ZIPCODE ",
    STATE_COL:" STATE ", COUNTRY_COL: " COUNTRY ",
    SS_COL: " SS ", CAT_COL: " CAT ",
    DATE_COL: " DATE ", SALARY_COL: " SALARY " }
    # Use these for indexing by column name

    # demonstration on how to 'get'

    print mycol[CITY_COL]


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

    _________________________________________________________________
    Stop worrying about overloading your inbox - get MSN Hotmail Extra Storage!
    http://join.msn.click-url.com/go/onm00200362ave/direct/01/
     
    David Stockwell, May 27, 2004
    #1
    1. Advertising

  2. David Stockwell

    Peter Hansen Guest

    David Stockwell wrote:

    > FWIW this is what I'm going to do for enumerated types in python. Its
    > not perfect but it will make it fairly easy to get at column names so I
    > can build SQL statements on the fly and still have fairly easy to
    > maintain code
    >
    > #setup stuff
    > NAME_COL, ADDRESS_COL, CITY_COL, ZIPCODE_COL, \
    > STATE_COL, COUNTRY_COL, SS_COL, CAT_COL, \
    > DATE_COL, SALARY_COL = range(10)
    >
    > mycol = {
    > NAME_COL: " NAME ", ADDRESS_COL: " ADDRESS ",
    > CITY_COL: " CITY ", ZIPCODE_COL: " ZIPCODE ",
    > STATE_COL:" STATE ", COUNTRY_COL: " COUNTRY ",
    > SS_COL: " SS ", CAT_COL: " CAT ",
    > DATE_COL: " DATE ", SALARY_COL: " SALARY " }
    > # Use these for indexing by column name


    Any time you see a pattern of repetition like this, you can
    make improvements, at least with Python if not some other
    languages. If nothing else, this will generally vastly improve
    maintainability, and often readability as well.


    >>> cols = 'NAME ADDRESS CITY ZIPCODE STATE COUNTRY SS CAT DATE SALARY'
    >>> mycol = {}
    >>> for i,col in enumerate(cols.split()):

    .... globals()[col + '_COL'] = i
    .... mycol = ' %s ' % col
    ....
    >>> dir()

    ['ADDRESS_COL', 'CAT_COL', 'CITY_COL', 'COUNTRY_COL', 'DATE_COL',
    'NAME_COL', 'SALARY_COL', 'SS_COL', 'STATE_COL', 'ZIPCODE_COL',
    '__builtins__', '__doc__', '__name__', 'col', 'cols', 'enum', 'i', 'mycol']
    >>> mycol[CITY_COL]

    ' CITY '

    -Peter
     
    Peter Hansen, May 27, 2004
    #2
    1. Advertising

  3. David Stockwell wrote:
    > FWIW this is what I'm going to do for enumerated types in python.
    > Its not perfect but it will make it fairly easy to get at column names
    > so I can build SQL statements on the fly and still have fairly easy to
    > maintain code
    >
    > #setup stuff
    > NAME_COL, ADDRESS_COL, CITY_COL, ZIPCODE_COL, \
    > STATE_COL, COUNTRY_COL, SS_COL, CAT_COL, \
    > DATE_COL, SALARY_COL = range(10)
    >
    > mycol = {
    > NAME_COL: " NAME ", ADDRESS_COL: " ADDRESS ",
    > CITY_COL: " CITY ", ZIPCODE_COL: " ZIPCODE ",
    > STATE_COL:" STATE ", COUNTRY_COL: " COUNTRY ",
    > SS_COL: " SS ", CAT_COL: " CAT ",
    > DATE_COL: " DATE ", SALARY_COL: " SALARY " }
    > # Use these for indexing by column name
    >
    > # demonstration on how to 'get'
    >
    > print mycol[CITY_COL]


    I don't know much about SQL, so this may be a dumb question, but... Is it a
    requirement that you have indices for the columns as well as names? IOW,
    does it matter that NAME_COL is 0, ADDRESS_COL is 1, etc.? Or is the end
    result you're after to have NAME_COL be a way to get to the string " NAME "?

    If you don't need the numeric indices, there are a couple of other
    approaches you could take, both inspired by Peter Hansen's nifty bit of
    code:

    First, since you're putting CITY_COL and so forth in the global namespace
    anyway, you could simply define them as the strings:

    def makeColumnNames( names ):
    class container: pass
    cols = container()
    for c in names.split():
    cols.__dict__[c] = ' %s ' % c
    return cols

    cols = makeColumnNames(
    'NAME ADDRESS CITY ZIPCODE STATE COUNTRY SS CAT DATE SALARY' )

    print "'%s'" % cols.CITY

    Or, you could avoid cluttering up the global namespace with those names,
    avoid the need for the _COL suffix, and use the cleaner cols.CITY notation
    instead of cols[CITY_COL]:

    def makeColumnNames( names ):
    class container: pass
    cols = container()
    for c in names.split():
    cols.__dict__[c] = ' %s ' % c
    return cols

    cols = makeColumnNames(
    'NAME ADDRESS CITY ZIPCODE STATE COUNTRY SS CAT DATE SALARY' )

    print "'%s'" % cols.CITY

    If you do need the numeric indices as well, it would be easy to come up with
    a variation of one of the above approaches that gives you both the indices
    and the names in a convenient way.

    -Mike
     
    Michael Geary, May 27, 2004
    #3
  4. David Stockwell

    Roy Smith Guest

    In article <>,
    "David Stockwell" <> wrote:

    > FWIW this is what I'm going to do for enumerated types in python. Its not
    > perfect but it will make it fairly easy to get at column names so I can
    > build SQL statements on the fly and still have fairly easy to maintain code
    >
    > #setup stuff
    > NAME_COL, ADDRESS_COL, CITY_COL, ZIPCODE_COL, \
    > STATE_COL, COUNTRY_COL, SS_COL, CAT_COL, \
    > DATE_COL, SALARY_COL = range(10)
    >
    > mycol = {
    > NAME_COL: " NAME ", ADDRESS_COL: " ADDRESS ",
    > CITY_COL: " CITY ", ZIPCODE_COL: " ZIPCODE ",
    > STATE_COL:" STATE ", COUNTRY_COL: " COUNTRY ",
    > SS_COL: " SS ", CAT_COL: " CAT ",
    > DATE_COL: " DATE ", SALARY_COL: " SALARY " }
    > # Use these for indexing by column name


    I'm not sure what problem you're trying to solve here, but I suspect the
    cure is worse than the disease. This seems like a lot of error-prone
    typing, and I'm not sure what the benefit is.

    What happens when your SQL schema changes? You need to make multiple
    changes to the above code to handle adding or deleting a column.

    > print mycol[CITY_COL]


    How is that any better than:

    print "CITY"
     
    Roy Smith, May 27, 2004
    #4
    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

    Best way to enumerate something in python

    David Stockwell, May 26, 2004, in forum: Python
    Replies:
    2
    Views:
    323
    Hallvard B Furuseth
    May 26, 2004
  2. David Stockwell

    re best way to enumerate something in Python

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

    Re: Best way to enumerate something in python

    David Stockwell, May 26, 2004, in forum: Python
    Replies:
    1
    Views:
    350
  4. Дамјан ГеоргиевÑки

    Best way to enumerate classes in a module

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

    ruby way to enumerate users

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

Share This Page