Replace one element of a tuple (LONG)

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Jun 2 21:42:23 EDT 2006


Captain Dondo a écrit :
(snip)

> c=masterDB.cursor()
> c.execute("""SELECT title, subtitle, starttime FROM recorded""")
> 
> # build our dialog checkbox
> 
> d = dialog.Dialog(dialog="dialog")
> d.add_persistent_args(["--backtitle", "Myth2Go"])
> 
> recordings=[]
> for listing in c.fetchall():
>         recordings.append(
>         (listing[0]+'|'+listing[2].isoformat(),
>         listing[1],0))
 >
> recordings.sort()

Why don't you just add an order by clause in your sql statement ???

> (retcode, itemlist) = d.checklist(text="",
>         height=15, width=70, list_height=7,
>         choices=recordings,
>         title="Which recordings do you want to transfer?")
> 
> selectlist=[]
> for listing in itemlist:
>         print listing
>         (rectitle, recdate) = listing.split('|',1)
>         c.execute("""SELECT * FROM recorded WHERE title=%s AND
>         starttime=%s""",(rectitle,recdate))

is the combination of title and starttime a unique key ?

Also, unless you have lots of records with huge blobs in 'recorded' 
table, this smells like a WTF.

>         selectlist.append(c.fetchone())


> The problem is the last line.

I'm afraid this is not the only problem...

>  I am creating a bunch of tuples that are, 
> for my purposes, incorrect.

selectlist.append(list(c.fetchone)) would create a bunch of lists instead.

>  I would like to create them with 
> masterBackend replaced by toGoBackend.
> 
> Currently (I corrected a small bug based on another post) after a user 
> selects what recordings s/he wants, selectlist contains:
> 
> [
> 
> (1028L, datetime.datetime(2006, 5, 26, 7, 0), datetime.datetime(2006, 5, 
> 26, 7, 30), 'Arthur', "What's Cooking?; Buster's Special Delivery", '', 
> 'Children', 'tooth.seiner.lan', None, 0L, None, 1L, 1L, 'Default', 9L, 
> 'SH044107', 'EP0441070207', datetime.datetime(2006, 5, 26, 7, 31, 1), 
> 1162899392L, 0.0, 0, datetime.date(2006, 5, 26), 0, 0L, 0),

No huge blob in sight. And since the list of records fits on a dialog 
with checkboxes, I think you could easily avoid querying the DB that 
many times. FWIW, you could probably query it once only.

> (1028L, datetime.datetime(2006, 5, 27, 9, 0), datetime.datetime(2006, 5, 
> 27, 9, 30), 'Arthur', 'Unfinished; D.W., Bossy Boots', '', 'Children', 
> 'tooth.seiner.lan', None, 0L, None, 1L, 1L, 'Default', 9L, 'SH044107', 
> 'EP0441070204', datetime.datetime(2006, 5, 27, 9, 31, 26), 1164783552L, 
> 0.0, 0, datetime.date(2006, 5, 23), 0, 0L, 0),
> 
> (1028L, datetime.datetime(2006, 5, 30, 7, 0), datetime.datetime(2006, 5, 
> 30, 7, 30), 'Arthur', 'Prunella Sees the Light; Return of the Snowball', 
> 'Prunella prepares for a sleepover with Marina; D.W. protects a 
> snowball.', 'Children', 'tooth.seiner.lan', None, 0L, None, 1L, 1L, 
> 'Default', 9L, 'SH044107', 'EP0441070123', datetime.datetime(2006, 5, 
> 30, 7, 31, 24), 1164179392L, 0.0, 1, datetime.date(2002, 11, 28), 0, 0L, 0)
> 
> ]
> 
> which is the correct format to insert into the new database.
> 
> What I'd like to do is build the correct selectlist in the first place, 
> rather than build the wrong one and then rebuild a correct one.
> 
> I can't find a replace method that would work on a tuple (not surprising 
> since they're immutable) but I also can't find a replace function that 
> would replace an element of a tuple and return a new tuple.

You can of course turn a tuple into a list, then modify the list. But it 
would still be a waste of time. Learning the full syntax for a SQL 
select might be more useful. You can do something like:

  select item1, item2, 'someconstant', item3, ..., from sometable (...)

which avoid having anything to replace.


Assuming that title + starttime is the primary key, and - not knowing 
the structure of your table - assuming that starttime is the second 
field, title the 5th and subtitle the 6th :

# ---------------------
sql = """
SELECT f1, starttime, f3, f4, title, subtitle, f7, '%s',  f9, 
(..etc...), f24
FROM recorded
ORDER BY title, starttime
""" % toGoBackend

c.execute(sql)
rows = c.fetchall()
# seems like dialog.checklist wants strings as tags
# tried with ints, but it crashed...
choices = [(str(i), row[5], 0) for row in rows]
d = dialog.Dialog(dialog="dialog")
d.add_persistent_args(["--backtitle", "Myth2Go"])
retcode, indices = d.checklist('', choices=choices)
if retcode:
   # exit or return

selectlist = [rows[i] for i in map(int, indices)]
# ---------------------



HTH



More information about the Python-list mailing list