Re: list1.append(list2) returns None

2006-12-20 Thread Todd Neal
Pyenos wrote:
> def enlargetable(table,col):
> return table.append(col)
>
> def removecolfromtable(table,col):
> return table.remove(col)
>
> print enlargetable([[1],[2],[3]],[4]) # returns None
>
> Why does it return None instead of [[1],[2],[3],[4]] which I expected?

append modifies the list and then returns None:

>>> print a
[1, 2, 3]
>>> print a.append(4)
None
>>> print a
[1, 2, 3, 4]


The reasoning given at
http://www.python.org/doc/faq/general.html#why-doesn-t-list-sort-return-the-sorted-list
is so you wont do something like this:

a = [1,2,3]
b = a.append(4)

and assume that a is still [1,2,3]


More discussion on this topic is available at
http://groups.google.com/group/comp.lang.python/browse_thread/thread/8ab2e67550123b92

Todd

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb, lots of columns and newb-ness

2006-12-19 Thread Todd Neal
Andrew Sackville-West wrote:
>
> I can successfully connect to mysql and do stuff to my tables my
> specific problem is how to efficiently put those 132 fields into the
> thing. All I have been able to figure out is really ugly stuff like:
> build the mysql statement out of various pieces with appropriate
> commas and quote included. stuff like (not tested)
>

I just started looking into Python myself, so someone can probably
clean this up or suggest a better way, but this may work:


import MySQLdb

fields = ["field1\r\n","field2\r\n","field3\r\n"]

def escapeAndQuote(x):
return "\"%s\"" % MySQLdb.escape_string(x)

values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
q = "insert into daily values(%s)" % values


In testing I got:

>>> fields = ["field1\r\n","field2\r\n","field3\r\n"]
>>> values = ", ".join([escapeAndQuote(f[:-2]) for f in fields])
>>> values
'"field1", "field2", "field3"'
>>> q = "insert into daily values(%s)" % values
'insert into daily values("field1", "field2", "field3")'



Todd

-- 
http://mail.python.org/mailman/listinfo/python-list