Replace one element of a tuple
I have an array(?) (sorry, I'm new* to python so I'm probably mangling the terminology) that looks like this: [((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),), ((1028L, datetime.datetime(2006, 5, 4, 10, 0), datetime.datetime(2006, 5, 4, 10, 30), 'Bob the Builder', 'Using Clues', '', 'Children', 'tooth.seiner.lan', None, 0L, None, 1L, 1L, 'Default', 6L, 'SH326087', 'EP3260870141', datetime.datetime(2006, 5, 4, 10, 31, 30), 1163673536L, 0.0, 1, datetime.date(2005, 3, 19), 0, 0L, 0),)] I want to replace every instance of 'tooth.seiner.lan' with 'localhost'. There may be lots and lots of these entries (they're pulled from my mythtv database). I could brute-force this by rewriting the whole thing and replacing every 9th element but there has to be a better way I've looked at various search-and-replace snippets but none that address what I am trying to do --Yan *I'm not really new to python, just very very rusty. Last time I used it was about 3 years ago, and I was equally clueless -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace one element of a tuple (LONG)
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace one element of a tuple (LONG)
Brian wrote: Captain Dondo wrote: 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. This is sounding more like a SQL/DB problem and less like a Python one. If you have a field that is being pulled from the database that you would like to do a substitution on, I'm fairly sure MySQL includes a CASE statement in their flavor of SQL. Instead of performing a SELECT * in your script you could select the individual fields you want, and instead of just pulling the problematic column as is, you could pull it as something like select case when sourcehost = x then y else sourcehost end case, Naturally if an entire column needs to be replaced with a static value, you won't even need to use CASE. AFAICT, that one column is always the same, the name of the host that the database resides on. As myth can use multiple backends , it sort of makes sense, but it seems redunandant to me. Even with mutliple backends, I would hope you have a single database I thought about just picking all the other fields via the SQL query, but this seemed simpler to me Anyway, here's the code I came up with: for listing in itemlist: (rectitle, recdate) = listing.split('|',1) c.execute(SELECT * FROM recorded WHERE title=%s AND starttime=%s,(rectitle,recdate)) for listitem in c.fetchall(): if 'tooth.seiner.lan' in listitem: selectlist.append(listitem[:7] + ('hermes.seiner.lan',) + listitem[9:]) else: selectlist.append(listitem) -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace one element of a tuple (LONG)
BartlebyScrivener wrote: that one column is always the same, the name of the host that the database resides on. Then why are you pulling all of the other stuff out of the db? Why don't you just UPDATE tablename SET hostname(or colname) = 'localhost' WHERE search condition = the rows you want to change Well, there is an interactive dialog where the user picks which records s/he wants to put into the other database. So in the target database, that table may not even exist until the user selects which records go in there. But it does sound like I need to do some work on the database query :-) --Yan -- http://mail.python.org/mailman/listinfo/python-list
Re: Dumb*ss newbie Q
On Mon, 28 Mar 2005 08:42:22 -0600, Larry Bates wrote: Others have answered your specific question, I thought I would add some suggestions (not tested): 1) You don't need a separate set_title method. You can change the title attribute at any time by just saying m.title=new title. No method is required unless you need to do some pre/post processing. m.title=anything you want 2) To get class to prepare its output, just insert a __str__ method like following: def __str__(self): return 'a href=avi://%s/%s/%s img src=%s%s/tn/%s.jpg/a' % \ (self.audience, self.title, self.driver, self.audience, self.title, self.title) Then you can eliminate the html method and thumb method and just write: print m 3) Add extend keyword arguments for url and audience to the __init__ method. That way you (or other users) will know that each keyword means when they run in Idle or other interpreter that expands keyword arguments on the screen as you are typing): m=Movie(title=Fate_is_the_Hunter, audience=kids, \ driver=X=hermes.seiner.lan:xv,athena.seiner.lan:xmga, \ default:x11;console=vesa) print m If you plan on doing a lot of this you may want to take a look at the htmlgen module at: http://starship.python.net/crew/friedrich/HTMLgen/html/main.html (I actually stole the idea of using the the __str__ method to generate the output from this module). Hope information helps. Thanks, it does. I am trying to write a much simplified album - http://marginalhacks.com/Hacks/album/ . The author has taken it in a direction that is no longer useful to me and I am desparately incompetent in perl Python and I like each other, except that I don't have much experience writing it from scratch For now I am using this tutorial as a go-by: http://www.linuxgazette.com/issue19/python.html, but I will check out the link you provided. --Yan -- use munged address above to email me SpamTrap [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Dumb*ss newbie Q
OK, I know this is covered somewhere in Python 101, but for the life of me I cannot figure this out. I really need a basic intro to Python book I am trying to do something very simple - create an HTML tag using objects: class Movie: def __init__ (self, t=, a=, d=): # # Create an instance of Movie # self.title = t self.audience = a self.driver = d def set_title (self, new_title): self.title = new_title def set_audience (self, audience): # # Only 3 valid values: kids, parents, guests # self.audience = audience def set_driver (self, new_driver): self.driver = new_driver def url (self): self.url = avi:// + self.audience + / + self.title + / + self.driver def thumb (self): self.thumb = self.audience + /tn/ + self.title + .jpg def html (self): print a href=avi:// + self.audience + / + self.title + / + self.driver + img src= + self.thumb + + self.title + /a # # Code to test this class # if __name__ == '__main__': print Test 1 m=Movie(Fate_is_the_Hunter) m.set_audience (kids) m.set_title (Fate_is_the_Hunter) m.set_driver (X=hermes.seiner.lan:xv,athena.seiner.lan:xmga,default:x11;console=vesa) m.html () print *** Finish *** The problem is that m.html in the test section fails with TypeError: cannot concatenate 'str' and 'instancemethod' objects I got it working once. The output should be something like this: a href=avi://kids/Fate_is_the_Hunter/X=hermes.seiner.lan:xv,athena.seiner.lan:xmga,default:x11;console=vesa img src=kids/tn/Fate_is_the_Hunter.jpgFate_is_the_Hunter/a but then I made some minor edits and I can't get it to work again Where do I find documentation on Python classes? Or how do I convert one to the other? Or, how do I get the above to work? This is the first time I've really tried to work with a class I've defined myself and I obviously don't know what I am doing On a minor note, if you look at the audience method, I want to limit it to 3 values. How do I do that? TIA --Yan -- use munged address above to email me SpamTrap [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Dumb*ss newbie Q
On Mon, 28 Mar 2005 01:15:34 +, Jp Calderone wrote: Notice that you have a method named url as well as an attribute named url. You have the same problem for thumb. These methods and attributes are in collision with each other. When you try to look up the attribute, you might get the method. When you try to look up the method, you might get the attribute. It is deterministic, but depends on the order in which you do things, and highly confusing no matter what. Avoid naming attributes and methods the same thing. DUH!!! works like a charm Thanks! -- use munged address above to email me SpamTrap [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Help on project, anyone?
On Sun, 23 Jan 2005 21:11:32 +0100, Georg Brandl wrote: Hello, to train my Python skills I am looking for some project I can contribute to. I learned Python about one year ago, and had already some programming background behind (I contributed to SharpDevelop for instance), so I'm not the complete newbie. About myself: I'm a 20 year old German with strong interests in programming and, of course, especially in Python (I love it...). Does anyone run, or participate in, a project looking for fellow programmers? I don't have a special area of interest, well, perhaps web programming... Thanks, Georg Well, if you want to help, I've got a project that has been a python learning experience for me, and it has *lots* of potential ( read: very poor code and implementation ;-) ). Basically, it has to do with managing and playing movies on a computer, automatically moving them from recent arrivals to archives, keeping track of how many times each has been played, etc. I'd like to implement a simple text-based record for each movie to set up the brightness, volume, etc. I also need to write a back end that will generate web pages for the web interface; something along the lines of album http://marginalhacks.com/Hacks/album/ but the author is taking album in a direction that won't work for me anymore. Email me if interested. Remvoe the obvious from my email, and include this kpwq1jkcsEzdx39gnkVvgycd15ayqq anywhere in your email to get through my spam filters. --Yan -- http://mail.python.org/mailman/listinfo/python-list