Replace one element of a tuple (LONG)

Captain Dondo yan at NsOeSiPnAeMr.com
Fri Jun 2 00:19:38 CEST 2006


BartlebyScrivener wrote:
>>>I've looked at various search-and-replace snippets but none that address
>>>what I am trying to do....
> 
> 
> I think you need to tell more about what you're trying to do. You say
> it's in a database? Is that why you can't just put the whole blob in
> your text editor and do search-and-replace?
> 
> And is that also why you can't turn it into a giant string and do
> giantstring.replace('unwanted','wanted')
> 
> rd
> 

Fair enough.  I am trying to pull records from one database (on 
tooth.seiner.lan) and create a smaller table with only selected elements 
in another database (on localhost aka hermes).

My code so far:

import MySQLdb
import sys, os, os.path, time, string, dialog

masterBackend="tooth.seiner.lan"
toGoBackend="hermes.seiner.lan"

masterDB=MySQLdb.connect(host=masterBackend,user="mythtv",passwd="mythtv",db="mythconverg")

# pull recordings from masterDB

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()

(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))
         selectlist.append(c.fetchone())

========================================================

The problem is the last line.  I am creating a bunch of tuples that are, 
for my purposes, incorrect.  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),

(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.

--Yan



More information about the Python-list mailing list