Loading select queries into objects

Roman Suzi rnd at onego.ru
Mon Jun 18 09:42:14 EDT 2001


On Mon, 18 Jun 2001, Graham Ashton wrote:

>Hi. I've just started playing with database access from Python and have
>stumbled across yet another hole in my knowledge of Python idioms.
>
>I've got a class that has an attribute for each column in a database
>table, and an accessor method that sets the value of each attribute. I
>then populate the attributes with the result of a database query.
>
>Here's an example:
>
>    query = """
>        SELECT column1, column2, some_column
>          FROM a_table
>    """
>
>    cursor = self.db.cursor()
>    cursor.execute(query)
>    tup = cursor.fetchone()
>
>    self.column1(tup[0])		# this could be better!
>    self.column2(tup[1])
>    self.some_column(tup[2])
>
>What I'm wondering is; is there an idiomatic way of applying the values
>in a tuple to a list of functions? In Perl I'd use map(), but I'm not
>sure if there's a "cleaner" way in Python.

map(apply, functions, values)

?

Like in:

>>> map(apply, [int, str, string.lower], [["123"], ["abc"], ["Abc"]])
[123, 'abc', 'abc']

>When the number of columns grows large the above is not only ugly, but a
>nightmare to maintain.
>
>Thanks.
>

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Monday, June 18, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Nostalgia isn't what it used to be." _/





More information about the Python-list mailing list