Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Best way to enumerate something in python

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


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      05-27-2004
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[i] = ' %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
 
Reply With Quote
 
 
 
 
Michael Geary
Guest
Posts: n/a
 
      05-27-2004
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


 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      05-27-2004
In article <(E-Mail Removed)>,
"David Stockwell" <(E-Mail Removed)> 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"
 
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
Best way to enumerate something in python David Stockwell Python 2 05-26-2004 05:40 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